Variable timesteps cause drift
Browser displays refresh at very different rates: 60 Hz on phones, 120 Hz on ProMotion laptops, 144 Hz or 240 Hz on gaming monitors. Tie a physics simulation directly to the requestAnimationFrame timestamp deltas and you get integration drift, unpredictable collision tunnelling, and gameplay that differs from one machine to the next.
With Euler integration and a variable dt, two players who enter identical inputs on different machines see their rigid bodies diverge within seconds. In a competitive arcade engine or a physics model that has to be checked, replays stop being reproducible and anti-cheat validation becomes impossible.
A fixed-timestep accumulator
To get bit-identical physics at any refresh rate, separate the physics step from the drawing. The simulation advances in fixed slices (here exactly 16.666ms per step), and the renderer interpolates between the previous and current positions:
const FIXED_TIMESTEP_MS = 1000 / 60; // Exactly 16.666ms
let accumulator = 0;
let lastTimestamp = performance.now();
function gameLoop(now: number) {
const frameDelta = Math.min(now - lastTimestamp, 250); // Bound spiral of death
lastTimestamp = now;
accumulator += frameDelta;
while (accumulator >= FIXED_TIMESTEP_MS) {
integrateRigidBodies(physicsState, FIXED_TIMESTEP_MS);
resolveCollisions(physicsState);
accumulator -= FIXED_TIMESTEP_MS;
}
// Fractional alpha for butter-smooth visual interpolation
const alpha = accumulator / FIXED_TIMESTEP_MS;
renderInterpolated(renderContext, physicsState, alpha);
requestAnimationFrame(gameLoop);
}
Interpolating between ticks
The fraction alpha says how far the renderer is between the previous physics tick and the current one. Interpolate position and rotation with lerp(prevPos, currPos, alpha) and a 144 Hz monitor draws smooth frames with no jitter, while the physics underneath runs at a locked 60 Hz.
Allocating nothing in the loop
Garbage collection spikes cause most of the micro-stutters and dropped frames in busy 2D canvas scenes. Creating temporary objects, such as coordinate vectors, inside the inner simulation loop makes the runtime collect garbage often.
TypedArray pools and ring buffers
Allocate contiguous Float64Array pools and circular ring buffers once, when the engine starts, and gameplay allocates no memory at all:
class ParticleRingBuffer {
private readonly positions: Float64Array; // [x0, y0, vx0, vy0, ...]
private head = 0;
constructor(public readonly capacity: number) {
this.positions = new Float64Array(capacity * 4);
}
spawn(x: number, y: number, vx: number, vy: number): void {
const idx = (this.head % this.capacity) * 4;
this.positions[idx] = x;
this.positions[idx + 1] = y;
this.positions[idx + 2] = vx;
this.positions[idx + 3] = vy;
this.head++;
}
}
Sharp canvases on high-DPI screens
On high-DPI displays (such as Apple Retina panels with devicePixelRatio = 2 or 3), a canvas sized the naive way comes out blurry. Scale the backing buffer by the device pixel ratio and hold the CSS size fixed, and the canvas rasterizes crisply at the sub-pixel level, with proper anti-aliasing.
To see these physics loops and memory pools running, play the Quasi-Perfect Puzzler or work through the physics obstacles in Retro Labyrinth. The maths behind them is in the Laser Loon Architecture.