## 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

position of the vehicle has exceeded the screen boundaries. If so, the posi
-
tion is wrapped around accordingly.
That’s the boring stuff out of the way — let’s move on and have some
fun!
The Steering Behaviors
I’m now going to describe each steering behavior individually. Once I’ve
covered all of them I’ll explain the
SteeringBehaviors class that encapsu
-
lates them and show you the different methods available for combining
them. Toward the end of the chapter I’ll demonstrate a few tips and tricks
for getting the most out of steering behaviors.
Seek
The seek steering behavior returns a force that directs an agent toward a
target position. It is very simple to program. The code looks like this (note
that
m_pVehicle points to the Vehicle that owns the SteeringBehaviors
class):
Vector2D SteeringBehaviors::Seek(Vector2D TargetPos)
{
Vector2D DesiredVelocity = Vec2DNormalize(TargetPos - m_pVehicle->Pos())
* m_pVehicle->MaxSpeed();
return (DesiredVelocity - m_pVehicle->Velocity());
}
First the desired velocity is calculated. This is the velocity the agent would
need to reach the target position in an ideal world. It represents the vector
from the agent to the target, scaled to be the length of the maximum possi
-
ble speed of the agent.
The steering force returned by this method is the force required, which
when added to the agent’s current velocity vector gives the desired veloc
-
ity. To achieve this you simply subtract the agent’s current velocity from
the desired velocity. See Figure 3.2.
How to Create Autonomously Moving Game Agents | 91
The Steering Behaviors
Figure 3.2. Calcu
-
lating vectors for the
seek behavior. The
dotted vector shows
the steering force to
the current velocity
produces the desired
result.

## 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