Making Physics / Mechanics / N-Body Gravity
Chapter 06

N-Body Gravity

When every force comes from the other bodies, what must the simulator preserve, and what does each integrator actually do to it?

Interactive · live simulation

The stable figure-eight three-body choreography, integrated live with velocity-Verlet — watch total momentum stay pinned. Switch to a binary, or click anywhere to drop in another body.

N-Body Gravity — simulation render
fig 1. The Pythagorean three-body problem: masses 3-4-5 released from rest at the corners of a right triangle. A famously intricate dance of close encounters and ejections.

Newton's third law as a checksum

With no external force, total linear momentum is conserved exactly — and so it is by the discrete scheme too: every velocity kick sums to \sum_i F_i\,dt = 0 by Newton's third law. Even RK4 holds it to ~1e-15. That makes momentum a far sharper correctness check than energy: any drift you see is roundoff, not algorithm.

A two-body binary. With explicit Euler the orbit spirals out as energy grows, wh
fig 2. A two-body binary. With explicit Euler the orbit spirals out as energy grows, while linear and angular momentum stay flat to floating point.

Energy tells the real story

Energy and angular momentum are the interesting diagnostics. Energy drifts under explicit Euler, oscillates under semi-implicit Euler and Verlet, and slowly creeps under RK4. The binary visibly spirals outward under Euler while its momentum sits perfectly still — two invariants, two completely different verdicts on the same run.

Conserved quantities. Total momentum holds to machine precision regardless of in
fig 3. Conserved quantities. Total momentum holds to machine precision regardless of integrator; energy and angular momentum reveal the algorithm's true quality.

Softening and the COM

A softening length keeps close approaches finite. Boost every body by the same velocity and the center of mass drifts at exactly that speed while the relative motion is untouched — Galilean invariance you can read off the plot.

This chapter is drawn from the physics-lab study notes and renders. The longer write-ups and project essays live on the blog.

Read more on the blog →