Back to Projects

Grain

Browser falling-sand sandbox built on a chunked cellular automaton, with sand, water, fire, acid, gas, oil, wood, stone, and spawners. Zero dependencies, no build step.

Architecture Overview

Struct-of-arrays world state (type, life, shade, and tick-stamp Uint8Arrays) stepped bottom-up over a double-buffered active-chunk map; a separate UI module handles the toolbar, pointer/keyboard input, and the main loop.

Key Challenges

Keeping the simulation unbiased and correct under chunk sleeping: cells on chunk edges wake their neighbours, a per-cell tick stamp prevents double-moves, and the sweep direction alternates per row to avoid sideways drift.

Security Considerations

Not a security-focused project; it is a fully static client-side page with no network calls, accounts, or user data.

Future Improvements

Planned: more element interactions and reactions, plus save/load of scenes.

Technical Impact

  • Built a falling-sand cellular automaton in plain JavaScript ES modules with no dependencies or build step.
  • Implemented chunk-based dirty tracking over 16x16 chunks, so only chunks disturbed in the previous step are simulated and settled regions sleep at zero cost.
  • Modeled density-based displacement, liquid viscosity, acid corrosion, fire spread, and gas/steam/smoke lifetimes as per-element update rules.
  • Rendered directly into a Uint32Array-backed ImageData using precomputed per-element shade and fire-intensity color lookup tables.