A real-time 2D physics simulation built from scratch in C++20 with SFML 3. Spawn balls into a bounded box and watch them fall under gravity, bounce off walls, and collide with each other.
- Circle-Circle Collision Detection -- Distance-based overlap test between ball pairs, optimized with a sweep-and-prune pass (sort on X-axis, early exit when separation exceeds combined radii).
- Impulse-Based Collision Resolution -- Computes collision normals and applies mass-proportional impulse responses so smaller balls react more than larger ones.
- Wall Bouncing -- Balls reflect off all four walls of a centered bounding box with velocity clamping to prevent tunneling.
- Gravity & Integration -- Constant downward acceleration with per-frame Euler integration for position and velocity updates.
- Interactive Spawning -- Press Enter to spawn a new ball at the top of the box with a random radius (15--25 px), random velocity, and random color.
Ball.h Circle entity with position, velocity, radius, mass, and color
Box.h Axis-aligned rectangular boundary
Vector2D.h/cpp 2D vector math (arithmetic, dot product, normalization)
Physics.h/cpp Collision detection, resolution, wall bouncing, physics step
main.cpp SFML window, event loop, rendering
CMakeLists.txt Build configuration
cmake -B build
cmake --build build
./build/main| Key | Action |
|---|---|
| Enter | Spawn a new ball |
| Close | Click the window close button |