Vec2 SteeringForce::Interpose(const Vehicle *AgentA, const Vehicle *AgentB) { //首先,我们需要算出在未来时间T时,这个两个智能体的位置。 //交通工具以最大速度到达中点所花的时间近似于T Vec2 MidPoint = (AgentA->getPosition() + AgentB->getPosition())/2.0; double TimeToReachMidPoint = m_pVehicle->getPosition().distance(MidPoint)/m_pVehicle->getMaxSpeed(); //现在我们有T,我们假设智能体A和智能体B将继续径直前行 //推断得到他们将来的位置 Vec2 APos = AgentA->getPosition() + AgentA->getVeloctity()*TimeToReachMidPoint; Vec2 BPos = AgentB->getPosition() + AgentB->getVeloctity()*TimeToReachMidPoint; MidPoint = (APos+BPos)/2.0; return Arrive(MidPoint, fast); }
Vector2D B020612E_Steering::CalculatePrioritised() { Vector2D force; if (On(STEERING_OBSTACLEAVOIDANCE)) { force = ObstacleAvoidance(ObstacleManager::Instance()->GetObstacles()) * 10.0f; if (!AccumulateForce(_pSteeringForce, force)) return _pSteeringForce; } if (On(STEERING_FLEE)) { force = Flee(_pTank->GetClick()) * 1.0f; if (!AccumulateForce(_pSteeringForce, force)) return _pSteeringForce; } if (On(STEERING_SEEK)) { force = Seek(_pTank->GetClick()) * 1.0f; if (!AccumulateForce(_pSteeringForce, force)) return _pSteeringForce; } if (On(STEERING_WANDER)) { force = Wander() * 1.0f; if (!AccumulateForce(_pSteeringForce, force)) return _pSteeringForce; } if (On(STEERING_ARRIVE)) { force = Arrive(_pTank->GetClick(), _pDeceleration) * 1.0f; if (!AccumulateForce(_pSteeringForce, force)) return _pSteeringForce; } return _pSteeringForce; }
//--------------------------- 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); }
Vector2D B020612E_Steering::CalculateWeightedSum() { //if (On(STEERING_OBSTACLEAVOIDANCE)) //_pSteeringForce += ObstacleAvoidance(); if (On(STEERING_WANDER)) _pSteeringForce += Wander() * 1.0f; if (On(STEERING_SEEK)) _pSteeringForce += Seek(_pTank->GetClick()) * 1.0f; if (On(STEERING_FLEE)) _pSteeringForce += Flee(_pTank->GetClick()) * 1.0f; if (On(STEERING_ARRIVE)) _pSteeringForce += Arrive(_pTank->GetClick(), _pDeceleration) * 1.0f; if (On(STEERING_PURSUIT)) _pSteeringForce += Pursuit(_pTank->GetMotion(), _pTank->GetMouseVelocity()) * 1.0f; //if (On(STEERING_PATHFOLLOWING)) // _pSteeringForce += FollowPath() * 0.5f; _pSteeringForce.Truncate(_pTank->GetMaxForce()); return _pSteeringForce; }
//------------------------- Offset Pursuit ------------------------------- // // Produces a steering force that keeps a MovingEntity at a specified offset // from a leader MovingEntity //------------------------------------------------------------------------ Vector2D SteeringBehavior::OffsetPursuit(const MovingEntity* leader, const float offset) { Vector2D displacement = leader->Heading() * - offset; //calculate the offset's position in world space Vector2D WorldOffsetPos = PointToWorldSpace(displacement, leader->Heading(), leader->Pos()); Vector2D ToOffset = WorldOffsetPos - m_pMovingEntity->Pos(); //the lookahead time is propotional to the distance between the leader //and the pursuer; and is inversely proportional to the sum of both //agent's velocities double LookAheadTime = ToOffset.Length() / (m_pMovingEntity->MaxSpeed() + leader->Speed()); //now Arrive at the predicted future position of the offset return Arrive(WorldOffsetPos + leader->Velocity() * LookAheadTime, fast); }
Vector2 TikiSteering::CalculatePrioritized() { Vector2 force = Vector2::Zero; if (On(separation)) { force = Separation() * weightSeparation; if (!AccumulateForce(steeringForce, force)) return steeringForce; } if(On(seek)) { force = Seek(target) * weightSeek; if (!AccumulateForce(steeringForce, force)) return steeringForce; } if (On(arrive)) { force = Arrive(target, deceleration) * weightArrive; if (!AccumulateForce(steeringForce, force)) return steeringForce; } if (On(wander)) { force = Wander() * weightWander; if (!AccumulateForce(steeringForce, force)) return steeringForce; } return steeringForce; }
Vector2D S013010C_Aaron_Smith_Steering::Calculate(float deltaTime) { Vector2D force; Vector2D totalForce; if (mIsObstacleAvoiding) { force = ObstacleAvoidance() * mObstacleAvoidanceWeight; if (!AccumulateForce(totalForce, force)) return totalForce; } if (mIsWallAvoided) { force = WallAvoidance() * mWallAvoidWeight; if (!AccumulateForce(totalForce, force)) return totalForce; } if (mIsWandering) { force = Wander(deltaTime) * mSeekWeight; if (!AccumulateForce(totalForce, force)) return totalForce; } if (mIsPursuing) { force = Pursuing() * mPursuitWeight; if (!AccumulateForce(totalForce, force)) return totalForce; } if (mIsSeeking) { force = Seek(mTank->GetTarget()) * mSeekWeight; if (!AccumulateForce(totalForce, force)) return totalForce; } if (mIsArriving) { force = Arrive(mTank->GetTarget(), Deceleration::NORMAL) * mArriveWeight; if (!AccumulateForce(totalForce, force)) return totalForce; } if (mIsFleeing) { force = Flee(mTank->GetTarget()) * mFleeWeight; if (!AccumulateForce(totalForce, force)) return totalForce; } return totalForce; //if (isFleeing) Flee(mTank->GetTarget()); //Vector2D totalForce; /*for (Vector2D vec : mSteeringForces) { totalForce += vec; } mSteeringForces.clear(); return totalForce;*/ }
//---------------------- CalculateWeightedSum ---------------------------- // // this simply sums up all the active behaviors X their weights and // truncates the result to the max available steering force before // returning //------------------------------------------------------------------------ ofVec3f SteeringBehaviors::CalculateWeightedSum() { if (On(wall_avoidance)) { m_SteeringForce += WallAvoidance(m_Vehicle->GameWorld()->getWalls()) * m_WeightObstacleAvoidance; } if (On(obstacle_avoidance)) { m_SteeringForce += ObstacleAvoidance( m_Vehicle->GameWorld()->getObstacles() ) * m_WeightObstacleAvoidance; } if (On(seek)) { m_SteeringForce += Seek(m_Vehicle->Target()->Pos()) * m_WeightSeek; } if (On(flee)) { m_SteeringForce += Flee(m_Vehicle->Target()->Pos()) * m_WeightFlee; } if (On(arrive)) { m_SteeringForce += Arrive(m_Vehicle->Target()->Pos(), normal) * m_WeightArrive; } if (!isSpacePartioningOn()) { if (On(separation)) { m_SteeringForce += Separation(m_Vehicle->rGroup()) * m_WeightSeparation; } if (On(alignment)) { m_SteeringForce += Alignment(m_Vehicle->rGroup()) * m_WeightAlignment; } if (On(cohesion)) { m_SteeringForce += Cohesion(m_Vehicle->rGroup()) * m_WeightCohesion; } } else { if (On(separation)) { m_SteeringForce += SeparationPlus(m_Vehicle->rGroup()) * m_WeightSeparation; } if (On(alignment)) { m_SteeringForce += AlignmentPlus(m_Vehicle->rGroup()) * m_WeightAlignment; } if (On(cohesion)) { m_SteeringForce += CohesionPlus(m_Vehicle->rGroup()) * m_WeightCohesion; } } if (On(repulsion)) { //need to retag the guys! m_SteeringForce += Repulsion(m_Vehicle->oGroup()) * m_WeightRepulsion; } if (On(wander)) { m_SteeringForce += Wander() * m_WeightWander; } if (On(follow_path)) { m_SteeringForce += FollowPath() * m_WeightFollowPath; } m_SteeringForce.limit(m_Vehicle->MaxForce()); return m_SteeringForce; }
//---------------------- CalculatePrioritized ---------------------------- // // this method calls each active steering behavior in order of priority // and acumulates their forces until the max steering force magnitude // is reached, at which time the function returns the steering force // accumulated to that point //------------------------------------------------------------------------ ofVec3f SteeringBehaviors::CalculatePrioritized() { ofVec3f force; if (On(wall_avoidance)) { force = WallAvoidance(m_Vehicle->GameWorld()->getWalls()) * m_WeightObstacleAvoidance; if (!AccumulateForce(m_SteeringForce, force)) return m_SteeringForce; } if (On(obstacle_avoidance)) { force = ObstacleAvoidance(m_Vehicle->GameWorld()->getObstacles()) * m_WeightObstacleAvoidance; if (!AccumulateForce(m_SteeringForce, force)) return m_SteeringForce; } if (On(flee)) { force = Flee(m_Vehicle->Target()->Pos()) * m_WeightFlee; if (!AccumulateForce(m_SteeringForce, force)) return m_SteeringForce; } if (On(repulsion)) { //need to retage the guys! force = Repulsion(m_Vehicle->oGroup()) * m_WeightRepulsion; if (!AccumulateForce(m_SteeringForce, force)) return m_SteeringForce; } if (!isSpacePartioningOn()) { if (On(separation)) { force = Separation(m_Vehicle->rGroup()) * m_WeightSeparation; if (!AccumulateForce(m_SteeringForce, force)) return m_SteeringForce; } if (On(alignment)) { force = Alignment(m_Vehicle->rGroup()) * m_WeightAlignment; if (!AccumulateForce(m_SteeringForce, force)) return m_SteeringForce; } if (On(cohesion)) { force = Cohesion(m_Vehicle->rGroup()) * m_WeightCohesion; if (!AccumulateForce(m_SteeringForce, force)) return m_SteeringForce; } } else { if (On(separation)) { force = SeparationPlus(m_Vehicle->rGroup()) * m_WeightSeparation; if (!AccumulateForce(m_SteeringForce, force)) return m_SteeringForce; } if (On(alignment)) { force = AlignmentPlus(m_Vehicle->rGroup()) * m_WeightAlignment; if (!AccumulateForce(m_SteeringForce, force)) return m_SteeringForce; } if (On(cohesion)) { force = CohesionPlus(m_Vehicle->rGroup()) * m_WeightCohesion; if (!AccumulateForce(m_SteeringForce, force)) return m_SteeringForce; } } if (On(seek)) { force = Seek(m_Vehicle->Target()->Pos()) * m_WeightSeek; if (!AccumulateForce(m_SteeringForce, force)) return m_SteeringForce; } if (On(arrive)) { force = Arrive(m_Vehicle->Target()->Pos(), normal) * m_WeightArrive; if (!AccumulateForce(m_SteeringForce, force)) return m_SteeringForce; } if (On(wander)) { force = Wander() * m_WeightWander; if (!AccumulateForce(m_SteeringForce, force)) return m_SteeringForce; } if (On(follow_path)) { force = FollowPath() * m_WeightFollowPath; if (!AccumulateForce(m_SteeringForce, force)) return m_SteeringForce; } return m_SteeringForce; }
Vec2 SteeringForce::Calculate() { m_vSteeringForce = Vec2::ZERO; Vec2 force; std::vector<Vehicle*> neighbors; if (On(allignment)||On(separation)||On(cohesion)) { if (m_pVehicle->isCellSpaceOn()) { CellSpacePartition<Vehicle*>* cellSpace = GameData::Instance()->getCellSpace(); neighbors = cellSpace->getNeighbors(); }else{ auto data = GameData::Instance()->getEntityVector(); neighbors = tagNeighbors(m_pVehicle,data,SearchRad); } } if (On(wall_avoidance)) { auto data = GameData::Instance()->getWallls(); force = WallAvoidance(data); if (!AccumulateForce(m_vSteeringForce, force)) { return m_vSteeringForce; } } if (On(obstacle_avoidance)) { auto data = GameData::Instance()->getObstacle(); force = ObstacleAvoidance(data); if (!AccumulateForce(m_vSteeringForce, force)) { return m_vSteeringForce; } } if (On(separation)) { force = Separation(neighbors); if (!AccumulateForce(m_vSteeringForce, force)) { return m_vSteeringForce; } } if (On(cohesion)) { force = Cohesion(neighbors); if (!AccumulateForce(m_vSteeringForce, force)) { return m_vSteeringForce; } } if (On(allignment)) { force = Alignment(neighbors); if (!AccumulateForce(m_vSteeringForce, force)) { return m_vSteeringForce; } } if (On(hide)) { CCASSERT(m_pVehicle->getHideTarget()!=nullptr, "不存在躲避目标"); auto data = GameData::Instance()->getEntityVector(); force = Hide(m_pVehicle->getHideTarget(),data); if (!AccumulateForce(m_vSteeringForce, force)) { return m_vSteeringForce; } } if (On(follow_path)) { force = PathFollow(); if (!AccumulateForce(m_vSteeringForce, force)) { return m_vSteeringForce; } } if (On(seek)) { force = Seek(m_pVehicle->getTarget()); if (!AccumulateForce(m_vSteeringForce, force)) { return m_vSteeringForce; } } if (On(flee)) { force = Flee(m_pVehicle->getTarget()); if (!AccumulateForce(m_vSteeringForce, force)) { return m_vSteeringForce; } } if (On(arrive)) { force = Arrive(m_pVehicle->getTarget(), fast); if (!AccumulateForce(m_vSteeringForce, force)) { return m_vSteeringForce; } } if (On(offset_pursuit)) { force = OffsetPursuit(m_pVehicle->getLeader(), m_pVehicle->getOffsetToLeader()); if (!AccumulateForce(m_vSteeringForce, force)) { return m_vSteeringForce; } } if (On(pursuit)) { force = Pursuit(m_pVehicle->getEvaderv()); if (!AccumulateForce(m_vSteeringForce, force)) { return m_vSteeringForce; } } if (On(evade)) { force = Evade(m_pVehicle->getPursuer()); if (!AccumulateForce(m_vSteeringForce, force)) { return m_vSteeringForce; } } if (On(wander)) { force = Wander(); if (!AccumulateForce(m_vSteeringForce, force)) { return m_vSteeringForce; } } if (On(interpose)) { force = Interpose(m_pVehicle->getInterposeA(), m_pVehicle->getInterposeB()); if (!AccumulateForce(m_vSteeringForce, force)) { return m_vSteeringForce; } } return m_vSteeringForce; }
//---------------------- CalculateWeightedSum ---------------------------- // // this simply sums up all the active behaviors X their weights and // truncates the result to the max available steering force before // returning //------------------------------------------------------------------------ Vector2D SteeringBehavior::CalculateWeightedSum(float dt) { //reset the steering force m_vSteeringForce.Zero(); if (On(bounds_avoidance)) { Vector2D force = BoundsAvoidance() * m_dWeightBoundsAvoidance; /*if (!force.isZero()) { std::cout << "avoid wall: " << force << std::endl; }*/ m_vSteeringForce += force; } if (On(obstacle_avoidance)) { Vector2D force = ObstacleAvoidance() * m_dWeightObstacleAvoidance; /*if (!force.isZero()) { std::cout << "avoid obst: " << force << std::endl; }*/ m_vSteeringForce += force; } if (On(flee)) { assert(fleeTarget && "Flee target not assigned"); m_vSteeringForce += Flee(fleeTarget->Pos()) * m_dWeightFlee; } if (On(seek)) { assert(seekTarget && "Seek target not assigned"); m_vSteeringForce += Seek(seekTarget->Pos()) * m_dWeightSeek; } if (On(arrive)) { assert(seekTarget && "Arrive target not assigned"); m_vSteeringForce += Arrive(seekTarget->Pos(), m_Deceleration) * m_dWeightArrive; } if (On(wander)) { Vector2D force = Wander(dt) * m_dWeightWander; //std::cout << "wander: " << force << std::endl; m_vSteeringForce += force; } if (On(pursuit)) { assert(pursuitTarget && "pursuit target not assigned"); m_vSteeringForce += Pursuit(pursuitTarget) * m_dWeightPursuit; } if (On(offset_pursuit)) { assert (pursuitTarget && "pursuit target not assigned"); m_vSteeringForce += OffsetPursuit(pursuitTarget, m_vOffset) * m_dWeightOffsetPursuit; } if (On(hide)) { assert(hideTarget && "Hide target not assigned"); m_vSteeringForce += Hide() * m_dWeightHide; } if (On(forward)) { Vector2D force = Forward() * m_dWeightForward; m_vSteeringForce += force; } //m_vSteeringForce.Truncate(m_pMovingEntity->MaxForce()); return m_vSteeringForce; }
//---------------------- CalculatePrioritized ---------------------------- // // this method calls each active steering behavior in order of priority // and acumulates their forces until the max steering force magnitude // is reached, at which time the function returns the steering force // accumulated to that point //------------------------------------------------------------------------ Vector2D SteeringBehavior::CalculatePrioritized(float dt) { //reset the steering force m_vSteeringForce.Zero(); Vector2D force; if (On(bounds_avoidance)) { force = BoundsAvoidance() * m_dWeightBoundsAvoidance; if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; } if (On(obstacle_avoidance)) { force = ObstacleAvoidance() * m_dWeightObstacleAvoidance; if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; } if (On(flee)) { assert(fleeTarget && "Flee target not assigned"); force = Flee(fleeTarget->Pos()) * m_dWeightFlee; if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; } if (On(seek)) { assert(seekTarget && "Seek target not assigned"); force = Seek(seekTarget->Pos()) * m_dWeightSeek; if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; } if (On(arrive)) { assert(seekTarget && "Arrive target not assigned"); force = Arrive(seekTarget->Pos(), m_Deceleration) * m_dWeightArrive; if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; } if (On(wander)) { force = Wander(dt) * m_dWeightWander; if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; } if (On(pursuit)) { assert(pursuitTarget && "pursuit target not assigned"); force = Pursuit(pursuitTarget) * m_dWeightPursuit; if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; } if (On(offset_pursuit)) { assert (pursuitTarget && "pursuit target not assigned"); force = OffsetPursuit(pursuitTarget, m_vOffset); if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; } if (On(hide)) { assert(hideTarget && "Hide target not assigned"); force = Hide() * m_dWeightHide; if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; } if (On(forward)) { force = Forward() * m_dWeightForward; if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; } return m_vSteeringForce; }
void SObject::TakeTurn() { target = NULL; if(destination && cur_game->turn >= arrive_turn) Arrive(); }
Vector2D SteeringBehavior::CalculateWeightedSum() { //if (On(wall_avoidance)) //{ //m_vSteeringForce += WallAvoidance(m_pVehicle->World()->Walls()) * // m_dWeightWallAvoidance; //} // //if (On(obstacle_avoidance)) //{ //m_vSteeringForce += ObstacleAvoidance(m_pVehicle->World()->Obstacles()) * // m_dWeightObstacleAvoidance; //} //if (On(evade)) //{ //assert(m_pTargetAgent1 && "Evade target not assigned"); // //m_vSteeringForce += Evade(m_pTargetAgent1) * m_dWeightEvade; //} ////these next three can be combined for flocking behavior (wander is ////also a good behavior to add into this mix) //if (!isSpacePartitioningOn()) //{ //if (On(separation)) //{ // m_vSteeringForce += Separation(m_pVehicle->World()->Agents()) * m_dWeightSeparation; //} //if (On(allignment)) //{ // m_vSteeringForce += Alignment(m_pVehicle->World()->Agents()) * m_dWeightAlignment; //} //if (On(cohesion)) //{ // m_vSteeringForce += Cohesion(m_pVehicle->World()->Agents()) * m_dWeightCohesion; //} //} //else //{ //if (On(separation)) //{ // m_vSteeringForce += SeparationPlus(m_pVehicle->World()->Agents()) * m_dWeightSeparation; //} //if (On(allignment)) //{ // m_vSteeringForce += AlignmentPlus(m_pVehicle->World()->Agents()) * m_dWeightAlignment; //} //if (On(cohesion)) //{ // m_vSteeringForce += CohesionPlus(m_pVehicle->World()->Agents()) * m_dWeightCohesion; //} //} //if (On(wander)) //{ //m_vSteeringForce += Wander() * m_dWeightWander; //} if (On(seek)) { m_vSteeringForce += Seek(m_pVehicle->World()->Crosshair()) * m_dWeightSeek; } if (On(flee)) { m_vSteeringForce += Flee(m_pVehicle->World()->Crosshair()) * m_dWeightFlee; } if (On(arrive)) { m_vSteeringForce += Arrive(m_pVehicle->World()->Crosshair(), m_Deceleration) * m_dWeightArrive; } //if (On(pursuit)) //{ //assert(m_pTargetAgent1 && "pursuit target not assigned"); //m_vSteeringForce += Pursuit(m_pTargetAgent1) * m_dWeightPursuit; //} //if (On(offset_pursuit)) //{ //assert (m_pTargetAgent1 && "pursuit target not assigned"); //assert (!m_vOffset.isZero() && "No offset assigned"); //m_vSteeringForce += OffsetPursuit(m_pTargetAgent1, m_vOffset) * m_dWeightOffsetPursuit; //} //if (On(interpose)) //{ //assert (m_pTargetAgent1 && m_pTargetAgent2 && "Interpose agents not assigned"); //m_vSteeringForce += Interpose(m_pTargetAgent1, m_pTargetAgent2) * m_dWeightInterpose; //} //if (On(hide)) //{ //assert(m_pTargetAgent1 && "Hide target not assigned"); //m_vSteeringForce += Hide(m_pTargetAgent1, m_pVehicle->World()->Obstacles()) * m_dWeightHide; //} //if (On(follow_path)) //{ //m_vSteeringForce += FollowPath() * m_dWeightFollowPath; //} //m_vSteeringForce.Truncate(m_pVehicle->MaxForce()); // //return m_vSteeringForce; }