2D tile-based liquid simulation (cellular automata)
cellular-automatawatersimulationterrariaminecraftnoita
How games simulate water on a tile grid, which model fits a 2D platformer, and the performance lessons from Run's liquid system.
Approaches compared
| Approach | Used by | Pressure | Cost |
|---|---|---|---|
| Levels 0-7 | Minecraft | none | cheapest |
| Byte volume 0-255 | Terraria | none | cheap, queue-based |
| Float mass + compression | w-shadow.com, DwarfCorp | implicit | moderate |
| Per-pixel materials | Noita (falling sand) | density swaps | expensive |
| Virtual pipes | academic | yes + waves | moderate |
| Mass in kg, iterative | Oxygen Not Included | yes | expensive |
- Minecraft: source blocks + flowing levels 1-7, 4-block BFS toward the nearest drop-off, two adjacent sources create a new one, updates ~4 Hz on scheduled ticks.
- Terraria: byte volume, max ~5000 active queue entries, "panic mode" over 4000 for 1800 frames, volumes under 2 are deleted; no pressure, so U-tubes never equalize.
Recommended: the compression CA model (w-shadow.com)
Each cell has a float mass (0 empty, 1 full, slightly above 1 = compressed).
Constants: MaxMass 1.0, MaxCompress 0.02, MinMass 0.0001, MinFlow 0.01.
Per cell per tick: flow down first (using the stable-state split), then left/right by
(mass - neighbor) / 4, then up only if compressed.
stable_state(total):
if total <= 1: return total
if total < 2 + MaxCompress: return (MaxMass^2 + total*MaxCompress) / (MaxMass + MaxCompress)
else: return (total + MaxCompress) / 2
Compression gives pressure equalization without tracking pressure.
Gotchas
- Oscillation is the main failure: damp flows (x0.5), ignore flows under MinFlow, divide horizontal differences by 4 not 2.
- Double-buffer the grid to conserve volume; alternate scan direction each frame to avoid directional bias; periodically re-sum total water to correct float drift.
- Dropped water pools slowly ("hill problem"): run several steps per frame.
- Only simulate dirty cells; dormant pools should cost nothing; cap active cells (500-1000 on mobile).
Run's implementation (a simpler, game-oriented variant)
- Discrete cells with a recursive gravity cascade; a stationary source emits through directional outlets (8-direction bitmask); only the source spawns new cells.
- Pressure push: BFS to the nearest empty connected cell, lowest first. Diagonal moves are blocked unless both adjacent cardinal cells are passable.
- Sink platforms block expansion; cells falling below the level are freed and the source
re-enables.
maxBlocks: 0 static, -1 infinite, N cap (safety cap 2000). - Performance lesson: computing blocked/sink cells by scanning every cell against every child each tick created millions of Rect tests and frame spikes (a prime audio-stutter suspect). Rasterize blocked cells once per tick into a set instead.
- Hitbox lesson: one bounding-box hitbox around an L-shaped pool applied the "in liquid" slow in empty air. Use the bounding box only as a broadphase and then test actual cell membership.