Skip to content

hilbertcube/Simple-Physics-Engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spatial Partitioning & Rigid Body Physics

A 2D physics engine built with C++ and SFML, featuring two simulations:

  1. Spatial Grid — Thousands of Verlet-integrated particles with spatial grid partitioning for O(n) collision detection, slanted wall obstacles, and drag-and-shoot interaction.
  2. 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.

Screenshots

Spatial Grid (./spatial-grid)

Spatial Grid

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

Rigid Body (./rigid-body)

Rigid Body

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)$

Requirements

On Ubuntu/Debian:

sudo apt install libsfml-dev

Build & Run

mkdir -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

About

Simple 2D physics engine implementing spatial grid partitioning to accelerate broad-phase collision detection, combined with SAT-based collision handling for rigid body simulation

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors