A C++ implementation of a 2D physics engine using SFML for rendering and spatial grid partitioning for efficient collision detection.
- Real-time 2D physics simulation
- Spatial grid-based collision detection (O(n) complexity)
- Elastic and inelastic collisions
- Gravity and viscosity effects
- Particle-based rigid body dynamics
- Boundary collision handling
- Energy conservation and dissipation
- Newton's Laws of Motion
- Conservation of Momentum
- Elastic Collisions
- Impulse-Based Physics
- Spatial Partitioning
- Collision Detection and Resolution
- Energy Dissipation
- SFML 2.5+
- C++17 or higher
- CMake 3.10+ (optional)
- Install SFML:
sudo apt-get install libsfml-dev
- Clone the repository:
git clone https://github.com/yourusername/physics-simulation.git
cd physics-simulation
- Compile the code:
g++ -o spatial spatial.cpp -lsfml-graphics -lsfml-window -lsfml-system
Run the simulation:
./spatial
- Close window: Click the X button
- Physics parameters can be adjusted in the source code:
NUM_OF_BODIES
: Number of particlesGRAVITY
: Gravity constantVISCOSITY
: Damping factorCELL_SIZE
: Size of grid cells for collision detection
- Divides the window into a grid of cells
- Each cell tracks particles within its bounds
- Reduces collision checks to nearby particles only
- Circle-circle intersection tests
- Grid-based broad phase
- Precise narrow phase checking
- Impulse-based collision response
- Energy conservation with optional damping
- Position correction to prevent sinking
This project is licensed under the MIT License - see the LICENSE file for details.
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a new Pull Request