HomeArticlesSpace & Astronomy

N-Body Gravity: RK4 Integration, Binary Stars & Orbital Resonance

Two bodies orbiting each other has a clean formula. Add a third and the equations stop having a general solution — so simulations step gravity forward numerically instead, one tiny slice of time at a time.

mysimulator teamUpdated July 2026≈ 8 min read▶ Open the simulation

Every body pulls on every other body

Newton's law of universal gravitation, F = G·m₁m₂/r², describes the pull between any two masses precisely. The trouble starts once you add a third body: now each object is pulled simultaneously by every other one, and those pulls all change as the bodies move. Two bodies alone can be solved exactly, producing tidy ellipses — but the moment you're at three or more, the system becomes the notorious three-body problem, which has no general closed-form solution. An N-body simulation sidesteps this entirely: rather than solving the equations algebraically, it computes every pairwise gravitational force at each instant and steps every body's position and velocity forward by a tiny timestep, repeating thousands of times per second.

RK4: stepping gravity forward without it falling apart

The simplest way to advance a simulation is Euler integration: compute the acceleration now, apply it for one timestep, move on. It's fast but leaky — errors accumulate every step, and over many orbital periods a naively-integrated planet will visibly spiral inward or fly outward even though no real force is pulling it that way. The fix used here is fourth-order Runge-Kutta (RK4) integration, which samples the acceleration at four points inside each timestep — the start, two midpoint estimates, and the end — and blends them into a single, far more accurate step:

k1 = f(t,        y)
k2 = f(t + dt/2,  y + dt/2·k1)
k3 = f(t + dt/2,  y + dt/2·k2)
k4 = f(t + dt,    y + dt·k3)
y(t+dt) = y(t) + (dt/6)·(k1 + 2k2 + 2k3 + k4)

RK4's error per step shrinks with the fifth power of the timestep, versus only the second power for simple Euler stepping — so it holds orbits stable through thousands of revolutions at a timestep large enough to run smoothly in a browser. A small softening term is also added to the denominator of the force calculation to prevent the force (and the timestep) from blowing up when two bodies pass extremely close together.

live demo · orbits evolving under mutual gravity● LIVE

Binary stars and the figure-8 orbit

Two roughly equal-mass stars don't have one star orbiting the other — both orbit their shared centre of mass, called the barycentre, tracing mirrored ellipses. More exotic still is the figure-eight three-body orbit: three equal masses chasing each other along a single lemniscate path, discovered numerically in 1993 and only proven to exist rigorously afterward. It's a periodic, stable solution to the three-body problem — a rare pocket of order inside a system that is chaotic in general, and a good stress test for how well an integrator conserves energy over time.

Orbital resonance: when periods lock together

Orbital resonance happens when two bodies' orbital periods form a simple integer ratio — 2:1, 3:2, 1:1 — so their closest approaches keep happening at the same relative position every few orbits instead of drifting randomly. Resonance can stabilise a system: Jupiter's Trojan asteroids sit locked 60° ahead and behind the planet in a 1:1 resonance for the same reason a marble stays in a valley. It can also destabilise one: repeated tugs at the same orbital phase carved the Kirkwood gaps in the asteroid belt, at the 3:1 and 2:1 resonances with Jupiter, by slowly pumping up eccentricity until asteroids were ejected. Simulating a galaxy collision or a multi-planet system numerically is often the only way to see which resonances are stable and which are ticking time bombs.

Frequently asked questions

Why can't the N-body problem be solved with a single formula for N > 2?

For exactly two bodies, Newton's law of gravitation can be solved exactly, producing clean elliptical orbits. Add a third body and every object pulls on every other simultaneously, producing a system of coupled differential equations with no general closed-form solution — this is the classic three-body problem. Beyond three bodies the same applies: N bodies require numerically stepping the equations of motion forward in tiny time increments rather than solving them algebraically.

What is RK4 integration and why is it used for gravity simulations?

The fourth-order Runge-Kutta method (RK4) advances a simulation by sampling the acceleration at four points within each timestep — the start, two midpoint estimates, and the end — and combining them in a weighted average. This makes its error shrink far faster than simple Euler integration as the timestep shrinks, which matters enormously for gravity: naive integration slowly leaks or gains orbital energy, causing orbits to spiral in or fly apart over many revolutions, while RK4 keeps orbits stable for thousands of periods with a reasonably sized timestep.

What is orbital resonance and why does it stabilise some multi-body systems?

Orbital resonance occurs when two orbiting bodies have periods in a simple integer ratio, such as 2:1 or 3:2, so their close approaches repeat at the same points in each orbit rather than drifting randomly. This regularity can either stabilise a configuration, as with Jupiter's Trojan asteroids locked 60° ahead and behind in a 1:1 resonance, or destabilise it by compounding a gravitational tug at the same phase every cycle, as happens in the Kirkwood gaps of the asteroid belt.

Try it live

Everything above runs in your browser — open N-Body Gravitational Simulation, load the binary-star or figure-8 preset, and watch RK4 integration keep genuinely chaotic orbits stable in real time. Nothing is installed, nothing is uploaded.

▶ Open N-Body Gravitational Simulation

What did you find?

Add reproduction steps (optional)