Interactive falling-sand sandbox built in C++ using SDL2.
Click to drop sand and watch it fall, stack, and slide with simple physics.
Includes a Restart button, Exit button, pixel-art grid and smooth simulation loop.
- Falling sand physics using a cellular-automata model
- Pixel grid visual style
- Click to spawn sand
- Restart button to clear the screen
- Exit button to stop the app
- UI for materials
- SDL2 rendering with VSync
- Easy to extend (new materials like water, stone, etc.)
You need these installed:
- Homebrew
- Xcode Command Line Tools
- SDL2
- SDL2_ttf (for rendering text )
Install if needed:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" xcode-select --install brew install sdl2 sdl2_ttf
Place a .ttf font in the same folder as sand.cpp, for example:
OpenSans-Regular.ttf
Compile using:
clang++ sand.cpp -o sand
-I/opt/homebrew/include
-I/opt/homebrew/opt/sdl2/include
-I/opt/homebrew/opt/sdl2_ttf/include
-L/opt/homebrew/lib
-L/opt/homebrew/opt/sdl2/lib
-L/opt/homebrew/opt/sdl2_ttf/lib
-lSDL2 -lSDL2_ttf
Then run:
./sand
Hold Left Mouse Button → Spawn falling sand
Click Restart Button → Clear the world
Click Exit Button → Exit the world
Close Window → Exit the app
Each sand grain acts as a cell in a fixed grid.
Every frame, each grain tries in order:
- Move down
- Move down-left or down-right if blocked
- Otherwise stay put
This produces realistic piling and sliding behavior.
- Water, stone, smoke materials
- Right-click → eraser
- Brush size options
- Randomized sand color variation
- Save/load sandbox state
MIT License
Pranav Kumar