Level Discoverystart here Intermediatethe architecture Expertin depth
Intermediate · Batch 4 · Volcano
Chapter 4 · intermediate level

Destructible terrain & shared state

How a battlefield can be torn apart and still stay perfectly in sync across screens: a heightfield + craters, seed-based determinism, a compact snapshot, and a first look at turbo's continuous wind.

1The terrain is a heightfield, not an image

The destructible ground isn't a big bitmap. It's a heightfield: one surface height per column, plus a growing list of craters. Cheap to store, cheap to send, and easy to keep identical everywhere.

src/sim/terrain.js — the surface height per column
export function generateHeights(seed, roughness = 1) {
  const r = rng(seed);              // seeded generator
  const heights = new Float32Array(width);
  // sum of sine waves → natural relief
}

Destruction doesn't rewrite that array. Instead, we keep a list of craters alongside it. The terrain's final shape is “the heightfield minus the craters” — a subtraction computed on demand, never materialised.

State = heights + craters. The whole battlefield is two pieces of data. Everything the game needs to ask — “is this point solid?”, “did the ball land?” — derives from them.

2Solidity by squared distance

A point is solid when it sits below the surface and inside no crater. Testing “inside a crater?” is a circle test — and we do it without a square root by comparing the squared distance to the squared radius.

src/sim/terrain.js — inCrater & pointSolid
export function inCrater(craters, x, y) {
  for (const c of craters) {
    const dx = x - c.x, dy = y - c.y;
    if (dx * dx + dy * dy <= c.r * c.r) return true;
  }
  return false;
}
empty (crater) solid ↑ overhang that “floats”
Solid = below the surface minus the craters. A crater below the surface can leave material above it: that's how caves and overhangs appear.

That tiny rule is enough to produce caves and overhangs: a crater dug under a bump leaves the matter above it floating, because that matter is still below the surface and inside no crater.

For the curious. Comparing dx²+dy² with c.r² is the trick from the maths annex: the square root is monotonic, so for a yes/no test the squares answer the same question — without the expensive root.

3Determinism: send the seed, not the terrain

The terrain is a pure function of a seed. The server rolls one number, generates the heights, and ships only that seed in the snapshot. Each client regenerates the identical surface — so the ground the server resolves collisions against is the very ground the TV draws.

src/sim/Simulation.js — the compact snapshot
snapshot() {
  return {
    seed: this.seed,        // regenerate the heightfield from this
    craters: this.craters,  // just the carved circles
    // …phase, wind, scores, towers, live projectiles
  };
}

And the craters? Just a short list of { x, y, r } circles. So the entire destructible state on the wire is one seed + a handful of crater circles — not a heightfield, never a bitmap.

src/sim/Simulation.js — terrain from a seed
if (p.y > 0 && pointSolid(this.heights, this.craters, p.x, p.y)) {
  this.craters.push({ x: Math.round(p.x), y: Math.round(p.y), r });
}
Shared state, kept small. Sending the whole terrain ~30 times a second would be wasteful and could desync. Sending the recipe (seed) plus the diffs (craters) is both lighter and self-consistent.

4A taste of turbo: continuous wind

Classic mode rolls a fresh wind each turn. Turbo mode keeps the wind flowing: it eases between random keypoints every 10 seconds with a smoothstep curve, and layers a light gust wave on top so the breeze breathes.

src/sim/Simulation.js — updateTurboWind (the easing)
const t = this.windClock / WIND_KEY_SECONDS;
const s = t * t * (3 - 2 * t);          // smoothstep easing
const base = this.windFrom + (this.windTo - this.windFrom) * s;

On top, a light gust wave (two sines) rides over the base, its amplitude set by a per-match trait (gustiness). The result: a wind that drifts gently between keypoints but breathes instead of gliding mechanically. The expert level details the full formula.

Remember. Batch 4's architecture is about shared state that stays small and deterministic: terrain as heights + craters, solidity by squared distance, the seed as the recipe — and wind that flows continuously when the pace heats up. The full maths is in the maths annex.
← Chapter 3 · Real-time client/server Chapter 5 →