## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

If you have a computer at hand I recommend you check out the demo for
this behavior. The green circle is the constraining “wander circle” and the
red dot the target. The demo allows you to adjust the size of the wander cir
-
cle, the amount of jitter, and the wander distance so you can observe the
effect they have on the behavior. Notice the relationship between the wan
-
der distance and the variation in angle of the steering force returned from
the method. When the wander circle is far away from the vehicle, the
method produces small variations in angle, thereby limiting the vehicle to
small turns. As the circle is moved closer to the vehicle, the amount it can
turn becomes less and less restricted.
z
3D TIP If you require your agents to wander in three dimensions (like a space
-
ship patrolling its territory), all you have to do is constrain the wander target to
a sphere instead of a circle.
Obstacle Avoidance
Obstacle avoidance is a behavior that steers a vehicle to avoid obstacles
lying in its path. An obstacle is any object that can be approximated by a
circle (or sphere, if you are working in 3D). This is achieved by steering
the vehicle so as to keep a rectangular area — a detection box, extending
forward from the vehicle — free of collisions. The detection box’s width is
equal to the bounding radius of the vehicle, and its length is proportional to
the vehicle’s current speed — the faster it goes, the longer the detection
box.
I think before I describe this process any further it would be a good idea
to show you a diagram. Figure 3.6 shows a vehicle, some obstacles, and the
detection box used in the calculations.
How to Create Autonomously Moving Game Agents | 99
The Steering Behaviors
Figure 3.6. Setup for the obstacle avoidance steering behavior
Finding the Closest Intersection Point
The process of checking for intersections with obstacles is quite compli
-
cated, so let’s take this step by step.
A) The vehicle should only consider those obstacles within range of its
detection box. Initially, the obstacle avoidance algorithm iterates
through all the obstacles in the game world and tags those that are
within this range for further consideration.
B) The algorithm then transforms all the tagged obstacles into the vehi
-
cle’s local space (for an explanation of local space, see Chapter 1).
This makes life much easier as after transformation any objects with a
negative local x-coordinate can be dismissed.
C) The algorithm now has to check to see if any obstacles overlap the
detection box. Local coordinates are useful here as all you need to do
is expand the bounding radius of an obstacle by half the width of the
detection box (the vehicle’s bounding radius) and then check to see if
its local y value is smaller than this value. If it isn’t, then it won’t
intersect the detection box and can subsequently be discarded from
further consideration.
Figure 3.7 should help clear up these first three steps for you. The letters on
the obstacles in the diagram correspond to the descriptions.
D) At this point there are only those obstacles remaining that intersect the
detection box. It’s now necessary to find the intersection point closest
to the vehicle. Once more, local space comes to the rescue. Step C
expanded an object’s bounding radius. Using this, a simple line/circle
intersection test can be used to find where the expanded circle inter
-
sects the x-axis. There will be two intersection points, as shown in
Figure 3.8. (We don’t have to worry about the case where there is one
100 | Chapter 3
The Steering Behaviors
Figure 3.7. Steps A, B, and C

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required