// Applies the three laws to the flock of boids void Boid::flock(vector<Boid> v) { Pvector sep = Separation(v); Pvector ali = Alignment(v); Pvector coh = Cohesion(v); // Arbitrarily weight these forces sep.mulScalar(SepW); ali.mulScalar(AliW); // Might need to alter weights for different characteristics coh.mulScalar(CohW); // Add the force vectors to acceleration applyForce(sep); applyForce(ali); applyForce(coh); }
void Small::Update(int mousex, int mousey) { //XMVECTOR playerlocation = {playerx, playery, 0, 0}; //float displacement = XMVectorGetX(XMVector2Length(playerlocation - Location)); //if (displacement <= 130.0f) //{ //Separation(0.000f); //Cohesion(0.000f); //Alignment(0.0f); //Chase(0.03f); //} //else //{ if (Flocking) { Separation(0.04f); Cohesion(0.01f); Alignment(0.01f); } else { //Wander } if (Chasing) { Chase(0.05f); } BorderRestrict(); if (Chasing) { Location += (Direction * 3.3f); } PointingTwoLocation = Location + (Direction * 10.0f); }
//---------------------- 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; }
Vector2D SteeringBehavior::CalculatePrioritized() { Vector2D force; //if (On(wall_avoidance)) //{ // force = WallAvoidance(m_pVehicle->World()->Walls()) * // m_dWeightWallAvoidance; // if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; //} //if (On(obstacle_avoidance)) //{ // force = ObstacleAvoidance(m_pVehicle->World()->Obstacles()) * // m_dWeightObstacleAvoidance; // if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; //} //if (On(evade)) //{ // assert(m_pTargetAgent1 && "Evade target not assigned"); // force = Evade(m_pTargetAgent1) * m_dWeightEvade; // if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; //} if (On(flee)) { force = Flee(vehicle_->GetGameWorld()->Agents()) * m_dWeightFlee; if (!AccumulateForce(steering_force_, force)) return steering_force_; } // The next three can be combined for flocking behavior (wander is // Also a good behavior to add into this mix) if (On(separation)) { force = Separation(vehicle_->GetGameWorld()->Agents()) * m_dWeightSeparation; if (!AccumulateForce(steering_force_, force)) return steering_force_; } if (On(alignment)) { force = Alignment(vehicle_->GetGameWorld()->Agents()) * m_dWeightAlignment; if (!AccumulateForce(steering_force_, force)) return steering_force_; } if (On(cohesion)) { force = Cohesion(vehicle_->GetGameWorld()->Agents()) * m_dWeightCohesion; if (!AccumulateForce(steering_force_, force)) return steering_force_; } //if (On(seek)) //{ // force = Seek(m_pVehicle->World()->Crosshair()) * m_dWeightSeek; // if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; //} //if (On(arrive)) //{ // force = Arrive(m_pVehicle->World()->Crosshair(), m_Deceleration) * m_dWeightArrive; // if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; //} if (On(wander)) { force = Wander() * weight_wander_; if (!AccumulateForce(steering_force_, force)) return steering_force_; } if (On(pursuit)) { //assert(target_agent_ && "pursuit target not assigned"); force = Pursuit(target_agent_) * m_dWeightPursuit; if (!AccumulateForce(steering_force_, force)) return steering_force_; } /* if (On(offset_pursuit)) { assert(m_pTargetAgent1 && "pursuit target not assigned"); assert(!m_vOffset.isZero() && "No offset assigned"); force = OffsetPursuit(m_pTargetAgent1, m_vOffset); if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; } if (On(interpose)) { assert(m_pTargetAgent1 && m_pTargetAgent2 && "Interpose agents not assigned"); force = Interpose(m_pTargetAgent1, m_pTargetAgent2) * m_dWeightInterpose; if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; } if (On(hide)) { assert(m_pTargetAgent1 && "Hide target not assigned"); force = Hide(m_pTargetAgent1, m_pVehicle->World()->Obstacles()) * m_dWeightHide; if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; } if (On(follow_path)) { force = FollowPath() * m_dWeightFollowPath; if (!AccumulateForce(m_vSteeringForce, force)) return m_vSteeringForce; }*/ return steering_force_; }
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; }