Sandcastle
A sand and water physics simulation with multiple renderers. Click to raise/lower sand, add water, watch it flow and erode terrain.
๐ฎ Live Demo
โ ๏ธ Work in Progress - Under active development.
Features โจ
- ๐๏ธ Sand physics (collapse, angle of repose)
- ๐ง Water physics (flow, surface tension, erosion)
- ๐จ Multiple renderers (2D top-down, isometric, voxel WebGL, Three.js 3D)
- ๐ฑ๏ธ Interactive terrain editing
Physics โ๏ธ
See PHYSICS.md for detailed explanation of how the physics engine works.
Summary:
- Sand: Cellular automata with angle of repose, collapses when slopes exceed ~35ยฐ
- Water: Surface harmonization with surface tension thresholds (0.05 wet, 0.8 dry)
- Both use random iteration to avoid directional bias
- Water checks evolving state during iteration for faster convergence
Renderers ๐จ
See RENDERERS.md for detailed explanation of how rendering works.
Available renderers:
- Top-Down (Canvas 2D): Overhead view, fastest
- Voxel (WebGL): Instanced 3D cubes with lighting/AO
- Three.js (WebGL): Full 3D with PBR materials (mostly broken for now)
All support runtime switching without affecting simulation.
Architecture ๐๏ธ
src/
โโโ core/
โ โโโ simulation/
โ โ โโโ base/
โ โ โ โโโ Grid2D.ts # Base grid class
โ โ โโโ sandgrid/
โ โ โ โโโ SandGrid.ts # Sand height storage
โ โ โโโ water/
โ โ โ โโโ WaterGrid.ts # Water depth + velocity
โ โ โโโ physics/
โ โ โ โโโ SandPhysics.ts # Sand collapse logic
โ โ โ โโโ WaterPhysics.ts # Water flow + erosion
โ โ โโโ Simulation.ts # Main coordinator
โโโ renderers/
โ โโโ base/
โ โ โโโ Renderer.ts # Renderer interface
โ โโโ canvas2d/
โ โ โโโ TopDownRenderer.ts
โ โ โโโ IsometricRenderer.ts
โ โโโ webgl/
โ โโโ VoxelWebGLRenderer.ts
โ โโโ ThreeJSRenderer.ts
โโโ systems/
โ โโโ GameLoop.ts # Fixed timestep loop
โ โโโ SceneManager.ts # Lifecycle management
โโโ input/
โ โโโ InputManager.ts # Mouse/keyboard handling
โ โโโ tools/
โ โโโ SandTool.ts # Raise/lower sand
โ โโโ WaterTool.ts # Add water
โโโ ui/
โโโ UIManager.ts # Controls and debug info
Installation ๐ฆ
npm install
npm run dev # development
npm run build # production
npm test # run testsUsage ๐ฎ
- Click/drag to raise sand, Shift+click to lower
- Select water tool to add water
- Adjust physics parameters in UI
- Switch renderers from dropdown
Tech Stack ๐ ๏ธ
- TypeScript + Vite
- Three.js for 3D rendering
- Vitest for testing
- Canvas 2D for simple renderers
Fixed 60Hz physics timestep, variable render rate.
Made with humans ๐จโ๐ป using AI ๐ค ๐ค