Playground · Gravity Sandbox
Gravity sandbox
Your own little universe. Click and drag to fling a body — the drag sets its speed and direction. Scroll to change its mass, and hit space to drop a cloud that collapses into a solar system. Everything pulls on everything else, and bodies that touch merge.
Click & drag to launch · scroll to change mass · space for a cloud
Bodies
0
Total mass
0
Mergers
0
How it's built
Pure vanilla JavaScript on a <canvas>. Every body pulls on every other with Newton's
inverse-square law (an O(n²) sum each step), advanced with the cheap, energy-friendly
semi-implicit Euler (Euler–Cromer) method — update velocity, then move with the
new velocity. Touching bodies merge, conserving momentum
(v = (v₁m₁ + v₂m₂)/(m₁+m₂)) and taking the bigger one's colour. Trails are a cheap
fade-to-black overdraw rather than stored paths, so hundreds of bodies stay smooth.
Inspired by briandoescode's Newtonian gravity simulator (and the classic “Gravity Toy” lineage) — reimplemented from scratch in bigspark's house style. Want the rigorous, curated version? Try the Three-Body Problem lab.