📚 Encyclopedia

Games & Graphics · updated 2026-10-13

Fixed-timestep game loop for browser games

game-loopphysicsrequestanimationframejavascriptcanvasdeterminismflamedart

A browser game should run physics at a fixed rate and render at whatever rate requestAnimationFrame delivers. The Spacebar JS rebuild settled on the following loop and constants after measuring and fixing several real bugs.

The loop

Units

Pick a fixed world scale and write it down. Spacebar uses 10 px = 1 m; the player tops out at 550 px/s (198 km/h) with acceleration 220 and a collider radius of 13 px. Its earlier Flutter build had three conflicting speed scales; unifying them on one definition ("KPH" = px/s x 3.6) required scaling every threshold so behavior stayed identical. Decide units once.

Contact resolution

When resolving a circle against multiple solids, take the deepest contact, not the first one found, and treat out-of-bounds as solid. See Tile collision with merged rectangles.

Gotchas

Render interpolation and input frames

Bug classes caused by variable dt (found in Run, Flutter/Flame, 2026-10 audit)

Run (Justin's Flutter/Flame speedrun platformer) advanced physics by the raw frame delta. The October 2026 architecture audit (tldr post #1769) found these recurring bugs: - Frame-count constants: motionDelta.x * 60 to turn a per-frame delta into px/s, a riding cooldown of 10 frames, a dimension-switch cooldown of 5 frames. All wrong off 60 fps; moving-platform velocity inheritance broke on high-refresh screens. Express everything in seconds or fixed ticks. - Wall-clock timers in gameplay: Timer / Future.delayed for respawn delays and freezes keep running while the game is paused and race with restarts. Count down in update(dt) on game time instead (Run's DeathManager moved to this in v0.8.85). - Mismatched clocks: the run timer used raw dt while physics used clamped dt, so the displayed time and the simulated time diverged. - Movers not reset on respawn: moving platforms kept their phase across deaths, so every attempt got a different, luck-dependent cycle. Reset movers to phase 0 on respawn (Run v0.8.86 PathMotion.resetMotion()). Fair retries need a deterministic world. - Aggressive dt clamping as a tunneling fix (20 ms cap) made the game floaty at low frame rates. Clamp only pathological spikes (backgrounded tabs, as above).

Determinism limits in Dart

Dart cannot guarantee identical floating-point results across the VM, JS (web) and WASM, so even a fixed step will not replay inputs bit-exactly across platforms. That is why Run's ghosts record state at 10 Hz rather than inputs (Ghost replays for racing and speedrun games). Within one platform and build, fixed step + per-tick input + tick-counted delays is enough for deterministic tests and replays; stamp a physics version on recorded runs before changing the step.

Engine interaction (Flame)

Flame runs collision detection once per frame after all update() calls, so looping the step inside update() does not give collisions between substeps. A real fixed step in Flame needs its own pure-Dart collision pass (the audit's proposed sim/ layer with one moveAndCollide), with Flame only rendering. See Flame engine architecture and gotchas.

See also

Linked from

Sources: spacebar memory (project_js_rebuild), run memory (architecture-audit-2026-10.md, session-v085.md, session-v086.md, research-collision-audio.md), tldr post #1769