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.
Gravity Simulator Sandbox — Launch Your Own Planets and Stars
The last scene is a free gravity simulator sandbox. It starts with the Sun and the Earth, and you add the rest. Choose Sandbox, pick the body to launch under Body to launch, then press on empty space and drag. The four bodies are the Asteroid (a test mass 600 km across, too small to pull on anything), the Earth (one Earth mass), the Jupiter (about 318 Earth masses) and the Red dwarf (0.3 solar masses). The arrow is the launch velocity: its tip is where the body would be after 60 days with no gravity, and its label gives the speed in km/s. Let go and the body joins the same N-body calculation as everything else, so it pulls on the Sun and the Earth as they pull on it.
What happens next depends on the launch speed. From 1 AU, at right angles to the line to the Sun, a body needs 29.78 km/s for a circle and 42.12 km/s to escape. Slower than circular speed it falls inward on an ellipse; between the two it swings out on a wider ellipse; at escape speed or faster it leaves for good. The table gives the results the sandbox computes for an asteroid launched from the far side of the Sun:
| Launch speed (km/s) | Orbit | Nearest the Sun | Farthest from the Sun | Period / fate |
|---|---|---|---|---|
| 15 | Ellipse inward | 0.15 AU | 1.00 AU | 158 days |
| 20 | Ellipse inward | 0.29 AU | 1.00 AU | 189 days |
| 25 | Ellipse inward | 0.54 AU | 1.00 AU | 248 days |
| 29.78 (circular speed) | Circle | 1.00 AU | 1.00 AU | 1.00 years |
| 35 | Ellipse outward | 1.00 AU | 2.23 AU | 2.05 years |
| 40 | Ellipse outward | 1.00 AU | 9.18 AU | 11.49 years |
| 42.12 (escape speed) | Parabola | 1.00 AU | none | never returns |
| 50 | Hyperbola | 1.00 AU | none | never returns; leaves at 26.9 km/s |
Mergers. Bodies that touch merge into one. Their masses add, their total momentum is kept, and so is their volume (both are taken to have the same density), so a planet that hits the Sun simply joins it and the Sun carries on with the combined momentum. Momentum is conserved through a merger but energy is not: the collision turns kinetic energy into heat, which the simulator does not track. An asteroid that hits a planet or star is absorbed.
Three things to try. Drag a red dwarf past the Sun and watch the Earth’s orbit get disturbed, or the Earth taken away. Launch a planet with a short arrow straight at the Sun: it falls in and merges. Launch anything at 42 km/s from 1 AU: that is escape speed, and it never comes back.
What the sandbox does not model. Every body moves in one plane, so the sandbox is two-dimensional. Gravity is Newton’s, with no relativity. Bodies merge rather than break apart, so there are no tides, debris or fragments.
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.
Is there a free gravity simulator sandbox online?
Yes. This gravity simulator sandbox is free and runs in your browser with no download or sign-up. It starts with the Sun and the Earth. Pick an asteroid, an Earth, a Jupiter or a red dwarf and drag on empty space to launch it: the arrow sets its speed and direction. Every body pulls on every other by Newton's law of gravitation, so you can put a planet into orbit, throw it out of the Solar System at 42.1 km/s from 1 AU, or send a star past the Sun and watch it disturb the Earth's orbit. Bodies that touch merge and keep their total momentum. The sandbox is two-dimensional and uses Newtonian gravity.
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