MechSimulator

Gravity Simulator

Real Newtonian N-body orbits • Kepler’s laws • Solar System • binary stars • Lagrange points • three-body chaos • gravity assist — Simulate • Explore • Practice • Quiz

Mode
Display Controls

📖 Understand what you see
Σ Live equations — this scene’s numbers substituted
💡 What-if coach — predictions to test
User Guide — Gravity Simulator
1 What this gravity simulator does

A free gravity simulator that computes real Newtonian gravity between every pair of bodies and moves them step by step. Nothing is animated along a pre-drawn path: the ellipses, the equal areas, the Lagrange points and the slingshot all come out of one force law. It is written for high-school physics (including AP Physics 1 and C), introductory astronomy and first-year mechanics.

Eight scenes in four groups: Kepler’s laws (Kepler orbit, Solar System), Two-body systems (Earth & Moon, Binary stars), Three bodies (Lagrange points, Three bodies, Gravity assist) and the Sandbox. Four modes: Simulate, Explore (18 concept cards in six groups), Practice (12 problem types) and Quiz (5 questions from a pool of 20).

2 Getting started
  1. Pick a scene from the tiles above the canvas. It starts running at once.
  2. Change the set-up in the panel on the right (on a phone, below). Any change restarts the scene from its new starting positions.
  3. Read the chart under the space view and the readings under the canvas. The status line says what is happening in words.
  4. Click a body to read its mass, distance, speed and orbit (semi-major axis, eccentricity, period) about the heaviest body.

Keyboard: Space play / pause, . step, R restart, F fit the view, + / − zoom.

3 Simulate: the space view and the controls

Drag to pan, scroll the mouse wheel or pinch to zoom, or use the + / − / fit buttons at the top right of the canvas. A scale bar sits at the bottom left. Planets and moons are tiny next to their orbits, so bodies are drawn at a minimum size and the canvas says so. Collisions use the true radii.

The toolbar runs time: Play / Pause, Step, Reverse time (the clock runs backwards and the bodies retrace their paths, because Newton’s laws are time-reversible), Restart (the same set-up from the start), Reset (the scene’s default settings) and the speed in simulated time per second. Each scene picks a sensible speed; choose another at any time. If a close encounter needs more steps than one frame allows, the clock slows down and the canvas tells you; it never cuts accuracy instead.

Display Controls (top left of the canvas) switch trails, orbit guides, velocity arrows (green), gravity arrows (orange; length ∝ √force so small pulls stay visible), the barycentre marker and labels. Your choices are remembered.

4 The eight scenes
  • Kepler orbit: one planet on an ellipse round a star. Dashed guides show the ellipse, the empty focus, perihelion and aphelion; coloured wedges show the area swept in each twelfth of a period, with its value. The chart shows kinetic, potential and total energy.
  • Solar System: the Sun and four or eight planets placed from their J2000 orbital elements. As each planet completes its first orbit its measured period is plotted against its semi-major axis on log scales, on the line T² = a³.
  • Earth & Moon: the pair orbiting their barycentre, 4,670 km from Earth’s centre. The Moon-mass control moves the barycentre out of the Earth.
  • Binary stars: two stars round their centre of mass and the radial velocities an astronomer below the screen would measure. Make Star B 0.001 M☉ to see the wobble a planet causes.
  • Lagrange points: the Sun and Jupiter with test asteroids at L1–L5, viewed in the frame that turns with Jupiter (or against the fixed stars). The chart shows each asteroid’s angle ahead of Jupiter.
  • Three bodies: the figure-eight choreography of three equal suns (with an optional kick and a faint copy of the original), or Burrau’s Pythagorean problem, which ends in an ejection.
  • Gravity assist: a probe launched from 1 AU past Jupiter. Readings give the closest approach, the speed relative to Jupiter before and after (the same, apart from the Sun’s small pull) and relative to the Sun (changed).
  • Sandbox: the Sun and Earth. Drag on empty space to launch an asteroid, a planet, a Jupiter or a red dwarf; the arrow’s tip is where the body would be after 60 days without gravity. Touching bodies merge, keeping their total momentum.
5 How the physics is computed

Each body feels a = Σ G·mⱼ·(rⱼ − r)/|rⱼ − r|³ from every other body. Positions and velocities advance with Yoshida’s 4th-order symplectic integrator; the step is 1 % of the shortest free-fall or crossing time of any pair, so it shrinks automatically at a close pass. Units are astronomical units, solar masses and days, where G = k² exactly (k = 0.01720209895, the Gaussian gravitational constant). The canvas prints how well total energy is being held, typically one part in 10¹⁰.

Everything is in a plane: planets are placed from their real elements projected onto the ecliptic (their inclinations, at most 7°, are ignored). Bodies are point masses: no tides, relativity, radiation pressure or atmospheric drag. Test asteroids feel gravity but exert none.

6 Explore, Practice and Quiz

Explore has 18 concept cards: Newton’s law, surface gravity, superposition, Kepler’s three laws, orbital speed, vis-viva, orbital energy and escape, the barycentre, weighing binary stars, radial velocity, Lagrange points, the figure-eight, chaos, gravity assists, Hohmann transfers and the integration method. Each has a diagram (the three-body ones are computed live by the engine), a formula, a worked example with real numbers and usually a Try it in the simulator button. Practice deals unlimited problems from 12 types; answers within 2 % count and a worked solution follows. Quiz draws 5 questions from 20 and shuffles the options every time.

7 Units

Astronomy has its own units, used the same way in every country, so there is no SI / Imperial switch: distances are in astronomical units (1 AU = 149,597,870.7 km) or kilometres, speeds in km/s, times in hours, days or years, and masses in solar (M☉), Jupiter (M♃) or Earth (M⊕) masses. Practice problems use SI where the textbook formula does (G = 6.6743 × 10⁻¹¹ N·m²/kg²).

8 Tips
  • Turn on Velocity arrows and Gravity arrows in the Kepler scene: gravity always points at the star, the velocity only on a circle is at right angles to it.
  • Try Reverse time in the figure-eight: the bodies run backwards along the same curve.
  • A 0.2° change of the launch timing in the gravity assist turns an escape into a collision with Jupiter: that is why real missions make several correction burns.
  • Use Show working for the equations with this scene’s numbers, and Export for the positions and velocities of every body.

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:

Planeta (AU)ea1.5 (yr)Observed period (yr)
Mercury0.38710.20560.24080.2408
Venus0.72330.00680.61520.6152
Earth1.00000.01671.00001.0000
Mars1.52370.09341.88081.8808
Jupiter5.20290.048411.86811.863
Saturn9.53670.053929.45129.447
Uranus19.1890.047384.0684.02
Neptune30.0700.0086164.89164.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