Level Discoverystart here Intermediatethe architecture Expertin depth
Expert · Batch 1 · Meadow
Chapter 1 · expert level

Deterministic loop & numerical integration

Deep in the engine: why a fixed timestep and an authoritative server, what Euler integration is really worth, and how to guarantee that one and the same calculation always produces the same match.

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.

server/Room.js — the authoritative clock
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.

Authority. The clients never integrate the physics. They send intents (angle, power, “ready”) and only display the snapshots they get back. This eliminates disagreements between machines — and cheating via a modified client.

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.

src/sim/Simulation.js — stepProjectiles
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.

Sub-steps. Need more precision without speeding up the clock? Split each tick into N sub-steps of 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.

the terrain is a pure function of the seed
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.

src/sim/rng.js — mulberry32, a PRNG with a tiny seed
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;
  };
}
Remember. Fixed-step loop + server authority + seeded RNG = a deterministic, replayable match. It's the invisible skeleton that makes everything else possible: networking, destructible terrain, continuous wind.
← Home Chapter 2 · Asset-free rendering →