2D Physics Engine Simulator: Rigid Body Collisions

simulator intermediate ~9 min
Loading simulation...
8 rigid bodies — restitution 0.7, gravity 980 px/s²

Eight rigid bodies fall under gravity (980 px/s²) and collide with 70% energy retention per bounce. Friction gradually decelerates sliding. After approximately 5 seconds, most energy is dissipated and bodies come to rest.

Formula

Position: x(t+dt) = x(t) + v(t)×dt + 0.5×a×dt²
Collision response: v' = -e × v (normal component)
Kinetic energy = 0.5 × m × (vx² + vy²)

Physics Makes Games Feel Real

When a crate slides down a ramp in Half-Life 2, when birds smash into pig fortresses in Angry Birds, when cars crumple in BeamNG.drive — physics engines are doing the work. At their core, these engines solve Newton's equations of motion in real time: F=ma for acceleration, impulse-momentum for collisions, and constraint solvers for joints and contacts. This simulator demonstrates the fundamental building blocks.

The Simulation Loop

Every frame, the physics engine performs the same steps: apply forces (gravity, player input), integrate to get new positions, detect collisions, resolve interpenetrations, and apply impulses to change velocities. This cycle repeats 60 times per second (or more — many engines run physics at a fixed timestep of 120Hz or 240Hz for stability). The time scale slider lets you slow this loop to observe individual collisions in detail.

Collision Detection and Response

Detecting whether two circles overlap is simple: check if the distance between centers is less than the sum of radii. The response is more nuanced. The collision normal determines the direction of the impulse. The coefficient of restitution determines how much velocity is reflected. Friction removes tangential velocity. Together, these three factors determine whether a collision looks like a bouncing ball, a sliding hockey puck, or a crumpling car.

Energy Conservation and Dissipation

In a perfect system (restitution=1, friction=0), total kinetic energy is conserved and objects bounce forever. Real systems lose energy through inelastic deformation (restitution < 1) and friction (converting kinetic energy to heat). This simulation tracks total kinetic energy over time, letting you see how quickly energy dissipates for different material properties — and why all physical systems eventually come to rest.

FAQ

How do game physics engines detect collisions?

Collision detection has two phases: broad phase uses spatial structures (quadtrees, sweep-and-prune) to quickly eliminate pairs that can't possibly collide. Narrow phase tests remaining pairs with precise algorithms — circle-circle distance checks, Separating Axis Theorem (SAT) for polygons, or GJK algorithm for complex shapes.

What is coefficient of restitution?

The coefficient of restitution (e) measures how much kinetic energy is preserved in a collision. At e=1, the collision is perfectly elastic (a superball). At e=0, it's perfectly inelastic (a lump of clay). Real-world values range from 0.8 (basketball) to 0.3 (baseball) to near 0 (beanbag). Games tune this for feel rather than physical accuracy.

Why do objects sometimes pass through each other in games?

This 'tunneling' happens when an object moves far enough in a single frame to skip past a thin wall. Solutions include continuous collision detection (CCD), which traces the object's path between frames, or simply capping maximum velocity. Most physics engines offer CCD as an option for fast-moving objects like bullets.

What physics engines do popular games use?

Box2D (free, 2D) powers Angry Birds, Limbo, and Crayon Physics. Havok (commercial) runs in most AAA titles including Skyrim, Destiny, and Dark Souls. PhysX (NVIDIA, free) is Unity's default and is used in Unreal Engine. Bullet (open-source) is popular in indie games and robotics simulation.

Sources

Embed

<iframe src="https://homo-deus.com/lab/game-design/physics-engine/embed" width="100%" height="400" frameborder="0"></iframe>
View source on GitHub