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

signals (vocal commands, spurs, reins), the cowboy steers his horse
toward the target. In general terms, these signals express concepts like:
go faster, go slower, turn right, turn left, and so on. The horse imple
-
ments the locomotion level. Taking the cowboy’s control signals as
input, the horse moves in the indicated direction. This motion is the
result of a complex interaction of the horse’s visual perception, its sense
of balance, and its muscles applying torques to the joints of its skeleton.
From an engineering point of view, legged locomotion is a very hard
problem, but neither the cowboy nor the horse give it a second thought.
Not all is rosy and sweet in the world of autonomous agents though. The
implementation of steering behaviors can beset the programmer with a
truckload of new problems to deal with. Some behaviors may involve
heavy manual tweaking, while others have to be carefully coded to avoid
using large portions of CPU time. When combining behaviors, care usually
must be taken to avoid the possibility that two or more of them may cancel
each other out. There are means and ways around most of these problems
though (well, all except for the tweaking — but that’s fun anyway), and
most often the benefits of steering behaviors far outweigh any
disadvantages.
The Vehicle Model
Before I discuss each individual steering behavior I’m going to spend a lit-
tle time explaining the code and class design for the vehicle model (the
locomotion).
MovingEntity is a base class from which all moving game
agents are derived. It encapsulates the data that describes a basic vehicle
with point mass. Let me run you through the class declaration:
class MovingEntity : public BaseGameEntity
{
protected:
The MovingEntity class is derived from the BaseGameEntity class, which
defines an entity with an ID, a type, a position, a bounding radius, and a
scale. All game entities from here onward in this book will be derived from
BaseGameEntity.ABaseGameEntity also has an additional Boolean member
variable,
m_bTag, which will be utilized in a variety of ways, some of which
will be described very shortly. I’m not going to list the class declaration
here, but I recommend you take a quick look at the BaseGameEntity.h
header sometime during your read through this chapter.
SVector2D m_vVelocity;
//a normalized vector pointing in the direction the entity is heading.
SVector2D m_vHeading;
//a vector perpendicular to the heading vector
SVector2D m_vSide;
How to Create Autonomously Moving Game Agents
| 87
The Vehicle Model
The heading and side vectors define a local coordinate system for the mov
-
ing entity. In the examples given in this chapter, a vehicle’s heading will
always be aligned with its velocity (for example, a train has a velocity
aligned heading). These values will be used often by the steering behavior
algorithms and are updated every frame.
double m_dMass;
//the maximum speed at which this entity may travel.
double m_dMaxSpeed;
//the maximum force this entity can produce to power itself
//(think rockets and thrust)
double m_dMaxForce;
//the maximum rate (radians per second) at which this vehicle can rotate
double m_dMaxTurnRate;
public:
/* EXTRANEOUS DETAIL OMITTED */
};
Although this is enough data to represent a moving object, we still need a
way of giving a moving entity access to the various types of steering
behaviors. I have chosen to create a class,
Vehicle, which inherits from
MovingEntity and owns an instance of the steering behavior class,
SteeringBehaviors. SteeringBehaviors encapsulates all the different steer-
ing behaviors I’ll be discussing throughout this chapter. More on that in a
moment though; first, let’s take a look at the
Vehicle class declaration.
class Vehicle : public MovingEntity
{
private:
//a pointer to the world data enabling a vehicle to access any obstacle
//path, wall, or agent data
GameWorld* m_pWorld;
The GameWorld class contains all the data and objects pertinent to the envi
-
ronment the agents are situated in, such as walls, obstacles, and so on. I
won’t list the declaration here to save space, but it might be a good idea to
check out GameWorld.h in your IDE at some point to get a feel for it.
//the steering behavior class
SteeringBehaviors* m_pSteering;
A vehicle has access to all available steering behaviors through its own
instance of the steering behavior class.
public:
//updates the vehicle’s position and orientation
void Update(double time_elapsed);
88 | Chapter 3
The Vehicle Model

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