✚ Game development

Steering behaviors: seeking, fleeing and moving as a flock

How steering behaviors work for autonomous movement in games: seek, flee, arrive, wander, obstacle avoidance and flocks of boids built from simple rules.

Pathfinding tells a character where to go; steering behaviors tell it how to move moment by moment. They are a set of simple rules, each producing a steering force. Combined, they yield smooth, believable movement: a homing missile, a fleeing fish, a flock of birds moving as one. The ideas were popularised by Craig Reynolds, who presented the boids model for simulating flocks back in 1987.

The basic model

Each agent has a position, a velocity and two limits: a maximum speed and a maximum steering force. On each update:

force = sum of active behaviours
force = truncate(force, max_force)
velocity = truncate(velocity + force * dt, max_speed)
position += velocity * dt

The capped force prevents instant changes of direction: the agent turns naturally, with an inertia that depends on how low the limit is. A lorry would have a low maximum force, a fly a very high one.

Seek and flee

Seek is the fundamental behaviour. You compute the desired velocity, pointing at the target at full speed, and the force is the difference from the current velocity:

desired = normalise(target - position) * max_speed
force = desired - velocity

The subtraction is the heart of the method: the agent does not aim directly at the target but gradually corrects its trajectory. Flee is identical with the direction reversed, and is often active only within a certain radius of the danger.

Arrive

With seek the agent reaches the target at full speed, overshoots it and comes back, oscillating. Arrive introduces a slowing radius:

distance = length(target - position)
if distance < slowing_radius:
    wanted_speed = max_speed * distance / slowing_radius
else:
    wanted_speed = max_speed
desired = normalise(target - position) * wanted_speed
force = desired - velocity

This is the right behaviour for a character following the waypoints of an A* path that must stop precisely at the last one.

Pursuit and evade

Seeking the current position of a moving target is inefficient: you always arrive where the target was. Pursuit estimates the future position:

time = distance / max_speed
predicted = target.position + target.velocity * time
force = seek(predicted)

Evade applies the same prediction to flee. For a homing missile or a predator, the difference compared with plain seek is obvious.

Wander

An animal roaming aimlessly should not change direction randomly every frame: the result would be jitter. Wander projects a circle in front of the agent and slowly moves a point around its circumference, adding a small random value to the angle each time. The agent seeks that point. The result is winding, unpredictable but continuous movement.

Avoiding obstacles

To avoid bumping into obstacles, the agent projects a «feeler» ahead of itself, for example a segment whose length is proportional to speed. If the feeler intersects an obstacle, a lateral force is generated that pushes away from its surface. The closer the obstacle, the stronger the push.

Flocks: the boids

The boids model shows how complex behaviour emerges from local rules. Each agent considers only neighbours within a certain radius and applies three forces:

Rule Description Effect
Separation Moves away from neighbours that are too close Prevents crowding
Alignment Steers its velocity towards the neighbours' average The flock heads the same way
Cohesion Heads towards the neighbours' centre of mass The group stays together

No agent knows the shape of the flock, yet the group moves as a unit, splits around obstacles and regroups. Varying the weights of the three rules takes you from tight schools of fish to scattered swarms.

Because each agent must find its neighbours, with many agents you need a spatial grid to avoid comparing everyone with everyone.

Combining behaviours

In practice an agent uses several behaviours together. Two common approaches:

  • weighted sum: each force is multiplied by a weight and added. Simple, but opposing forces can cancel out, leaving the agent stuck in front of an obstacle.
  • priority: forces are computed in order of importance, for example obstacle avoidance first, then separation, then seek, and added until the available maximum force is used up.

The priority approach ensures that safety behaviours always take precedence.

Where to use them

Steering behaviors are ideal for crowds, animals, vehicles, guided projectiles, flying enemies and companions accompanying the player. They do not replace pathfinding in a maze, but complement it: A* supplies the waypoints, while arrive and obstacle avoidance make movement between them natural.

In short

  • Steering behaviors produce capped steering forces that make movement smooth.
  • Seek, flee and arrive are the foundation; pursuit and evade add prediction.
  • Wander creates continuous roaming, obstacle avoidance uses forward feelers.
  • Separation, alignment and cohesion are enough to create believable flocks.
  • Forces are combined by weighted sum or, more robustly, by priority.
MM
Marco Marzaduri

Marco Marzaduri works in cybersecurity, digital forensics and web development. A computer science graduate, he combines rigorous technical analysis with attention to the people who use technology.

Read also