Gravity Simulator: Orbits, Kepler’s Laws and the N-Body Problem
This gravity simulator computes Newton’s law of gravitation between every pair of bodies and integrates their motion step by step. The planets are not animated along drawn ellipses: the ellipses appear because the inverse-square force produces them. That makes it an orbit simulator you can trust for real numbers. Earth’s year comes out within a few hours of the observed 365.26 days, Mars’s at 1.881 years and the Moon’s month at 27.3 days.
Eight scenes cover a physics or astronomy course: a single Kepler orbit, the Solar System from its real 2000 positions, the Earth and Moon, binary stars, Jupiter’s Lagrange points, the three-body problem, a gravity assist and a free sandbox.
Kepler’s three laws, measured
First law: planets move on ellipses with the Sun at one focus; perihelion is a(1 − e) and aphelion a(1 + e). Second law: the Sun–planet line sweeps equal areas in equal times, because gravity points at the Sun and cannot change the angular momentum. The Kepler scene draws the wedge swept in each twelfth of an orbit and prints its area. Third law: T² is proportional to a³; in years and AU, T² = a³. The Solar System scene times each planet’s first orbit and plots it:
| Planet | a (AU) | e | a1.5 (yr) | Observed period (yr) |
|---|---|---|---|---|
| Mercury | 0.3871 | 0.2056 | 0.2408 | 0.2408 |
| Venus | 0.7233 | 0.0068 | 0.6152 | 0.6152 |
| Earth | 1.0000 | 0.0167 | 1.0000 | 1.0000 |
| Mars | 1.5237 | 0.0934 | 1.8808 | 1.8808 |
| Jupiter | 5.2029 | 0.0484 | 11.868 | 11.863 |
| Saturn | 9.5367 | 0.0539 | 29.451 | 29.447 |
| Uranus | 19.189 | 0.0473 | 84.06 | 84.02 |
| Neptune | 30.070 | 0.0086 | 164.89 | 164.79 |
Elements: JPL mean elements for J2000. The small gaps for the giant planets are real: Newton’s form T² = 4π²a³/G(M + m) includes the planet’s own mass, and Jupiter is a thousandth of the Sun.
Orbital speed, energy and escape
A circular orbit needs v = √(GM/r): 29.78 km/s for the Earth, 7.66 km/s for the International Space Station. On an ellipse the speed follows the vis-viva equation v² = GM(2/r − 1/a). Mercury moves at 58.98 km/s at perihelion and 38.86 km/s at aphelion. The total energy E = −GMm/2a is negative for every bound orbit. At zero the body escapes, at vesc = √(2GM/r), √2 times the circular speed. That is 11.19 km/s from the Earth’s surface and 42.1 km/s from the Sun at 1 AU. The energy chart under the Kepler scene shows kinetic and potential energy trading places while their sum stays flat.
Barycentres, binary stars and exoplanets
Two bodies orbit their common centre of mass. The Earth–Moon barycentre is 4,670 km from Earth’s centre, inside the Earth. The Sun–Jupiter barycentre is 742,400 km from the Sun’s centre, just outside its surface. For a binary star, Kepler’s law gives the total mass: M1 + M2 = a³/T² in AU, years and solar masses. Alpha Centauri A and B, 23.4 AU apart with a 79.9-year period, come to 2.0 M☉. Seen edge-on, each star’s motion towards and away from us shows up as a Doppler shift. The radial-velocity method finds planets this way: Jupiter makes the Sun wobble at 12.5 m/s.
Lagrange points and the three-body problem
Add a third body and there is no general formula. In the frame turning with two orbiting masses, five Lagrange points let a small body keep station. L1, L2 and L3 lie on the line and are unstable. L4 and L5 sit 60° ahead and behind and are stable when the mass ratio exceeds 24.96 (Routh’s criterion). That is why Jupiter’s L4 and L5 hold thousands of Trojan asteroids, and why the James Webb Space Telescope at the Sun–Earth L2, about 1.5 million km out, needs station-keeping. The figure-eight orbit, three equal masses chasing each other round one curve, is one of the rare periodic three-body solutions. Burrau’s Pythagorean problem shows the usual fate: chaotic close encounters until one body is ejected.
Gravity assists and transfers
In a planet’s frame a flyby is elastic: the probe leaves at the speed it arrived, only turned. Relative to the Sun the planet’s own motion is added back, so the probe can gain or lose a lot of speed. Voyager 2 used Jupiter, Saturn, Uranus and Neptune in turn. The simulator’s default launch passes 3.6 Jupiter radii from Jupiter’s centre and leaves the Solar System at 7.4 km/s. Aim 0.2° differently and the probe hits Jupiter. The cheapest way between two circular orbits is a Hohmann transfer: Earth to Mars needs 2.94 km/s and takes 259 days.
Who uses this simulator?
High-school students on gravitation and circular motion (including AP Physics 1 and AP Physics C: Mechanics), introductory astronomy classes running Kepler’s-law labs, and first-year university mechanics students meeting orbital energy and the two-body problem. Teachers project it to show equal areas, barycentres or a slingshot as they happen; it runs in any browser, free, with no sign-up.
Frequently asked questions
How does a gravity simulator work?
It applies Newton's law of gravitation to every pair of bodies, a = G·m·r̂/r², adds up the pulls on each body, and moves all of them forward by a small time step, over and over. This one uses a 4th-order symplectic (Yoshida) integrator with a step that shrinks during close encounters, so total energy stays constant to about one part in ten billion over thousands of orbits.
Why do planets move faster when they are closer to the Sun?
Gravity points at the Sun, so it exerts no torque about it and the planet's angular momentum r·v⊥ stays constant. Where r is small, v must be large. That is Kepler's second law: the line from the Sun to the planet sweeps equal areas in equal times. Mercury moves at 59 km/s at perihelion and 39 km/s at aphelion.
What is Kepler's third law in simple terms?
The square of a planet's orbital period is proportional to the cube of its average distance from the Sun. In years and astronomical units, T² = a³: Mars at 1.524 AU takes 1.524^1.5 = 1.88 years. Newton's form, T² = 4π²a³/G(M + m), lets astronomers weigh stars and planets from their orbits.
What are Lagrange points?
They are five places where a small body can stay fixed relative to two larger bodies orbiting each other: L1, L2 and L3 on the line through them, and L4 and L5 at the corners of equilateral triangles. L1–L3 are unstable. L4 and L5 are stable when the larger mass is more than about 25 times the smaller, which is why Jupiter shares its orbit with thousands of Trojan asteroids.
How does a gravity assist speed up a spacecraft?
Relative to the planet, the spacecraft leaves at the same speed it arrived, only turned. But the planet is moving round the Sun, so after adding the planet's velocity back the spacecraft's speed relative to the Sun can be much larger. A pass behind the planet takes a little of its orbital energy; a pass in front gives some back.
Can the three-body problem be solved?
Not with a general formula. Two bodies always move on conic sections, but three generally move chaotically, so the motion has to be computed step by step. A few special periodic solutions exist, such as the figure-eight orbit of three equal masses found by Moore and proven by Chenciner and Montgomery in 2000.
Explore Related Simulators
To launch from a planet’s surface and find the speed that escapes it, open the Escape Velocity simulator (Newton’s cannonball). The centripetal force that bends every orbit is worked through in the Centripetal Force & Circular Motion simulator. Gravity near the ground, g = 9.81 m/s², is in Free Fall & Gravity, and momentum in collisions, which the sandbox’s mergers conserve, is in Collision & Momentum.
Updated