当前位置: 首页>>代码示例>>C++>>正文


C++ BaseGameEntity::getContentSize方法代码示例

本文整理汇总了C++中BaseGameEntity::getContentSize方法的典型用法代码示例。如果您正苦于以下问题:C++ BaseGameEntity::getContentSize方法的具体用法?C++ BaseGameEntity::getContentSize怎么用?C++ BaseGameEntity::getContentSize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BaseGameEntity的用法示例。


在下文中一共展示了BaseGameEntity::getContentSize方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: ObstacleAvoidance

Vec2 SteeringForce::ObstacleAvoidance(const std::vector<BaseGameEntity *> obstacles)
{
    m_dDBoxLength = m_pVehicle->getVeloctity().length()/m_pVehicle->getMaxSpeed()*40+40;
    
    //标记范围内的所有障碍物
    GameData::Instance()->tagObstaclesWithinViewRange(m_pVehicle, m_dDBoxLength);
    
    
    //追踪最近相交的障碍物
    BaseGameEntity *ClosestIntersectingObstacle = nullptr;
    
    //追踪到CIB的距离
    double DistToClosestIP =  MAXFLOAT;
    
    //记录CIB被转化的局部坐标
    Vec2 LocalPosOfClosestObstacle;
    
    std::vector<BaseGameEntity*>::const_iterator curOb = obstacles.begin();
    while (curOb!= obstacles.end()) {
        if ((*curOb)->isTagged()) {
            Vec2 LocalPos =  m_pVehicle->convertToNodeSpaceAR((*curOb)->getPosition());
            
            //如果举办空间位置有个负的x值,那么它肯定在智能体后面
            if (LocalPos.x >=0) {
                //如果物体到x轴的距离小于它的半径+检查盒宽度的一半
                //那么可能相交
                double ExpandeRadius = (*curOb)->getContentSize().height/2*(*curOb)->getScale()+m_pVehicle->getContentSize().height/2;
                
                if (fabs(LocalPos.y)<ExpandeRadius) {
                    //现在做线/圆周相交厕所吗 圆周圆心是(cx,cy);
                
                    //相交点的公式是x = cx +(-sqrt(  r*r - cY*cY));
                    //我们只需看x的最小值,因为那是最近的相交点
                    
                    double cX = LocalPos.x;
                    double cY = LocalPos.y;
                    //我们只需一次计算上面等式的开方
                    
                    double SqrtPart = sqrt(ExpandeRadius*ExpandeRadius-cY*cY);
                    
                    double ip = SqrtPart;
                    if (ip<=0) {
                        ip = cX+SqrtPart;
                    }
                    if (ip<DistToClosestIP) {
                        DistToClosestIP = ip;
                        
                        ClosestIntersectingObstacle = *curOb;
                        LocalPosOfClosestObstacle = LocalPos;
                    }
                }
            }
        }
        ++curOb;
    }
    
    
    Vec2 SteeringForce;
    
    if (ClosestIntersectingObstacle) {
        
        //智能体越近,操控力越强
        double muliplier =1 + (m_dDBoxLength-LocalPosOfClosestObstacle.x)/m_dDBoxLength;
        
        
        //计算侧向力
        SteeringForce.y = (ClosestIntersectingObstacle->getContentSize().height/2*ClosestIntersectingObstacle->getScale()-LocalPosOfClosestObstacle.y)*muliplier;
        
        
        if (LocalPosOfClosestObstacle.y>0&&LocalPosOfClosestObstacle.y<ClosestIntersectingObstacle->getContentSize().height/2*ClosestIntersectingObstacle->getScale()) {
            SteeringForce.y = -SteeringForce.y;
            }
        
       
        //施加一个制动力,它正比于障碍物到交通工具的距离
        const double BrakingWeight = 0.3;
        SteeringForce.x = (ClosestIntersectingObstacle->getContentSize().height/2*ClosestIntersectingObstacle->getScale()-LocalPosOfClosestObstacle.x)*BrakingWeight;
    }
    
    //最后,把操控向量换成世界坐标
//    log("********SteeringForce:%f,%f*********",( m_pVehicle->convertToWorldSpaceAR(SteeringForce)-m_pVehicle->getPosition()).x,( m_pVehicle->convertToWorldSpaceAR(SteeringForce)-m_pVehicle->getPosition()).y);
    
    return m_pVehicle->convertToWorldSpaceAR(SteeringForce)-m_pVehicle->getPosition();
    
}
开发者ID:TinySlik,项目名称:SteeringBehaviors,代码行数:85,代码来源:SteeringForce.cpp


注:本文中的BaseGameEntity::getContentSize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。