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

}
}
return SteeringForce;
}
Alignment
Alignment attempts to keep a vehicle’s heading aligned with its neighbors.
See Figure 3.16, middle. The force is calculated by first iterating through
all the neighbors and averaging their heading vectors. This value is the
desired heading, so we just subtract the vehicle’s heading to get the steer
-
ing force.
Vector2D SteeringBehaviors::Alignment(const std::vector<Vehicle*>& neighbors)
{
//used to record the average heading of the neighbors
Vector2D AverageHeading;
//used to count the number of vehicles in the neighborhood
int NeighborCount = 0
//iterate through all the tagged vehicles and sum their heading vectors
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)
{
AverageHeading += neighbors[a]->Heading();
++NeighborCount;
}
}
//if the neighborhood contained one or more vehicles, average their
//heading vectors.
if (NeighborCount > 0)
{
AverageHeading /= (double)NeighborCount;
AverageHeading -= m_pVehicle->Heading();
}
return AverageHeading;
}
Cars moving along roads demonstrate alignment type behavior. They also
demonstrate separation as they try to keep a minimum distance from each
other.
116 | Chapter 3
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