≈ the.bay.news

Water, sand and lava on a 96x96 grid in plain JavaScript: the few rules that keep it stable

programming.dev
Water, sand and lava on a 96x96 grid in plain JavaScript: the few rules that keep it stable
I’ve been building a small god game for the browser: a hand over an island scoops and pours sand, water and lava so a tribe survives a wave. The terrain is a 96x96 grid in plain JavaScript, stepped in a web worker. The flow came down to a few small rules: - Water moves by virtual pipes. Each cell keeps a flow along four pipes; each step adds dt * g * (height difference) and damps the old flow. Water then has momentum, so a wave runs up a beach and falls back instead of oozing. - A cell can’t give more than it holds. If the four outflows times dt exceed the water in the cell, all four are scaled by water / out. Without it a big pour can drain a cell below zero. With it, a fixed step (dt = 0.05) and a rate-limited pour stay stable, so I never needed sub-steps. - Sand slumps past an angle of repose, flatter when wet, and the same scale-to-what-you-have rule keeps a cell from giving sand it lacks. - Silt rides the water’s own pipes: the share of a cell’s water that leaves carries the same share of its silt. Fast water picks sand up; slow water drops it. That alone makes river deltas and wave-cut beaches. - Lava is a second fluid with its own gravity and damping, turning into rock slowly in air and a few hundred times faster under water. Pouring lava into the sea builds a wall that doesn’t erode. - The hand can also still water: cells in a small radius close their pipes for ten seconds, long enough to build a wall in the middle of a flood. There’s also a daily sea where every wave rises higher on a full or new moon, from the real moon phase, the same for everyone that day. What would you have done differently? I’m curious whether anyone ran shallow-water equations on a grid this small and found them worth the cost. Plays free in the browser, no account: https://overflow.games/stillwet/ [https://overflow.games/stillwet/]

0 comments

Sign in to join the discussion — your thebay.events account works here.

No comments yet.