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

Offset pursuit is useful for all kinds of situations. Here are a few:
n
Marking an opponent in a sports simulation
n
Docking with a spaceship
n
Shadowing an aircraft
n
Implementing battle formations
The demo executable for offset pursuit shows three smaller vehicles
attempting to remain at offsets to the larger lead vehicle. The lead vehicle
is using arrive to follow the crosshair (click the left mouse button to posi
-
tion the crosshair).
Group Behaviors
Group behaviors are steering behaviors that take into consideration some
or all of the other vehicles in the game world. The flocking behavior I
described at the beginning of this chapter is a good example of a group
behavior. In fact, flocking is a combination of three group behaviors —
cohesion, separation, and alignment — all working together. We’ll take a
look at these specific behaviors in detail shortly, but first let me show you
how a group is defined.
To determine the steering force for a group behavior, a vehicle will con-
sider all other vehicles within a circular area of predefined size — known
as the neighborhood radius — centered on the vehicle. Figure 3.15 should
help clarify. The white vehicle is the steering agent and the gray circle
shows the extent of its neighborhood. Consequently, all the vehicles shown
in black are considered to be its neighbors and the vehicles shown in gray
are not.
Before a steering force can be calculated, a vehicle’s neighbors must be
determined and either stored in a container or tagged ready for processing.
In the demo code for this chapter, the neighboring vehicles are tagged
How to Create Autonomously Moving Game Agents | 113
Group Behaviors
Figure 3.15. The neighborhood radius
using the BaseGameEntity::Tag method. This is done by the TagNeighbors
function template. Here’s the code:
template <class T, class conT>
void TagNeighbors(const T* entity, conT& ContainerOfEntities, double radius)
{
//iterate through all entities checking for range
for (typename conT::iterator curEntity = ContainerOfEntities.begin();
curEntity != ContainerOfEntities.end();
++curEntity)
{
//first clear any current tag
(*curEntity)->UnTag();
Vector2D to = (*curEntity)->Pos() - entity->Pos();
//the bounding radius of the other is taken into account by adding it
//to the range
double range = radius + (*curEntity)->BRadius();
//if entity within range, tag for further consideration. (working in
//distance-squared space to avoid sqrts)
if ( ((*curEntity) != entity) && (to.LengthSq() < range*range))
{
(*curEntity)->Tag();
}
}//next entity
}
Most of the group behaviors utilize a similar neighborhood radius, so we
can save a little time by calling this method only once prior to a call to any
of the group behaviors.
if (On(separation) || On(alignment) || On(cohesion))
{
TagNeighbors(m_pVehicle, m_pVehicle->World()->Agents(), ViewDistance);
}
z
TIP You can pep up the realism slightly for group behaviors by adding a
field-of-view constraint to your agent. For example you can restrict the vehicles
included in the neighboring region by only tagging those that are within, say,
270 degrees of the heading of the steering agent. You can implement this easily
by testing against the dot product of the steering agent’s heading and the vector
to the potential neighbor.
It’s even possible to adjust an agent’s FOV dynamically and make it into a
feature of the AI. For example, in a war game a soldier’s FOV may be detrimen
-
tally affected by its fatigue, thereby affecting its ability to perceive its
environment. I don’t think this idea has been used in a commercial game but
it’s certainly food for thought.
Now that you know how a group is defined let’s take a look at some of the
behaviors that operate on them.
114 | 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