1Fixed step, single authority
The simulation does not advance at the pace of the display. The server steps it in fixed steps of 33 ms (≈ 30 Hz), regardless of each screen's fps.
const TICK_MS = 33; // ~30 Hz const DT = TICK_MS / 1000; // constant step, in seconds this.loop = setInterval(() => this.step(), TICK_MS);
A constant step has two virtues. First, numerical stability: the integration error no longer depends on an erratic dt (a lag spike no longer “teleports” a ball through a tower). Second, determinism: same inputs + same steps ⇒ same result, bit for bit.
2Integrating the motion: semi-implicit Euler
The trajectory obeys a differential equation (constant acceleration per step). We approximate it with the simplest method, Euler integration — but in its semi-implicit (symplectic) variant: we update the velocity first, then the position using that fresh velocity.
p.vx += wind * dt; // 1) velocity updated first p.vy += gravity * dt; p.x += p.vx * dt; // 2) position with the fresh velocity p.y += p.vy * dt;
“Naive” Euler (position before velocity) drifts and inflates energy over time; the semi-implicit variant stays far more stable for the same cost. The local error is O(dt²): at 30 Hz, for flights of a few seconds, the gap is invisible — and, crucially, it is the same for everyone.
dt/N. The shot simulator in the discovery chapter already does this (2 sub-steps per frame) to smooth the curve. It's the classic precision/cost trade-off.3Reproducibility: the seed is the truth
For the server and every client to draw exactly the same terrain, we banish Math.random() from everything that has to be shared. Instead, a seeded pseudo-random generator: same seed ⇒ same sequence of numbers ⇒ same world.
this.seed = this.randInt(1, 2**31 - 1); this.heights = generateHeights(this.seed, roughness); // the server sends only the seed; each client regenerates it identically
The snapshot therefore carries the seed, not the hundreds of terrain heights — bandwidth saving and a consistency guarantee. Same principle for the hidden jitter of shots: drawn from the server's RNG, never transmitted, so invisible and yet reproducible on the server side.
export function mulberry32(seed) { let a = seed >>> 0; return function next() { a = (a + 0x6d2b79f5) | 0; let t = Math.imul(a ^ (a >>> 15), 1 | a); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; }