November 10, 2026

Gravity Lab: An N-Body Simulator in the Browser, From Newton’s Moon to the Three-Body Figure-Eight

Long-exposure night sky with a bright binary star pair at the centre

Two bodies under gravity are a solved problem; Newton solved it, and every orbit in that case is an ellipse, a parabola or a hyperbola that can be written on one line. Add a third body and the line disappears. There is no general closed-form solution to the three-body problem, and the system is chaotic, so the only way to know where three or more masses will be is to compute their paths one small step at a time. Gravity Lab does that in the browser for anything from two bodies to six hundred. Drag on empty space to launch a mass, load the inner solar system or a binary star, run the one three-body orbit that closes on itself, and turn Newton’s constant up, down or negative to see what gravity would do if it were different.

Video: TED-Ed, “Newton’s three-body problem explained,” by Fabio Pacucci

What the simulator computes

Every pair, every step, and a tree when the crowd gets large

Each body carries a position, a velocity and a mass. On each step the simulator sums the gravitational pull on every body from every other body, then advances positions and velocities with a velocity-Verlet integrator, a scheme that conserves energy far better than the obvious one and is standard in orbital and molecular dynamics. The energy readout in the header is the total kinetic plus potential energy of the system; for the figure-eight it holds to four decimal places over hundreds of orbits, which is the check that the arithmetic is sound.

Summing every pair is an O(n²) job: doubling the bodies quadruples the work. Above 150 bodies the simulator switches to the Barnes–Hut method, named after Joshua Barnes and Piet Hut, who published it in Nature in December 1986. The space is divided into a quadtree, and distant groups of bodies are treated as a single large particle centered at the cell’s center of mass. The cost falls to O(n log n) compared to a direct-sum algorithm which would be O(n²), which is the difference between a 600-body disc galaxy running at sixty frames a second and one running at six. An opening-angle parameter decides how far away a group must be before it collapses to one point; the simulator uses 0.6, a conventional compromise between speed and accuracy.

Two bodies that touch merge, conserving mass and momentum, unless the toggle is off. A small softening length keeps close encounters from producing infinite accelerations, the same trick astrophysical codes use.

An old leather-bound book open to hand-drawn geometric diagrams by candlelight

The problem Newton could not finish

The Moon, and two centuries of special cases

Newton’s Principia dates in substance from 1687. Having solved the two-body problem, he turned to whether the Earth, Moon and Sun could be stable together over the long run, and applied the results to lunar theory. He did not get a general answer, and neither did anyone after him. What the eighteenth century produced instead were special cases. Euler found three families of periodic solutions in which the three masses are collinear in 1767, and Lagrange found a family in which the three masses form an equilateral triangle in 1772.

The general case closed in 1889, when Poincaré showed there are no more analytic conserved quantities beyond the ones already known. Without enough conserved quantities to pin the motion down, the system is chaotic, and the three-body problem became the first place sensitive dependence on initial conditions was seen. Load the “Random cluster” preset twice and the two runs will not match; there is no bug, only chaos.

Three suspended steel balls with a long-exposure figure-eight light trail

The figure-eight

An orbit found on a computer and proven on paper

Chaos does not forbid order. In 1993 Cris Moore found a zero angular momentum solution with three equal masses moving around a figure-eight shape, each body chasing the next around the same lemniscate. Alain Chenciner and Richard Montgomery proved its formal existence in 2000. The simulator’s “Three-body figure-eight” preset uses their published starting positions and velocities, and the three bodies trace the eight indefinitely with the energy readout unchanged.

It is not alone. Šuvakov and Dmitrašinović discovered 13 new families of solutions for the equal-mass, zero-angular-momentum case in 2013, and Li and Liao reported 669 periodic orbits in 2017. All of them are fragile. Nudge a body in the figure-eight by a hair and the orbit falls apart within a few loops, which is the three-body version of the lesson in x9m8’s double pendulum simulator: the special solutions exist, but nothing in nature can hold them.

An antique brass orrery with planets on arms around a central sun

The presets, and what each one shows

Kepler, binaries, collapse and a disc

  • Inner solar system. A unit-mass sun and four planets at Mercury, Venus, Earth and Mars distances, each given the circular-orbit speed √(GM/r). Mercury completes roughly four laps for every one of Earth’s, Kepler’s third law made visible. The planets’ masses are set to their real fractions of the Sun’s, which is why they do not disturb each other noticeably.
  • Binary star with planet. Two stars of unequal mass orbit their shared centre of mass, and a distant planet orbits both. Turn on “follow centre of mass” to see the binary from its own frame.
  • Earth and Moon. The real 81-to-1 mass ratio, distance exaggerated. The Earth wobbles.
  • Random cluster. Two hundred bodies with small random velocities. Watch it collapse, form a core, fling a few members out to infinity and settle. This is the physics of globular clusters at toy scale.
  • Disc galaxy. Six hundred light bodies in near-circular orbits around a central mass, the Barnes–Hut tree doing the work.

What the G slider is for

Gravity stronger, weaker, and reversed

Newton’s constant is not something anyone can change, which makes it worth changing in a simulator. The G slider is logarithmic: the centre is the true value, the right end is thirty times stronger, the left end a tenth. Push it past the midpoint on a running solar system and the planets fall inward and merge; pull it down and they drift away on hyperbolas. Push it negative and gravity repels, every body fleeing every other. The orbits that exist are the ones that happen to balance at the real value, and the slider shows how narrow that window is.

The “New mass” slider sets the mass of the next body launched, from a hundredth of a unit to a thousand. Launch a thousand-unit body into the solar system preset and it becomes the sun.

Further reading

The three-body problem is also the title of Liu Cixin’s novel, in which a civilisation on a planet orbiting the ternary star system of Alpha Centauri lurches between Stable Eras and Chaotic Eras because its suns’ motions cannot be predicted. The Three-Body Problem, translated by Ken Liu, won the 2015 Hugo Award for Best Novel, and the physics underneath it is the physics in this simulator. See it on Amazon (affiliate link).

The Three-Body Problem by Cixin Liu, translated by Ken Liu. The 2015 Hugo winner, in which a civilisation’s survival hinges on an orbit nobody can predict. See it on Amazon (affiliate link)

As an Amazon Associate I earn from qualifying purchases. A commission may be earned if you buy through this link, at no extra cost to you.

Gravity Lab is a single page at apps.x9m8.com/gravity-lab, with the source on GitHub. Starting parameters can be set in the URL (?preset=figure8, ?preset=galaxy&trails=0), so any configuration can be linked.

Sources

394 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

2 Responses to “Gravity Lab: An N-Body Simulator in the Browser, From Newton’s Moon to the Three-Body Figure-Eight”

Leave a Reply

Your email address will not be published. Required fields are marked *

You Missed