Skip to content
Theo Bains
Menu
All projects

independent project

2D Rigid-Body Physics Engine

A Python pool simulation implementing rigid-body motion, collision detection, and impulse-based collision response.

Interactive collision table

Pool-table impulse demo

Object balls start in a pyramid rack. Aim the cue to preview predicted paths for every ball, then press Shoot to animate. Drag object balls to reposition - they cannot overlap.

Pyramid rack ready. Aim the cue, preview predicted paths, then shoot.

Trajectory mathematics

  • Impulse shot: cue velocity v = (power) · û, where is the unit vector from the pull-back point toward the cue.
  • Equal-mass elastic collision along the contact normal : exchange the normal components with restitution e, leaving tangential velocity unchanged.
  • Cushion bounce: reverse the wall-normal velocity and scale by e; motion integrates with per-step friction so paths settle to rest.

Balls on table: 0

Basic explanation

This project simulates balls on a two-dimensional pool table: update positions over small time steps, detect overlapping circles, and resolve contacts with impulses so the balls bounce apart convincingly. An impulse is an instantaneous change in momentum modelling a brief collision force. The coefficient of restitution e[0,1]e \in [0,1] scales how much relative speed along the contact normal survives the impact - near 11 for lively elastic bounces, near 00 when most energy is lost.

Mechanics

Between collisions, motion is integrated with a semi-implicit Euler step. Contact detection treats balls as discs; when penetration is found, an equal-and-opposite impulse along the contact normal separates the velocities consistently with conservation of momentum and the chosen restitution. Keeping the integrator, collision response, and rendering separate made it easier to test each piece in Python before adding table cushions and friction-like damping.

Interactive demonstration

The browser demo is a top-down pool-table reconstruction of that collision system. You can add and drag balls, aim the cue ball’s direction and speed, and fire a shot. As balls move and collide, dotted trails record their paths and dotted markers show where each ball comes to rest - so the geometry of the impulse responses is visible after the motion settles.