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

//add any entities found within query radius to the neighbor list
std::list<entity>::iterator it = curCell->Members.begin();
for (it; it!=curCell->Members.end(); ++it)
{
if (Vec2DDistanceSq((*it)->Pos(), TargetPos) <
QueryRadius*QueryRadius)
{
*curNbor++ = *it;
}
}
}
}//next cell
//mark the end of the list with a zero.
*curNbor = 0;
}
You can find the full implementation of this class in Common/misc/
CellSpacePartition.h. I have added cell space partitioning to the demo
Big_Shoal.exe. It’s now called Another_Big_Shoal.exe. You can toggle the
partitioning on and off and see the difference it makes to the frame rate.
There is also an option to view how the space is divided (default is7x7
cells) and to see the query box and neighborhood radius of one of the
agents.
z
TIP When applying the steering force to some vehicle types it can be useful to
resolve the steering vector into forward and side components. For a car, for
example, this would be analogous to creating the throttle and steering forces,
respectively. To this end, you will find the methods
ForwardComponent and
SideComponent in the 2D SteeringBehaviors class used in this chapter’s
accompanying project file.
Smoothing
When playing with the demos, you may have noticed that sometimes a
vehicle can twitch or jitter slightly when it finds itself in a situation with
conflicting responses from different behaviors. For example, if you run one
of the Big Shoal demos and switch the obstacles and walls on, you will see
that sometimes when the “shark” agent approaches a wall or an obstacle,
its nose shudders or trembles a little. This is because in one update step the
obstacle avoidance behavior returns a steering force away from the obsta
-
cle but in the next update step there is no threat from the obstacle, so one of
the agent’s other active behaviors may return a steering force pulling its
heading back toward the obstruction, and so on, creating unwanted oscilla
-
tions in the vehicle’s heading. Figure 3.19 shows how these oscillations can
be started with just two conflicting behaviors: obstacle avoidance and
seek.
130 | Chapter 3
Smoothing
This shakiness is usually not too noticeable. Occasionally though, there
will be times when it will be preferable for the shaking not to occur. So
how do you stop it? Well, as the vehicle’s velocity is always aligned with
its heading, stopping the shaking is not trivial. To negotiate the scenario
given in Figure 3.19 successfully and smoothly, the vehicle needs to be
able to foresee the conflict ahead of time and change behavior accordingly.
Although this can be done, the solution can require a lot of calculation and
additional baggage. A simple alternative suggested by Robin Green of
Sony is to decouple the heading from the velocity vector and to average its
value over several update steps. While this solution isn’t perfect, it pro
-
duces adequate results at low cost (relative to any other solution I know
about). To facilitate this, another member variable is added to the
Vehicle
class: m_vSmoothedHeading. This vector records the average of a vehicle’s
heading vector and is updated each simulation step (in
Vehicle::Update),
using a call to an instance of a
Smoother — a class that samples a value
over a range and returns the average. This is what the call looks like:
if (SmoothingIsOn())
{
m_vSmoothedHeading = m_pHeadingSmoother->Update(Heading());
}
This smoothed heading vector is used by the world transform function in
the render call to transform a vehicle’s vertices to the screen. The number
of update steps the
Smoother uses to calculate the average is set in
How to Create Autonomously Moving Game Agents | 131
Smoothing
Figure 3.19. Conflicting behaviors can produce “judder.”

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