2D Physics Engine

November 2009 - February 2013

In the process of writing my thesis, I wrote a toy physics engine in C++ with OpenGL and SDL 1.2. The constraint-based impulse physics solver I used closely mirrors that of Box2D; however, the algorithm for generating collision contacts is novel. I also added collision detection for particles and soft-body Verlet dynamics; in the process, I produced a few interesting demo programs.

Browse the source on GitHub.

Demo 1: "Pong" (pairwise collision detection test)

[ ys_pong.zip, 773.2 KB, Windows 32-bit ]
Pong Screenshot

The classic arcade game implemented in a physics engine. This version of Pong has a twist: the square ball spins and the paddles can rotate!

[+] Controls:
  • - move paddle
  • Z - freeze paddle movement
  • X - freeze paddle rotation
  • C V - rotate paddle
  • B - (hold) molasses mode
  • N - (hold) turbo mode

Demo 2: "Emitters" (particle collision test)

[ ys_particle.zip, 829.36 KB, Windows 32-bit ]
Emitters Screenshot

This demo shows off some particle emitters: a "burst" style emitter, two colored flames, a rain emitter, and finally, a rain-of-fire emitter. All particles collide with the map (modeled after a SMB1 warp zone).

Emitters may use other emitters as their particle type. Out of the five emitters, only the "armageddon" emitter does this: the top-level emitter has settings similar to the rain emitter, but the particles created are fire emitters themselves.

[+] Controls:
  • - move emitter
  • Z - toggle selected emitter
  • X - enable selected emitter
  • C - select next emitter
  • V - select previous emitter
  • J - zoom camera out
  • K - zoom camera in

Demo 3: "Stacking" (constraint solver test)

[ ys_stacking.zip, 854.8 KB, Windows 64-bit ]
[ ys_stacking_mac.zip, 292.19 KB, Mac OS X (UNIX executable)]
Stacking Screenshot

Stacking of rigid bodies is achieved by cataloging contacts and friction as constraints, then solving as shown in Iterative Dynamics with Temporal Coherence (Catto 2005). Contacts are visualized as green dots; the solved forces on them appear as magenta lines.

[+] Controls:
  • Home - reset current demo
  • End / Esc - next demo
  • F1 - pause simulation
  • PgDn - step paused simulation
  • J / Mouse scroll ↑ - zoom camera out
  • K / Mouse scroll ↑ - zoom camera in
  • W A S D - move camera
  • Mouse L - grab rigid body