Vec2 SteeringForce::Hide(const Vehicle *target, std::vector<Vehicle *> &obstacles) { double DistToClosest = MAXFLOAT; Vec2 BestHidingSpot; std::vector<Vehicle*>::iterator curOb = obstacles.begin(); while (curOb!=obstacles.end()) { if (*curOb!=target && *curOb != m_pVehicle) { //计算这个障碍物的hide地点 Vec2 HidingSpot = GetHidingPosition((*curOb)->getPosition(), (*curOb)->getContentSize().width/2, target->getPosition()); double dist = HidingSpot.distance(m_pVehicle->getPosition()); if (dist <DistToClosest) { DistToClosest = dist; BestHidingSpot = HidingSpot; } } ++curOb; } if (DistToClosest == MAXFLOAT) { return Evade(target); } return Arrive(BestHidingSpot, fast); }
//--------------------------- Hide --------------------------------------- // //------------------------------------------------------------------------ Vector2D SteeringBehavior::Hide() { double DistToClosest = MaxDouble; Vector2D BestHidingSpot; std::set<Obstacle*>::const_iterator curOb = Obstacle::getAll().begin(); std::set<Obstacle*>::const_iterator closest; while(curOb != Obstacle::getAll().end()) { //calculate the position of the hiding spot for this obstacle Vector2D HidingSpot = GetHidingPosition((*curOb)->Pos(), (*curOb)->BRadius(), hideTarget->Pos()); //work in distance-squared space to find the closest hiding //spot to the agent double dist = Vec2DDistanceSq(HidingSpot, m_pMovingEntity->Pos()); if (dist < DistToClosest) { DistToClosest = dist; BestHidingSpot = HidingSpot; closest = curOb; } ++curOb; }//end while //if no suitable obstacles found then Evade the hunter if (DistToClosest == MaxFloat) { return Flee(hideTarget->Pos()); } //else use Arrive on the hiding spot return Arrive(BestHidingSpot, fast); }