🪐 N-Body Gravitational Simulation

Every pair of bodies attracts each other with force F = G·m₁·m₂/r² (Newton's law of universal gravitation). The equations of motion are integrated with RK4. The general three-body problem has no closed-form solution and exhibits sensitive dependence on initial conditions (chaos). However, special stable configurations exist — such as the figure-8 orbit and Lagrange-point equilibria. Click the canvas to add a new body; use preset configurations to explore classic N-body scenarios. 🇺🇦 Українська

Presets

Bodies
Time
Kinetic E
Potential E

Numerical methods and chaos

RK4 integrates the coupled ODEs: ẍᵢ = Σⱼ G·mⱼ·(xⱼ−xᵢ)/(|rᵢⱼ|²+ε²)^(3/2). A softening parameter ε prevents singularities at close encounters. Total energy (KE + PE) and angular momentum are approximate conserved quantities — monitor them to judge integration quality. The figure-8 three-body orbit (Chenciner & Montgomery, 2000) is exact only with a specific mass ratio and velocities; small perturbations eventually cause it to break apart — you can see chaos in action.

About this simulation

This simulation solves the classical N-body problem: every pair of point masses attracts the others by Newton's law F = G·m₁·m₂/r², and the pairwise accelerations are summed over all bodies each frame. The equations of motion are advanced with fourth-order Runge–Kutta (RK4) integration rather than simple Euler stepping, giving far better accuracy for the same time step. A softening length ε is added inside the distance term so the force never blows up when two bodies pass very close together. Kinetic and potential energy are tracked live as a rough check on integration quality — for three or more bodies there is no general closed-form solution, so tiny changes in starting positions or velocities can send the system down completely different, chaotic paths.

🔬 What it Shows

Four preset configurations: a five-body Solar system, a Binary pair orbited by a light third body, the exact three-body Figure-8 orbit, and a Random cloud of bodies. Trails reveal orbit shapes, resonances, close encounters and slingshot (gravity-assist) deflections as the RK4 integrator advances every body's position and velocity each frame.

🎮 How to Use It

Click the canvas to drop in a new body with the mass set by the New body mass slider. Adjust G to strengthen or weaken gravity, dt to trade integration speed for accuracy, Trail length to see more or less orbital history, and Softening ε to control how gently close encounters are smoothed. Pause to freeze the scene or Clear to start empty.

💡 Did You Know?

The figure-8 three-body orbit — where three equal masses endlessly chase one another around a single lemniscate-shaped path — was only numerically discovered in 1993 and rigorously proven to exist by Chenciner and Montgomery in 2000. It requires an exact mass ratio and velocity configuration; the version here is only an approximation, so watch how quickly it drifts apart under RK4 integration and rounding.

Frequently asked questions

Why does the N-body problem have no general solution?

For two bodies, Newton's law of gravitation yields exact closed-form orbits (Kepler's ellipses). Once a third body is added, the system of coupled differential equations generically has no algebraic solution — Poincaré proved in the 1890s that the three-body problem is non-integrable in general. This simulator sidesteps that by integrating the equations of motion step by step numerically instead of solving them analytically.

Why use RK4 integration instead of simple Euler stepping?

Euler's method updates position and velocity using only the acceleration at the start of each step, which accumulates error quickly and leaks energy. Fourth-order Runge–Kutta (RK4) instead samples the acceleration at four points across the step (the k1–k4 stages) and combines them in a weighted average, giving error that shrinks much faster as the time step dt is reduced — far better long-term orbit fidelity for the same computational cost.

What does the softening parameter ε actually do?

Newtonian gravity's 1/r² force diverges as two bodies approach r = 0, which would send the simulation's numbers to infinity during a close encounter. Softening replaces r² with r²+ε² inside the acceleration formula, capping the maximum force at close range. Larger ε makes near-collisions gentler but less physically realistic; smaller ε is more accurate but risks numerical blow-ups at high speed or large dt.

What is the figure-8 orbit and why does it eventually break apart?

The figure-8 is a periodic solution where three equal masses chase each other around a single figure-eight-shaped curve, discovered numerically by Cristopher Moore in 1993 and proven rigorously by Chenciner and Montgomery in 2000. It exists only for a precise combination of starting positions, velocities and equal masses. Because this simulator uses a discrete time step and finite-precision arithmetic, tiny deviations from that exact configuration grow exponentially — a hallmark of chaos — so the orbit visibly drifts apart after enough revolutions.

How is the gravitational force between every pair of bodies calculated?

Each frame, the simulator loops over every unique pair (i, j) and computes the Newtonian force F = G·mⁱ·m⁅/r² along the line joining them, accumulating the resulting acceleration aⁱ = Σ⁅ G·m⁅·(r⁅−rⁱ)/(|r⁅−rⁱ|²+ε²)^(3/2) on each body. This is the direct, exact pairwise sum (an O(n²) calculation) rather than an approximation like Barnes–Hut, so it is accurate but only practical for the modest number of bodies used here.