O'Reilly logo

Programming Game AI by Example by Mat Buckland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Separation
Separation creates a force that steers a vehicle away from those in its
neighborhood region. When applied to a number of vehicles, they will
spread out, trying to maximize their distance from every other vehicle. See
Figure 3.16, top.
This is an easy behavior to implement. Prior to calling separation, all the
agents situated within a vehicle’s neighborhood are tagged. Separation
then iterates through the tagged vehicles, examining each one. The vector
to each vehicle under consideration is normalized, divided by the distance
to the neighbor, and added to the steering force.
Vector2D SteeringBehaviors::Separation(const std::vector<Vehicle*>& neighbors)
{
Vector2D SteeringForce;
for (int a=0; a<neighbors.size(); ++a)
{
//make sure this agent isn't included in the calculations and that
//the agent being examined is close enough.
if((neighbors[a] != m_pVehicle) && neighbors[a]->IsTagged())
{
Vector2D ToAgent = m_pVehicle->Pos() - neighbors[a]->Pos();
//scale the force inversely proportional to the agent's distance
//from its neighbor.
SteeringForce += Vec2DNormalize(ToAgent)/ToAgent.Length();
How to Create Autonomously Moving Game Agents
| 115
Group Behaviors
Figure 3.16. The separation, alignment, and cohesion group behaviors

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required