Visualizations
077 / 500

077 · Physics

Rigid Bodies

Stacked towers of boxes knocked down by a swinging wrecking ball.

A small rigid body physics engine: boxes and circles with mass, rotation and friction. Collisions are found with the separating axis theorem, contact points come from clipping, and an iterative impulse solver with warm starting keeps tall stacks and pyramids standing. A wrecking ball on a chain of distance joints swings through towers of wood, stone and glass, throwing up dust and shattering the glass, then the scene clears and rebuilds.

Try it. Drag any body to throw it. Click empty space to drop a box. Press R to rebuild.

  • Separating axis theorem
  • Sequential impulse solver
  • Rotational dynamics

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build a small 2D rigid body physics engine with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Make a canvas that fills the window, stays sharp on high-DPI screens (scale by devicePixelRatio), and resizes with the window. Simulate in meters.
- Give each box a position, angle, velocity, angular velocity, mass and moment of inertia. A static ground box has zero inverse mass.
- Detect box versus box contacts with the separating axis theorem: test every face normal of both boxes, and keep the axis with the least penetration.
- Find contact points by clipping the other box's incident edge against the side planes of the reference face.
- Resolve contacts with sequential impulses: about ten iterations per step, accumulating a normal impulse that never pulls and a friction impulse clamped by the normal one. Add a small position bias so overlaps slowly correct.

Once that works, make it beautiful:
- Keep contacts between frames, matched by which edges produced them, and warm start each step with last frame's impulses. This is what lets a tall stack stand still, so build a tower and a pyramid to prove it.
- Add circles, then a heavy wrecking ball hanging from a chain of distance joints that swings through the towers.
- Give the blocks wood, stone and glass looks, drop shadows, and dust puffs on hard impacts.
- Let me drag bodies with the mouse and click empty space to drop a new box.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as glass that shatters, sleeping bodies that stop simulating when still, or joints for ragdolls and bridges.
PreviousParticle LifeColored particles with simple attractions that assemble into living cells. NextBreadboard CPUA microcoded 8-bit breadboard computer paints cellular automata on a 16x16 LED matrix.

Related visualizations

  • Ball PitPhysics Hundreds of colorful balls tumble, stack and bounce off each other.
  • Spinning TopsPhysics Rigid-body tops precess, wobble and clash in a steel dish, then topple as they slow.
  • JellyPhysics Squishy soft bodies that wobble, bounce and squash into each other.
  • Tumbling T-HandlePhysics A spinning T-handle in zero g flips end over end, again and again.
  • Shattered PanePhysics A sunny window breaks into radial and ring cracks, then shards tumble and glint.
  • Pool Trick ShotsGames Billiards with real spin physics and an AI that plans by simulating every shot.
  • Dynamical BilliardsPhysics A fan of 1,000 balls in reshapable tables, linked live to the Birkhoff phase map.
  • Tensegrity3D Struts that never touch, floating in a web of cables, found by dynamic relaxation.
  • Truss Bridge BuilderPhysics Draft a bridge on blueprint paper, then send a truck over as each beam shows its stress.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs