A 2D physics engine built with C++ and SFML, featuring two simulations:
- Spatial Grid — Thousands of Verlet-integrated particles with spatial grid partitioning for O(n) collision detection, slanted wall obstacles, and drag-and-shoot interaction.
- Rigid Body — Rectangle-based rigid body simulation using the Separating Axis Theorem (SAT) for collision detection, with impulse resolution, Coulomb friction, and semi-implicit Euler integration.
Particles are spawned continuously and roll down a zigzag of slanted slopes, accumulating at the bottom. A spatial hash grid partitions the world into cells so that collision checks only happen between nearby particles — enabling 6000+ objects at 120 FPS. Click and drag to shoot new balls into the scene.
Key features:
- Verlet integration for position-based dynamics
- Cell-based spatial partitioning for broad-phase collision
- Segment-based wall collisions with velocity reflection
- Rainbow color cycling based on spawn time
- Drag-and-shoot input via mouse
A tower of rectangular blocks stacked on a static platform. Click anywhere to drop a new block and watch the tower react. Collisions are detected with SAT (Separating Axis Theorem) on oriented bounding boxes and resolved with normal + friction impulses.
Key features:
- Semi-implicit Euler integration with explicit velocity
- SAT collision detection for oriented rectangles
- Impulse-based resolution with restitution and Coulomb friction
- Baumgarte positional correction to prevent sinking
- Moment of inertia:
$I = \frac{1}{12} m (w^2 + h^2)$
On Ubuntu/Debian:
sudo apt install libsfml-devmkdir -p build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)Run either simulation:
./spatial-grid # Particle simulation with spatial partitioning
./rigid-body # Rigid body stacking simulation
