Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

2D Physics Engine in C++

A C++ physics project covering rigid-body dynamics, collision detection, constraint solving, and interactive cloth. SDL2 provides the window and input; the simulation math lives in the project source.

C++17 · SDL2 · Collision detection · Constraints · Verlet integration

Cloth demo · Collision demo · Constrained bodies

Two simulation paths

Rigid bodies

The World implementation applies forces and torque, integrates motion, detects contacts between pairs of bodies, and resolves joint and penetration constraints. The solver runs five iterations per update before integrating velocities.

The source includes circle and polygon shapes, contact handling, and the vector/matrix operations used by the solver.

Cloth

The current executable opens the cloth demo. A grid of points is connected by distance constraints, with selected points pinned along the top edge. Verlet-style position integration and constraint corrections give the fabric its motion. Mouse input lets you pull the cloth or break its connections.

The linked rigid-body videos show other project demos; they are not scene presets exposed by the current executable.

Build and run

Requires a C++17 compiler, Make, SDL2, SDL2_image, SDL2_gfx, and a graphical desktop. Install the development headers and libraries for all three SDL packages.

git clone https://github.com/coganka/2d-physics-engine.git
cd 2d-physics-engine
make build
make run

The Makefile assumes the libraries are on the compiler's default search path. For an installation exposed through pkg-config, the equivalent build is:

c++ -std=c++17 -Wall $(pkg-config --cflags sdl2 SDL2_image SDL2_gfx) \
  src/*.cpp src/Physics/*.cpp \
  $(pkg-config --libs sdl2 SDL2_image SDL2_gfx) -lm -o app

The application opens a fullscreen desktop window. Press Esc to exit.

Cloth controls

Input Action
Left mouse button + movement Pull nearby cloth points
Right mouse button Break nearby connections
Mouse wheel Change interaction radius
Esc Exit

The initial cloth dimensions and spacing are set by app.Setup(1200, 320, 10) in src/Main.cpp.

Code worth reading

Source Responsibility
World.cpp Rigid-body update order, pairwise collision checks, solver iterations
CollisionDetection.cpp Shape-specific collision handling
Constraint.cpp Joint and penetration constraint solving
Point.cpp, Stick.cpp Cloth integration and distance constraints
Cloth.cpp Grid construction, pinning, updates, and drawing
Application.cpp Input and the active demo loop

Design tradeoffs

The rigid-body world checks every pair of bodies, which keeps collision flow straightforward but scales quadratically. The cloth loop uses frame elapsed time rather than a fixed simulation step. Those choices suit a small interactive demo; spatial partitioning, fixed-step updates, and numerical regression tests are natural next improvements.

The project is intended to make the mechanics inspectable. It does not claim the stability, performance, or API coverage of an established game-physics library.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages