GitHunt
IK

iksaif/sandcastle

Sandcastle Logo

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 tests

Usage ๐ŸŽฎ

  • 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 ๐Ÿค– ๐Ÿค

iksaif/sandcastle | GitHunt