Popular right now

Fun

Gravity Simulator

Drag to fling planets and stars into orbit, or load a figure-8 three-body dance, a binary star or two colliding galaxies, and watch Newtonian gravity at work.

Ready to useFree · No sign-up
0
Bodies
0.00
Time
0
Energy drift
Direct sum
Forces

Three equal masses chasing each other round one figure-eight curve (Chenciner & Montgomery, 2000). Initial conditions from Simó’s numerical solution; period ≈ 6.326.

Drag on the space to fling a new body (the arrow is its starting velocity); tap to drop one at rest. Scroll or use + and − to zoom. Trails show for up to 64 bodies; merging works up to 300.

Play and explore in your browser. No account needed.

Advertisement

Frequently asked questions

How does the gravity simulator calculate the orbits?

Every body pulls on every other body with Newton’s inverse-square law, in units where the gravitational constant is 1. Positions and velocities are advanced with the velocity Verlet method, a symplectic integrator: its energy error stays bounded and oscillates instead of growing steadily, so orbits do not spiral in or out over thousands of turns. Each preset uses a fixed time step small enough for its closest encounters.

What is the figure-8 three-body orbit?

It is a periodic solution of the three-body problem in which three equal masses chase each other along a single figure-eight-shaped curve. Cris Moore found it numerically in 1993, and Alain Chenciner and Richard Montgomery proved it exists in 2000 (Annals of Mathematics 152, 881–901). The preset uses the widely quoted initial conditions computed by Carles Simó, with a period of about 6.326 time units; in the simulator’s tests the bodies return to their starting positions after one period to within a few ten-thousandths of a unit.

What does the energy drift number mean?

Total energy — kinetic plus gravitational potential — should stay constant in an isolated system, so its relative change since the last reset shows the numerical error. Over 60 time units the figure-8 stays within about one part in a million, the planetary system within one part in a billion and the binary star within one part in 100,000. Adding, merging or removing bodies changes the real energy, so the baseline resets whenever that happens. In the galaxy collision the tree approximation and the time step add a slowly growing error, under 1% over 10,000 steps.

What is softening, and why do bodies pass through each other?

With a pure inverse-square force, two bodies that pass very close experience enormous accelerations that a fixed time step cannot follow. Presets therefore use Plummer softening, which replaces the distance d with √(d² + ε²) for a small length ε, capping the force at close range. The figure-8 preset uses no softening at all. Bodies are points for the force calculation, so they pass through each other unless Merge on collision is on.

How can it simulate 2,000 stars at once?

Summing every pair of 2,000 bodies needs about two million force calculations per step. For large systems the simulator switches to the Barnes–Hut algorithm (Barnes & Hut, Nature, 1986): it groups bodies into a quadtree and treats a distant group as a single mass at its centre of mass, plus a quadrupole correction for its shape. With an opening angle of 0.6 this cuts the work to a few hundred interactions per body, with a typical force error of about 0.2%.

Is this how real astronomers simulate galaxies?

The ingredients are the same — N-body gravity, softening and tree codes — but research simulations use millions to billions of particles, dark matter halos, gas physics and three dimensions. This simulator is two-dimensional and uses a few thousand particles, which is enough to show the tidal bridges and tails that form when two disc galaxies pass close to each other.

Advertisement

About this tool

This gravity simulator lets you play with Newton’s law of universal gravitation directly in your browser. Every body attracts every other with a force proportional to their masses and inversely proportional to the square of the distance between them, and the simulator follows the resulting motion step by step — the classic N-body problem. There is no closed-form solution for three or more bodies, which is exactly why numerical simulation is how orbits, star clusters and galaxies are studied.

The integrator is velocity Verlet, the same second-order symplectic scheme used widely in orbital and molecular dynamics. Halving the time step cuts its energy error by four, and because it preserves the geometric structure of Hamiltonian motion, energy errors oscillate rather than accumulate; the simulator’s tests check this over two hundred eccentric Kepler orbits and ten periods of the figure-8. Momentum and angular momentum are conserved to rounding error.

The four presets show different sides of gravity. The figure-8 is Chenciner and Montgomery’s choreography, started from Simó’s initial conditions. The binary star shows two suns on eccentric orbits with a small planet circling both, like the planets later found around binary stars by the Kepler telescope. The planetary system obeys Kepler’s third law, and the galaxy collision throws two rotating discs of 1,000 stars at each other, computed with a Barnes–Hut quadtree including quadrupole terms.

You can add your own bodies at any time by dragging to set their launch velocity, merge colliding bodies into one while conserving mass and momentum, and watch the energy drift to judge how trustworthy a run is. Try flinging a small body past a binary star to see a gravitational slingshot, or add a heavy star to the planetary system and watch the orbits come apart.

Related tools