This simulation needs WebAssembly, and it could not start in this browser.
The image is a frame from a real run. Check that JavaScript is on, or try another browser.
The two-body problem is the one case of Newtonian gravity that can be solved exactly. Newton showed in the Principia (1687) that two masses attracting each other with an inverse-square force move on conic sections, which explained Kepler’s three laws of planetary motion. Every gravitational simulation with more bodies starts from this case.
Here a star and a planet orbit each other in units where and the star has mass 1. Because the exact solution is known, the simulation is also a clean test of the integrator.
What you see
The yellow body is the star, the blue one the planet. Both start on the horizontal axis at periapsis, their closest approach, with the planet on the right, and both orbit their common center of mass, which stays at rest. The heavier the planet, the larger the star’s own ellipse. Trails in the bodies’ colors show the recent path, and arrows on a grid show the gravitational field (switch them off with Force field).
The view is fitted to the orbit. From the current positions and velocities the simulation computes the ellipse of each body, and the view is centered on the box that holds both and just wide enough for it, so an elongated orbit is drawn smaller but always whole. With Follow center of mass the view is centered on the center of mass instead. Without a preset, the simulation starts with and mass ratio 0.001.
Governing equation
Each body is pulled toward the other by softened Newtonian gravity:
For the separation obeys with . Its solution is an ellipse with the center of mass at one focus:
The simulation sets the starting separation to the periapsis distance and gives the bodies the relative speed that the vis-viva equation requires there:
The period follows from Kepler’s third law, . Conservation of angular momentum makes the line between the bodies sweep equal areas in equal times, so they move fastest at periapsis and slowest at apoapsis, .
Presets
Earth-Sun (e=0.017): Earth’s real eccentricity and mass ratio, . The orbit is almost a circle, and the star’s motion is far too small to see.
Comet orbit (e=0.85): mass ratio 0.001. The planet races through periapsis, then climbs to an apoapsis 12 times farther out, at the far end of the view. One orbit takes about 17 times as long as a circular orbit at the periapsis distance.
Equal masses (binary star): mass ratio 1 and . The two bodies trace identical ellipses, mirrored through the center of mass halfway between them.
Circular orbit (e=0): mass ratio 0.001. The separation and both speeds stay constant.
Parameters
Time step dt: the integration step, 0.0005 to 0.04 (default 0.005), on a logarithmic slider. Steps per frame (1 to 32, default 8) sets how many steps make up one drawn frame.
Eccentricity e: the shape of the orbit, from 0 (circle) to 0.98 (default 0.5).
Mass ratio: the planet’s mass in units of the star’s, 0.0001 to 1 (default 0.001).
Separation: the starting distance, which is the periapsis, 0.5 to 5 (default 1). The view scales with it.
Integrator: leapfrog (velocity Verlet) by default. It is symplectic, so the energy error oscillates instead of growing. yoshida4 is a fourth-order symplectic method; rk4 is more accurate per step but drifts slowly; rk2 and euler drift quickly, and with euler the planet spirals outward.
Softening ε: the length above, default 0.001.
The remaining controls change only the drawing: trails, trail length, brightness and persistence, labels, the center-of-mass marker, a view locked to the center of mass, velocity arrows, and the force-field and equipotential overlays.