The demo drives itself
/race is a presentation, not a game — so it starts on autopilot and drives itself. The controller is deliberately small. This post is what each part is doing and what a real one adds.
Steering is an error signal
const desired = Math.atan2(aim.x - pos.x, aim.y - pos.y);
let err = desired - heading; // normalize to [-π, π]
steer = clamp(err * 2.5, -1, 1); // k_p · e
That's the whole steering law: a P-controller. err is the angle between
the car's nose and the aim point; multiply by a gain, clamp to full lock.
The while loops handle wraparound so a target 10° right doesn't spin the
car 350° left.
What it doesn't have is the D term — a derivative to damp the approach —
which is why it can wobble slightly into tight corners. The fix is one more
line and a tuning session; for a demo it reads as characterful rather than
broken, and it's honest about being P not PD.
The aim point is the racing line
The car doesn't chase gates — it chases the road. The aim is a centerline sample a look-ahead distance away, and the distance scales with speed:
lookI = nearest.i + (|speed| · 0.55) / segLen;
aim = track.pts[lookI].p;
Slow → aim nearby, tight control. Fast → aim far ahead, which cuts a smooth line through corners the way a human driver looks through a turn rather than at the cone in front of the bumper.
Braking for the corner you can see
Speed control is a min-scan over the look-ahead window:
vTarget = min over next window of sqrt(GRIP · radius)
Find the tightest upcoming radius, compute its corner speed with the same
v_max formula the crash rule enforces, and brake to meet it — full throttle
when below target, brake when above, cruise at the boundary. One number
(vTarget) is the entire traction policy.
The result is genuinely instructive to watch: the car visibly lifts before
the hairpin, brakes harder the faster it arrives, and opens up on the
straights. The autopilot respects the same physics sheet the site prints —
which is the point. The controller isn't a gimmick layered on the product;
controller · core components is the product.