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

over again. If it’s an open path, the vehicle should just decelerate to a stop
(arrive) over the final waypoint.
Path is a class that looks after all these details. I’m not going to list it
here but you may like to examine it in your IDE. You can find it in the file
Path.h.
The simplest way of following a path is to set the current waypoint to
the first in the list, steer toward that using seek until the vehicle comes
within a target distance of it, then grab the next waypoint and seek to that,
and so on, until the current waypoint is the last waypoint in the list. When
this happens the vehicle should either arrive at the current waypoint, or, if
the path is a closed loop, the current waypoint should be set to the first in
the list again, and the vehicle just keeps on seeking. Here’s the code for
path following:
SVector2D SteeringBehaviors::FollowPath()
{
//move to next target if close enough to current target (working in
//distance squared space)
if(Vec2DDistanceSq(m_pPath->CurrentWaypoint(), m_pVehicle->Pos()) <
m_WaypointSeekDistSq)
{
m_pPath->SetNextWaypoint();
}
if (!m_pPath->Finished())
{
return Seek(m_pPath->CurrentWaypoint());
}
else
{
return Arrive(m_pPath->CurrentWaypoint(), normal);
}
}
You have to be very careful when implementing path following. The
behavior is very sensitive to the max steering force/max speed ratio and
also the variable
m_WaypointSeekDistSq. The demo executable for this
behavior allows you to alter these values to see what effect they have. As
you will discover, it’s easy to create behavior that is sloppy. How tight you
need the path following to be depends entirely on your game environment.
If you have a game with lots of gloomy tight corridors, then you’re (proba
-
bly) going to need stricter path following than a game set in the Sahara.
Offset Pursuit
Offset pursuit calculates the steering force required to keep a vehicle posi
-
tioned at a specified offset from a target vehicle. This is particularly useful
for creating formations. When you watch an air display, such as the British
Red Arrows, many of the spectacular maneuvers require that the aircraft
How to Create Autonomously Moving Game Agents | 111
The Steering Behaviors
remain in the same relative positions to the lead aircraft. See Figure 3.14.
This is the sort of behavior we want to emulate.
The offset is always defined in “leader” space, so the first thing to do when
calculating this steering force is to determine the offset’s position in world
space. After that the function proceeds similar to pursuit: A future position
for the offset is predicted and the vehicle arrives at that position.
SVector2D SteeringBehaviors::OffsetPursuit(const Vehicle* leader,
const SVector2D offset)
{
//calculate the offset’s position in world space
SVector2D WorldOffsetPos = PointToWorldSpace(offset,
leader->Heading(),
leader->Side(),
leader->Pos());
SVector2D ToOffset = WorldOffsetPos - m_pVehicle->Pos();
//the look-ahead time is proportional to the distance between the leader
//and the pursuer; and is inversely proportional to the sum of both
//agents’ velocities
double LookAheadTime = ToOffset.Length() /
(m_pVehicle->MaxSpeed() + leader->Speed());
//now arrive at the predicted future position of the offset
return Arrive(WorldOffsetPos + leader->Velocity() * LookAheadTime, fast);
}
Arrive is used instead of seek as it gives far smoother motion and isn’t so
reliant on the max speed and max force settings of the vehicles. Seek can
give some rather bizarre results at times — orderly formations can turn into
what looks like a swarm of bees attacking the formation leader!
112 | Chapter 3
The Steering Behaviors
Figure 3.14. Offset pursuit. The leader is shown in dark gray.

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