An enemy is supposed to run to the player. The first attempt is always the same: work out the direction to the target, scale it to length 1, multiply by a speed, add it to the position. That looks like a train on rails. The enemy turns on the spot, never brakes, and once it has reached its target it twitches back and forth in front of it. In 1999 Craig Reynolds showed at the GDC how characters can move like vehicles instead, with momentum and curves. That is the starting point for this demo.
The green arrow is the agent's velocity, the dashed dark line is the velocity the behavior wants right now, and the orange arrow is the difference between the two. Chips 1 to 5 switch between seek, flee, arrive, wander and path, the mouse or a click sets the target, R resets. Next to it runs a second, orange agent with the same values, but only 30 steps per second instead of 120.
One rule for everything
Every behavior answers only one question: which velocity do I want right now? Everything else is the same for all of them. The steering is the velocity that is missing:
Vec2 steer = desired - a.vel; if (limitForce) steer = truncate(steer, p.maxForce * dt); a.vel = truncate(a.vel + steer, p.maxSpeed); a.pos = wrap(a.pos + a.vel * dt, p);
A vehicle cannot make up all of it at once, so the amount per step is capped, that is the force limit. The velocity itself has an upper limit too. The curves come out of these four lines: if you drive fast to the right and suddenly want to go left, you need several steps to turn around and drive an arc while you do.
Seek circles
Seek always wants to go to the target at full speed:
inline Vec2 seekDesired(Vec2 pos, Vec2 target, double maxSpeed) { return normalize(target - pos) * maxSpeed; }
You can see the problem in the demo after a few seconds. The agent reaches the target but still wants to go at it at 200, shoots straight through and has to turn around on the other side first. In the test (20 seconds of simulation, the first four left out) it keeps swinging out about 100 pixels from the target and comes back at nearly full speed. It never comes to rest, because seek only knows the direction and has no idea there is a target to stop at.
Arrive brakes
Arrive is seek with a braking radius. Outside it, full speed, inside it the wanted speed falls linearly with the distance and is zero on the target:
const double speed = dist >= slowRadius ? maxSpeed : maxSpeed * (dist / slowRadius); return toTarget * (speed / dist);
Because the agent wants less and less, less and less is missing, and the steering becomes small by itself. In the test it sits on the target after 14 seconds with a distance of 0.000 pixels and a speed of 0, at 30 as well as at 120 steps per second.
Flee and wander
Flee is the mirror image of seek, with a panic radius. Outside of it the agent wants zero and brakes, otherwise it would run from something far away forever. If it sits exactly on the target there is no "away", so it keeps its heading.
Wander needs a target that isn't there. Reynolds puts a circle just ahead of the agent and lets a point travel around it. The agent walks to that point:
const Vec2 h = normalize(heading);
const Vec2 center = pos + h * distance;
const Vec2 dir{h.x * std::cos(angle) - h.y * std::sin(angle), h.x * std::sin(angle) + h.y * std::cos(angle)};
return center + dir * radius;
The angle is a sum of three sine waves over time (wanderNoise). If every step rolled a die, an agent at 120 steps per second would wander more jittery than one at 30. With the angle a pure function of time, both walk the same curve. In the test they are 4.7 pixels apart after four seconds.
Following a path
A path here is a closed line of points with a corridor radius, the way Reynolds describes it: a center line and a radius. What gets checked is where the agent will be in 0.45 seconds:
const Vec2 predicted = pos + vel * pp.lookahead;
const PathHit hit = closestOnPath(path, predicted);
if (!hit.valid) return {0.0, 0.0};
if (hit.distance <= pp.radius && length(vel) > 1.0) return normalize(vel) * maxSpeed;
return seekDesired(pos, pointAhead(path, hit, pp.lead), maxSpeed);
If the prediction is inside the corridor, nothing happens and the agent keeps its direction. If it is outside, the agent aims at a point 45 pixels further along the path. Because the check looks at the future, the agent turns in before the corner instead of chasing after it. At speed 200 and force 200 the corners are still wide, in the test (a 400 by 300 rectangle, corridor radius 26) it strays at most 62 pixels from the line.
The dt
The force limit is a change of velocity per second, so a step of dt seconds may add maxForce * dt. Reynolds' pseudocode has no dt, it says velocity = truncate(velocity + acceleration, max_speed) and position = position + velocity. That is one step per frame. On a machine with twice the frame rate the agent drives twice as fast and steers twice as hard. It is the same trap as in Delta time and the fixed timestep.
So the demo runs two agents from the same state, A at 120 steps per second, B at 30. With the "dt scaling" chip on they stay together. Off, the step always goes into the calculation as 1/60 second, no matter how often it runs:
applySteering(a, desired, p, scaleByDt ? 1.0 / hz : 1.0 / kReferenceHz, limitForce);
Then A races off at double speed and B crawls at half. In the test, after two seconds of seek the distance is 3.5 pixels with dt and 285.6 pixels without. At 60 steps per second, the rate the version without dt is tuned for, the result is exactly the same, which is why the mistake never shows on the developer's machine.
What can go wrong
Switch the force limit off (chip "force limit", key F) and you see it at once: the velocity jumps to the wish, the agent turns on a dime. With seek it reaches the target and flickers around it at full speed, in the test it stays within a few pixels and never brakes.
normalize has a guard against the zero vector, otherwise the agent divides by zero on the target and is NaN from then on. The heading (heading) is only updated when the velocity is not zero, otherwise a standing agent would have no direction left. The world has no wall: whoever leaves the field comes back in on the other side.
If you want to send many agents across a map instead of chasing one target, the flow fields post has a different approach.
The code is in the repo under steering-behaviors. The project Game Dev Snippets collects all demos of the series.
The idea comes from Craig Reynolds' paper Steering Behaviors For Autonomous Characters, Daniel Shiffman explains it step by step in The Nature of Code. The code here is my own.