Chapter 3
How to Create Autonomously
Moving Game Agents
uring the late ’80s I remember watching a BBC Horizon documen
tary about state-of-the-art computer graphics and animation. There
was lots of exciting stuff covered in that program, but the thing I remember
most vividly was an amazing demonstration of the flocking behavior of
birds. It was based on very simple rules, yet it looked so spontaneous and
natural and was mesmerizing to watch. The programmer who designed the
behavior is named Craig Reynolds. He called the flocking birds “boids,”
and the simple rules the flocking behavior emerged from he called “steer-
ing behaviors.”
Since that time Reynolds has published a number of articles on various
types of steering behaviors, all of them fascinating. Most, if not all, of his
steering behaviors have direct relevance to games, which is why I’m going
to spend a considerable amount of time describing them and showing you
how to code and use them.
What Is an Autonomous Agent?
I’ve seen many definitions for what an autonomous agent is, but probably
the best is this:
An autonomous agent is a system situated within and a part of an envi
ronment that senses that environment and acts on it, over time, in pur
suit of its own agenda and so as to effect what it senses in the future.
Throughout this chapter I will use the term “autonomous agent” in refer
ence to agents that possess a degree of autonomous movement.Ifan
autonomous agent stumbles upon an unexpected situation, like finding a
wall in its way, it will have the ability to respond and adjust its motion
accordingly. For example, you might design one autonomous agent to
behave like a rabbit and one like a fox. If while munching happily on the
fresh dewy grass, the rabbit happens to spot the fox, it will autonomously
attempt to evade it. At the same time the fox will autonomously pursue the
rabbit. Both these events occur without any further intervention from the
programmer; once up and running, autonomous agents simply look after
This is not to say that an autonomous agent should be able to cope with
absolutely any situation at all (although that might be one of your goals),
but it is often very useful to be able to bestow an amount of autonomy. For
example, a common problem when writing pathfinding code is how to deal
with dynamic obstacles. Dynamic obstacles are those objects in your game
world that move around or change position, like other agents, sliding doors,
and so forth. Given a suitable environment, incorporating the correct steer
ing behavior into a game character will preclude writing special path
finding code to handle dynamic obstacles — an autonomous agent will
have the ability to deal with them if and when it has to.
The movement of an autonomous agent can be broken down into three
Action Selection: This is the part of the agent’s behavior responsible
for choosing its goals and deciding what plan to follow. It is the part
that says “go here” and “do A, B, and then C.”
Steering: This layer is responsible for calculating the desired trajec-
tories required to satisfy the goals and plans set by the action selec-
tion layer. Steering behaviors are the implementation of this layer.
They produce a steering force that describes where an agent should
move and how fast it should travel to get there.
Locomotion: The bottom layer, locomotion, represents the more
mechanical aspects of an agent’s movement. It is the how of travel-
ing from A to B. For example, if you had implemented the mechan-
ics of a camel, a tank, and a goldfish and then gave a command for
them to travel north, they would all use different mechanical pro
cesses to create motion even though their intent (to move north) is
identical. By separating this layer from the steering layer, it’s possi
ble to utilize, with little modification, the same steering behaviors
for completely different types of locomotion.
Reynolds makes use of an excellent analogy to describe the roles of each of
these layers in his paper “Steering Behaviors for Autonomous Characters.”
Consider, for example, some cowboys tending a herd of cattle out on
the range. A cow wanders away from the herd. The trail boss tells a
cowboy to fetch the stray. The cowboy says ‘giddy-up’ to his horse and
guides it to the cow, possibly avoiding obstacles along the way. In this
example, the trail boss represents action selection: noticing that the
state of the world has changed (a cow left the herd) and setting a goal
(retrieve the stray). The steering level is represented by the cowboy, who
decomposes the goal into a series of simple sub-goals (approach the
cow, avoid obstacles, retrieve the cow). A sub-goal corresponds to a
steering behavior for the cowboy-and-horse team. Using various control
86 | Chapter 3
What Is an Autonomous Agent?

Get Programming Game AI by Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.