A top-down stage rally against the clock. You cannot see far ahead, so a co-driver reads the road for you: every corner arrives first as a pace note ("Right 4 tightens, don't cut, into"), drawn as a tulip card in a road book at the side of the screen and called over the radio before you get there. Four regions (Pinewood gravel, Saltcliff tarmac between stone walls, Highfrost snow and ice, Red Mesa sand and jumps), each a rally of four stages with a night stage at the end, a service park between stages, three cars and nine upgrades. Every run is a ghost, and a finished stage can be sent to a friend as a link.
How it's built
- Rules apart from drawing.
core/ is plain TypeScript with no Phaser import: the car, the stage generator, the pace notes, the AI driver, the simulation, damage and service, the economy, ghosts and saves. It is covered by Vitest tests, including a test that replays a ghost from a decoded link and checks that it ends in exactly the same state, and a balance test. - A real vehicle model. A two-axle (bicycle) model stepped at 120 Hz inside a 60 Hz tick: slip angles per axle, a tyre curve that rises to a peak and keeps a share of its grip in a slide, a friction circle (braking or power takes grip away from cornering), weight transfer front to back, a torque curve with five automatic gears and reverse, a handbrake that locks the rear axle, and steering lock that fades with speed. Each wheel samples its own surface, so two wheels on the verge pull the car. Crests make the car light; where the ground falls away faster than gravity the car takes off, and a hard landing damages the suspension.
- Driving assists (simulation version 2). The physics helps the player without taking the car away: the front wheels never turn much past the slip angle of peak grip (more lock only ploughs on), and with the wheel let go in a slide they follow the car's direction, a natural counter-steer; a stability assist damps the yaw the steering does not ask for once the rear is past its peak (off on the handbrake, so handbrake turns still work); ABS keeps the brakes below locking so the car steers under full braking; traction control trims power while the rear slides. Steering lock fades less with speed because of it. A car stuck against something or facing the wrong way off the road while the player is on the pedals goes back on the road after 2.5 s. A painted driving line (on by default, L or the pause page) runs 110 m ahead along the AI's racing line, white where the speed can be kept, amber to lift and red to brake, from the AI's speed plan at a careful skill. The rookie co-driver (a speed on each card) is on by default.
- Drifting. The stability assist leaves a drift window: it barely acts up to about 0.45 rad of body slip and comes in fully toward a spin, and traction control only cuts in past 0.75 rad, so a slide can be held on the throttle and the steering. Drift points build while the car slides on the road above 9 m/s (speed times slip, a multiplier from ×1 to ×4 that rises every 1.5 s of sliding); 0.6 s of straight driving banks the chain, a hit, a reset or leaving the road loses it. A rubber-stamped tag under the stopwatch shows the live chain, then BANKED or CHAIN LOST; the timing card shows the stage's drift total and best chain, and drifting pays style money (a credit per 25 points, at most 300 a stage, by region).
- Drift control (simulation version 3). In a slide the steering input asks for a rate of turn and the car follows it, so the drift angle is set with the steering; the handbrake with lock swings the tail at the rate the steering asks; on the throttle the rear holds its slide a little longer. Every car, the front-drive Mosswren too, can start a drift with the handbrake and hold it (tested). A banked chain buys a drift boost (up to 3 s of 40% more engine torque, flames from the exhaust), so drifting well also wins time. The timing card says how far the time was from the next medal and marks a new drift record per stage; the first runs show a tip about drifting.
- Surfaces. Tarmac, wet tarmac, gravel, dirt, sand, snow, ice, grass, snow banks and desert scrub each set the peak grip, how forgiving a slide is, the slip angle of the peak and the rolling drag. Three tyre upgrades help on their own surfaces.
- Deterministic on purpose. The simulation uses only + - * /, square roots and its own sine, cosine and arctangent (
core/trig.ts, Taylor series), because the browser's Math.sin is not required to return the same bits in every engine. Inputs are quantised (steering in 31 steps, pedals in 4) and stored one per tick, so a run can be replayed exactly from its inputs. - Stages from a seed. The generator chains pieces (straights, corners of six grades with left or right, hairpins, tightening and opening corners, chicanes) under per-region rules, turning the road back toward its general direction the further it has wandered. The centre line is integrated every 2 m from the curvature, and a spatial grid rejects any piece that would bring the road within 30 m of an earlier part of it; when it gets stuck it takes back the last two pieces. Crests, jumps (a ramp, a lip, a drop), fords and bridges go on the straights; trees, rocks on the inside of some corners, crowds with tape and straw bales at hairpins, stone walls, snow banks and marker poles go along the sides. The same seed builds the same stage, tested by hash.
- Pace notes from geometry. The notes are read back from the finished road, not from the generator's plan: runs of curvature with one sign become corners, the tightest radius gives the grade (hairpin, 1 to 6, or square), the change of curvature through the corner gives "tightens" or "opens", the angle and length give "long", the height profile gives crests and jumps, rocks near the inside give "don't cut", ice, water or sand under the corner add a warning, and the gap to the next note gives "into", "and" or a distance. The road book shows the card just called and the two after it; calls come earlier the faster you drive, and the timing can be moved in the pause menu.
- The co-driver's voice. By default a short burst of radio squelch and one synthesised formant "syllable" per word, pitched by the grade, so a call sounds like a call without depending on the device. The browser's own speech voice can be chosen instead where it exists, and cards alone are an option too.
- An AI driver. It plans a racing line that uses the road's width, a target speed per sample from the radius and the surface grip, and braking points from a backward pass, then steers by pure pursuit with a correction against slides and feeds the power in gently with a lot of lock. It produces quantised inputs exactly like a player. It sets the medal times (gold is the best of three runs at different risk levels in a reference car per region; silver and bronze are 7% and 16% slower), drives the title screen through all four regions, takes the card and poster shots, and plays the balance test.
- Damage and the service park. Hits are resolved as impulses against trees, rocks, walls, boards and spectators (bales, tape, bushes and snow banks only slow the car), and the impact speed damages the steering, suspension, engine, gearbox, body or lights depending on where the car was hit. Damage changes how the car drives (a pull to one side, less power, slow shifts, less grip) and how it looks. Between rally stages the crew have 30 minutes; each repair costs minutes by how bad the part is, and every minute over costs 10 seconds.
- Ghosts in a link. A ghost stores the stage (region, index, seed), the car, its upgrades and setup, the damage it started with and the inputs, as two run-length streams (steering as zigzag deltas, so a keyboard ramp is one run, and the pedal bits) of varints, encoded in URL-safe base64. A 70–90 s run is a few kilobytes. "Challenge a friend" copies a link with the ghost in the hash; opening it starts that stage in the same car against the ghost, on a static site with no server. Links from another version of the simulation, or damaged ones, open a designed "road book is torn" card instead.
- Drawing. Phaser 4 (WebGL), with mipmaps. The ground is baked with Canvas 2D into 32 m chunks around the camera at 20–32 pixels per metre (chosen from the screen, so the camera never stretches it): large-scale colour from value noise in world space, then repeating material tiles laid as patterns in world coordinates (forest floor with needles, moss and cones; coastal turf with clover, flowers and heather; wind-carved snow; cracked desert hardpan), the road as ragged polygons (verge, ditch, shoulder, surface) filled with surface tiles (thousands of gravel stones, dirt, sand ripples, packed snow, ice, tarmac aggregate), wheel paths and loose gravel windrows, worn lines, cracks and patches on tarmac, puddles, grass creeping over the edge, surface changes faded over 8 m, water, start and finish, litter under the trees, undergrowth (ferns, stumps, logs, toadstools, gorse, sheep, snowy saplings, dry bushes, pebbles), soft shadows and the low props, every detail placed from a hash of its world cell or sample index so chunks agree at their edges. Trees (pines built from lit boughs over a round crown, broadleaf crowns of lit lumps with leaf speckle, snow on the sunlit boughs), cacti, buttes, spectators and boards are sprites above the car, painted at 48 pixels per metre. The cars are painted at 64 pixels per metre from code, nose up: shaded bodywork, panel gaps, glass with wipers, a windscreen sun strip, sponsor decals, chrome headlights, and every upgrade level changes the drawing (bonnet vents and a scoop, a rear wing, arch flares, mud flaps, brake ducts, a light pod with two to six lamps, a roof vent); body damage adds scratches, dents, a cracked windscreen and a hanging bumper. Mud builds up on the car through loose stages. Skid marks are a pool of stretched sprites with tread. Dust stays under the car and drifts downwind, a light trail at speed and a heavier plume only in a slide; tarmac gives tyre smoke instead; gravel, snow, spray, straw, leaves, sparks and exhaust flames are their own emitters. The body rolls out of corners. Cloud shadows drift over the ground, birds lift off as the car comes (vultures circle over the desert), chimneys smoke, a wind pump turns, and at night windows glow and cameras flash in the crowd. Along the roads stand farmhouses, barns, log cabins, tin shacks, telegraph lines with their wires, wooden fences and woodpiles, and behind the crowds their tents and parked cars. A CSS grade over the canvas adds a vignette and a sun tint per region. Night stages lay a darkness texture over the world with a hole for the headlights that follows the car, longer with the light pod, and lanterns at the crowds.
- The camera. A chase view that turns smoothly with the direction of travel and looks ahead with speed, zooming out as you go faster; or north-up. Under reduced motion the camera is north-up and the chase option is disabled.
- Everything readable is HTML. The road book, stopwatch with splits and a progress strip, the gauge, the damage diagram, the start lights, the title and stage boards (enamel signs), the championship map, the garage pegboard, the pause page, the punched timing card, the chalk service board and the rally classification are HTML in the game's own 1980s road-book style, with Big Shoulders Stencil, Saira Condensed and Courier Prime loaded only by this example.
- Input. Keyboard (arrows or WASD, Space for the handbrake, R back on the road for a 5 s penalty), on-screen steering buttons, pedals and a handbrake lever on touch screens, and a gamepad with analogue steering and triggers, merged once per simulation tick. Keyboard steering eases in and returns faster, like hands on a wheel.
- Sound is generated with Web Audio, off until turned on. The engine is a four-cylinder voice updated every simulation tick: a harmonic wave at the firing frequency, a crank sub-harmonic, a rasp an octave up, soft clipping that hardens with the throttle, a pulse at the firing rate, exhaust breath and gearbox whine, a short dip and clunk on each shift, pops on the overrun (with flames from the exhaust in the picture) and a turbo whistle and blow-off on the four-wheel-drive car. Under it: a gravel bed with single stones clicking against the floor, a softer snow crunch, road roar and a two-tone squeal on tarmac, wind, a crowd that cheers with whistles and air horns, impacts with a metal ring, landings, splashes, and an ambience per region through a generated room reverb (birds, surf and gulls, a howling wind, cicadas; crickets at night). The co-driver's radio voice is syllables of hiss and formant vowels through a band-limited, lightly clipped intercom with a squelch at each end.
- Saves. The career (credits, cars, upgrades, setup, open regions, the rally in progress, best times and medals, the daily stage) and the settings are kept in local storage and cleaned on load; best runs are stored as ghosts.
Balance
core/balance.test.ts has the AI play whole careers: it drives every rally stage at a fixed skill, services the car (worst parts first within the 30 minutes), takes the prize money, saves for the next car once it is on sale and otherwise buys the cheapest upgrades. The quick check always runs (a careful driver in the starting car finishes every Pinewood stage and wins at least bronze on three of four). BALANCE=1 writes tmp/switchback-balance.txt.
- Medal times first came from one AI run on the limit, which turned out slower than a careful run because the AI on the limit crashes more; gold is now the best of three runs.
- With the Tor at 9800 credits a career never afforded it and stalled in Red Mesa; at 7500, with the bot saving for it, careers at three skills reach Red Mesa after 12 stages and finish on its podium after 20 to 28 stages.