N-Body Gravity (2D)

Newtonian gravity for 2 to 200 bodies: choose the integrator and watch energy drift, launch bodies, explore chaos and clusters.

Presets

Playback

Simulation

View and interaction

Layers

Model check

The science

Newton's law of gravitation

Every body attracts every other: ai = G Σj≠i mj (rj - ri) / (|rj - ri|² + ε²)3/2. The page sums all N(N-1)/2 pairs directly each step, so cost grows as N². The softening length ε (0 by default for small systems) replaces the singular 1/r² force at very small separations; it is how cluster codes survive close encounters, and the potential -G mimj/sqrt(r²+ε²) is changed consistently so energy is still conserved.

Conserved quantities

Total energy E = K + U, total momentum P = Σ m v (so the centre of mass moves in a straight line) and angular momentum L = Σ m r × v are conserved by the exact equations. How well a numerical method keeps them is the quality measure shown in the graphs. The virial ratio 2K/|U| equals 1 for a bound system in equilibrium.

Integrators

Leapfrog (kick-drift-kick) is second order, time-reversible and symplectic: its energy error oscillates instead of growing, which is why it is the workhorse for orbits. Yoshida 4th order composes three leapfrog steps with coefficients w1 = 1/(2-21/3), w0 = -21/3 w1 for fourth-order accuracy and the same symplectic structure. Runge-Kutta 4 is very accurate per step but not symplectic: over very long runs energy drifts slowly. Explicit Euler is first order and adds energy every step: planets spiral outwards, which you can watch by selecting it. Use "Reverse time" to see that leapfrog retraces its path almost exactly.

Famous solutions

The figure-eight (Chenciner and Montgomery, 2000) is a periodic orbit of three equal masses chasing each other along one curve with period T = 6.32591398 in units G = m = 1. The Lagrange triangle rotates rigidly with ω² = 3Gm/s³. The Pythagorean 3-4-5 problem starts three bodies at rest at the corners of a 3-4-5 triangle and ends in a chaotic ejection: tiny changes in the time step change the outcome, which is a sign of the sensitivity of the three-body problem (it has no general closed-form solution). The solar-system preset uses AU, years and solar masses, where G = 4π².

Model checks (run in the page)

1. A circular two-body orbit must have the Kepler period T = 2π sqrt(a³/(G(M+m))). 2. An eccentric orbit (e = 0.6) must keep energy and angular momentum and have perihelion a(1-e). 3. The figure-eight must return to its starting positions after one period, with zero total momentum.

Honest simplifications

Bodies are point masses (no collisions, tides, relativity or non-gravitational forces); the fixed time step can be too coarse for very close encounters, where energy errors jump, and you can see this in the error graph. The solar-system preset ignores the other planets and the Moon, and 3D inclinations are small. Display sizes are exaggerated and not to scale. The Plummer cluster is a random statistical model, not a specific real cluster.

Energy: kinetic, potential, total
Conservation error (log10)
Distance from the centre of mass
Virial ratio 2K/|U|