The Universe Sandbox — Verlet N-Body Gravity, Lensing Black Holes
Spawn planets. Watch orbits stabilize. Drop a black hole and watch space distort. Real Newtonian gravity with Barnes-Hut octree above 100 bodies. Time scale to 1,000,000x.
What this is
A 3D N-body gravity sandbox. Real Newtonian gravity between every pair of bodies (with Plummer softening). Velocity Verlet integration for symplectic energy conservation. For N > 100 bodies it switches to Barnes-Hut octree (theta=0.7) for O(N log N) instead of O(N²). Click to spawn — left for planet, right for star, ctrl-click for black hole (warps spacetime via screen-space lensing distortion shader). Bodies have mass, velocity, color trails (~200 positions). Mass thresholds promote planets > 60 to stars, > 800 to black holes. Time scale slider from 0.01x to 1,000,000x. Press R for random 100-body cluster, O for stable orbit demo (star + planet + moon hierarchy), 1 for a 1000-body galactic disk that develops spiral arm density waves over time.
Why this is mind-blowing
Universe Sandbox is a $30 commercial game. The N-body gravity simulator at its heart is here in 1285 lines from one paragraph. The galactic disk slowly winding into spirals at 100,000x time scale is the kind of thing you can stare at for an hour.
3D N-body gravity sandbox in three.js. Real Newtonian gravity between
every pair of bodies (Barnes-Hut octree above 100 bodies). Click to
spawn — left for planet, right for star, ctrl-click for black hole
(warps spacetime visually). Time scale slider 1x to 1,000,000x.
Bodies have mass, velocity, color trails.
Paste this into Claude, Cursor, or Copilot. Change one thing that matters to you.
What I learned shipping it
- Velocity Verlet integration (vs explicit Euler) is the difference between energy-conserving stable orbits and bodies spiraling out to infinity. Symplectic integrators are the right tool for gravity.
- Mass thresholds that promote planets → stars → black holes give the simulation a story. Random clusters collapse, exchange momentum, eject light bodies, form binary cores.
- Screen-space lensing distortion shader on black holes is the visual hook that turns 'big mass' into 'spacetime is bent.'