WOAH
A colorful falling-sand simulation mid-catastrophe on sandspiel.club

Play

Let there be sand: a falling-sand physics toy written in Rust

By The host4 min#simulation#wasm#play

Sandspiel gives you elements, physics, and an empty canvas, then gets out of the way. Rust in the browser, chaos in your afternoon.

Every great physics toy is a moral test. Sandspiel hands you water, sand, fire, plants, oil and lava, and within ninety seconds you have built a little garden solely to burn it down.

Sandspiel, by Max Bittker, is the falling-sand game perfected for the modern browser. Pick an element, paint it onto the canvas, and watch the pixel physics do the rest: sand piles, water flows, plants grow along moisture, fire chases oil, lava turns everything into a geology lesson. Every particle follows tiny local rules, and the tiny rules add up to landslides, ecosystems and disasters with no scripting anywhere.

It shipped in 2018 and the internet moved in immediately. Within months, Bittker's own writeup counts a couple thousand people playing every day and a gallery past seventy thousand uploaded creations.

The genre has elders

Falling-sand games are a lineage, and Sandspiel knows its family tree. The elder every fan names first is The Powder Toy, the open-source desktop sandbox that simulates air pressure, heat, gravity and what its own page calls "a countless number of interactions between different substances", still shipping today at version 100.1 with a Lua API so players can script their machines.

The genre's loudest graduate is Noita, the roguelite built on an in-house C++ engine named Falling Everything, where every pixel of the game world is simulated. It took the Seumas McNally Grand Prize at the IGF and the GDCA Best Technology award for proving the idea scales to a whole game.

Petri Purho's GDC 2019 talk on that engine is the best hour anywhere on the technique, including the trick where loose pixels become rigid bodies: marching squares traces their outline, a line-simplification pass thins the vertices, and the triangles get handed to Box2D.

Sandspiel's contribution was making all that heritage load in about a second, in a tab, with a share button.

Ninety thousand cells, thirty-two bits each

The simulation core is Rust compiled to WebAssembly, and it is Bittker's third run at the idea: earlier versions lived in JavaScript and Lua, and the Rust rewrite is what bought the big grid. The world is 300 by 300, 90,000 cells, nine times the area of the JavaScript version, and each cell packs its whole existence into 32 bits: one byte naming the species, two spare registers each element uses for its own state, one byte marking who already moved this frame.

The two dead prototypes earn their mention. The 2015 version was pure JavaScript pushing a 100 by 100 grid through a canvas; the 2018 one was Lua running in the LÖVE framework. The Rust engine that replaced them compiles to a WebAssembly binary of 84 KB, a figure that actually went up from 66 when Bittker swapped the size-optimizing compiler flag for the fast one and decided the trade was worth it.

Any single element's rule is kept deliberately simple. The complexity you watch, Bittker writes, unfolds in the interactions between elements and across the space.

That is the cellular-automata bargain, dumb cells, smart crowd, the same one that grows Lenia's gliding creatures out of a single smooth equation. Here it runs with nineteen species and a fire button.

That format is the speed trick. The grid doubles as a valid RGBA texture, so the world ships to the GPU whole and a WebGL fragment shader paints it, a render step the writeup clocks at under one millisecond of CPU time inside a sixteen millisecond frame budget.

The wind is a second simulation stacked on the first. Smoke curls and embers drift because a GPU fluid solver, adapted from the same Navier-Stokes implementation I tore down in an earlier sighting, computes a velocity field for the particles to ride. Two simulations, sixty frames, one tab.

Even the gallery is a trick. A creation saves as a PNG, because the grid already is an image, and lands in Postgres behind a Firebase Cloud Function, while React runs the interface around the canvas. That gallery is what turned a physics sandbox into a museum of tiny engineered wonders, cannons and rain machines and self-sustaining forests, each one a program written in sand.

The PNG trick is tidier than it first sounds. The red channel carries each cell's species, green and blue carry its registers, so a save is not a picture of the world, it is the world, read back through the fast PNG codec every browser already ships. Even the boring part has a number: the gallery's Firestore bill was climbing toward 50 dollars a month before a migration to Postgres cut the cost by roughly 90 percent.

What the sand grew next

Bittker kept pulling the thread. Sandspiel Studio, built with the programmer Lu Wilson, is the editor version of the idea, a block-based visual language for designing your own elements and sharing them, still wearing a beta tag in its title.

And orb.farm points the same instincts at peace instead of arson: a sealed aquarium you stock with algae, fish and bacteria, then leave to find its own balance. Same pixels, opposite moral test.

Where the blueprints are

Making Sandspiel, the design writeup, covers all of the above in Bittker's own words, and the source is MIT-licensed on GitHub with 3,200 stars for company. When you want to build one, the Rust and WebAssembly book's Game of Life tutorial teaches the exact toolchain, cells to canvas.

Yes, it works on your phone. No, you will not do anything productive today. The fire button is right there.

Previously

The flat map stands up: OpenStreetMap rendered as a 3D world