Pursuit, Evade & Wander
Seek and flee react to where a target is. Against something that moves, that looks dumb: the chaser runs to where the player was half a second ago and ends up trailing behind in a curve. Pursuit and evade aim at where the target will be. Wander is the opposite problem: no target at all, but motion that looks alive instead of jittery.
Pursuit: seek the predicted position
The trick is one line of prediction. Estimate how long it would take to reach the target, and seek the point the target will have moved to by then:
T = distance / chaser.MaxSpeed (look-ahead time, clamped to a maximum)
predicted = target.Pos + target.Vel * T
pursuit = Seek(predicted)
evade = Flee(predicted)The look-ahead grows with distance: far away, the chaser aims well ahead; close up, pursuit turns back into seek. Clamping T (here to 2 s) stops a distant chaser from aiming at a point the target will never reach because it will turn first.
The program races a seek chaser against a pursuit chaser. The target runs along the x axis at 2 units/s; both chasers start below it and are faster (3 units/s):
using System;
var seeker = new Vehicle(new V2(4, -6));
var pursuer = new Vehicle(new V2(4, -6));
var target = new V2(0, 0);
var targetVel = new V2(2, 0);
int? seekCatch = null, pursueCatch = null;
Console.WriteLine("step target seek pursuit");
for (int step = 0; step <= 100; step++)
{
if (step % 10 == 0) Console.WriteLine(quot;{step,4} {target,-13} {seeker.Pos,-13} {pursuer.Pos}");
if (seekCatch == null && (target - seeker.Pos).Len < 0.5) seekCatch = step;
if (pursueCatch == null && (target - pursuer.Pos).Len < 0.5) pursueCatch = step;
seeker.Apply(Seek(seeker, target), 0.1);
pursuer.Apply(Pursue(pursuer, target, targetVel, maxLookAhead: 2), 0.1);
target += targetVel * 0.1;
}
Console.WriteLine(quot;seek caught the target at step {seekCatch?.ToString() ?? "never"}");
Console.WriteLine(quot;pursuit caught the target at step {pursueCatch?.ToString() ?? "never"}");
static V2 Seek(Vehicle v, V2 point) => (point - v.Pos).Norm() * v.MaxSpeed - v.Vel;
static V2 Pursue(Vehicle v, V2 targetPos, V2 targetVel, double maxLookAhead)
{
double t = Math.Min((targetPos - v.Pos).Len / v.MaxSpeed, maxLookAhead);
return Seek(v, targetPos + targetVel * t);
}
class Vehicle(V2 pos)
{
public V2 Pos = pos, Vel;
public double MaxSpeed = 3, MaxForce = 6;
public void Apply(V2 force, double dt)
{
V2 accel = (force * 4).Clamp(MaxForce);
Vel = (Vel + accel * dt).Clamp(MaxSpeed);
Pos += Vel * dt;
}
}
readonly record struct V2(double X, double Y)
{
public static V2 operator +(V2 a, V2 b) => new(a.X + b.X, a.Y + b.Y);
public static V2 operator -(V2 a, V2 b) => new(a.X - b.X, a.Y - b.Y);
public static V2 operator *(V2 a, double k) => new(a.X * k, a.Y * k);
public double Len => Math.Sqrt(X * X + Y * Y);
public V2 Norm() => Len > 1e-9 ? this * (1 / Len) : new V2(0, 0);
public V2 Clamp(double max) => Len > max ? Norm() * max : this;
static string F(double v) => (Math.Round(v, 2) + 0.0).ToString("F2");
public override string ToString() => quot;({F(X)}, {F(Y)})";
}step target seek pursuit
0 (0.00, 0.00) (4.00, -6.00) (4.00, -6.00)
10 (2.00, 0.00) (2.98, -3.96) (4.29, -3.74)
20 (4.00, 0.00) (2.99, -1.08) (4.70, -0.77)
30 (6.00, 0.00) (5.49, 0.06) (5.17, 0.73)
40 (8.00, 0.00) (7.90, -0.02) (7.88, -0.04)
50 (10.00, 0.00) (9.93, 0.05) (10.00, 0.01)
60 (12.00, 0.00) (11.91, -0.02) (12.04, 0.03)
70 (14.00, 0.00) (14.01, 0.00) (14.00, 0.01)
80 (16.00, 0.00) (16.02, 0.01) (16.01, 0.04)
90 (18.00, 0.00) (18.05, -0.02) (17.99, -0.03)
100 (20.00, 0.00) (19.98, 0.02) (19.99, 0.04)
seek caught the target at step 31
pursuit caught the target at step 22
The pursuer cuts the corner and intercepts; the seeker curves in behind the target and has to win a straight footrace. For enemies this matters a lot: pursuit feels smart and threatening, seek feels like being followed by a dog on a leash.
Note: Perfect prediction is not always what you want. A guard that always intercepts perfectly feels like it is cheating. Lower the look-ahead for slow, clumsy enemies, raise it for hunters. See Field Notes: Design.
Evade
Evade is flee from the predicted position, and it's how prey, fleeing civilians and "kiting" ranged enemies dodge. Two practical additions:
- Panic distance: only evade when the threat is within some radius; otherwise return zero force and let other behaviors (wander, go home) run.
- Clamp to the play area: fleeing in a straight line runs into walls. Combine evade with wall avoidance, or flee along the navmesh toward a safe point instead.
Wander
A character that should "mill about" needs small, smooth changes of heading. Picking a new direction every frame produces jitter. Reynolds' wander keeps a point on a circle projected in front of the vehicle and nudges that point a little each step:
wander circle
.---.
/ x \ x = wander target, moved a bit along the circle each step
>-------( o ) o = circle centre, "distance" ahead of the vehicle
\ /
'---'Because the target can only drift along the circle, the heading changes smoothly, and the circle's distance and radius control how twitchy the motion is. The randomness must come from a seeded generator so tests, replays and saved games behave the same:
using System;
var v = new Vehicle(new V2(0, 0)) { Vel = new V2(1, 0) };
var w = new Wander(seed: 2024);
for (int step = 0; step <= 60; step++)
{
if (step % 6 == 0)
{
double heading = Math.Atan2(v.Vel.Y, v.Vel.X) * 180 / Math.PI;
Console.WriteLine(quot;step {step,2}: pos {v.Pos,-14} heading {heading,7:F1} deg");
}
v.Apply(w.Force(v), 0.1);
}
class Wander(uint seed)
{
const double Distance = 2.0, Radius = 1.0, Jitter = 0.6;
double angle; // where the wander target sits on the circle
double Next() // tiny LCG: uniform in [-1, 1)
{
seed = seed * 1664525u + 1013904223u;
return (seed >> 8) / 8388608.0 - 1.0;
}
public V2 Force(Vehicle v)
{
angle += Next() * Jitter;
V2 heading = v.Vel.Norm();
V2 centre = v.Pos + heading * Distance;
double baseAngle = Math.Atan2(heading.Y, heading.X);
V2 onCircle = new(Math.Cos(baseAngle + angle) * Radius, Math.Sin(baseAngle + angle) * Radius);
return (centre + onCircle - v.Pos).Norm() * v.MaxSpeed * 0.5 - v.Vel; // stroll at half speed
}
}
class Vehicle(V2 pos)
{
public V2 Pos = pos, Vel;
public double MaxSpeed = 3, MaxForce = 6;
public void Apply(V2 force, double dt)
{
V2 accel = (force * 4).Clamp(MaxForce);
Vel = (Vel + accel * dt).Clamp(MaxSpeed);
Pos += Vel * dt;
}
}
readonly record struct V2(double X, double Y)
{
public static V2 operator +(V2 a, V2 b) => new(a.X + b.X, a.Y + b.Y);
public static V2 operator -(V2 a, V2 b) => new(a.X - b.X, a.Y - b.Y);
public static V2 operator *(V2 a, double k) => new(a.X * k, a.Y * k);
public double Len => Math.Sqrt(X * X + Y * Y);
public V2 Norm() => Len > 1e-9 ? this * (1 / Len) : new V2(0, 0);
public V2 Clamp(double max) => Len > max ? Norm() * max : this;
static string F(double v) => (Math.Round(v, 2) + 0.0).ToString("F2");
public override string ToString() => quot;({F(X)}, {F(Y)})";
}step 0: pos (0.00, 0.00) heading 0.0 deg
step 6: pos (0.82, -0.07) heading -1.1 deg
step 12: pos (1.58, 0.31) heading 48.1 deg
step 18: pos (1.67, 1.12) heading 108.7 deg
step 24: pos (1.01, 1.58) heading 173.5 deg
step 30: pos (0.32, 1.15) heading -125.5 deg
step 36: pos (0.36, 0.35) heading -59.9 deg
step 42: pos (1.09, -0.02) heading -2.6 deg
step 48: pos (1.88, 0.32) heading 36.8 deg
step 54: pos (2.64, 0.78) heading 28.7 deg
step 60: pos (3.32, 1.35) heading 46.8 deg
The heading never jumps: it turns gradually, sweeps through one lazy loop between steps 6 and 42, then straightens out again. Lower Jitter (or move the circle further ahead) and the loops disappear. Change the seed and you get a different but equally smooth stroll; keep it and the stroll repeats exactly.
Tuning table
| Behavior | Knob | Small value | Large value |
|---|---|---|---|
| Pursuit | max look-ahead | behaves like seek, trails behind | aims far ahead, can be fooled by a sudden turn |
| Evade | panic distance | lets threats get very close | runs from everything on screen |
| Wander | jitter | long, lazy curves | nervous zig-zag |
| Wander | circle distance | sharp turns possible | gentle, mostly-forward motion |
Test yourself
What point does pursuit seek?
- The target's current position
- The target's position plus its velocity times a look-ahead time
- The midpoint between chaser and target
- The target's starting position
Why does wander move a point on a circle in front of the vehicle instead of picking a new direction each frame?
- It is cheaper to compute
- The target can only drift a little along the circle, so the heading changes smoothly
- It makes the vehicle faster
- Engines require it
Why should wander use a seeded generator in a game?
- Seeded generators are faster
- So replays, tests and saves reproduce the same motion
- Unseeded generators can't produce negative numbers
- To make the motion more varied