Browser tool
Gravity simulator
The Earth and the Moon, moved by Newton’s law of gravitation with their real masses and distance, in a 3D view you can turn and zoom. Add bodies of any mass, throw them into orbit, and see the path each one will take before you let it go.
Press on the plane where the new body should go, and drag to give it a speed.
Bodies are drawn larger than they are; distances, masses and motion are real.
Your browser could not start WebGL, so the 3D view cannot be shown. The simulation still runs: add bodies with the form and follow them in the list of bodies.
Step moves the simulation on by 4 h 48 min.
The simulation is running slower than set, to keep its steps small enough to stay accurate.
Scene
The Moon at its mean distance of 384,400 km and mean speed of 1.022 km/s.
Add a body
Equals: 7.342 × 10²² kg · 0.01229 Earth masses
Choose a body, then press on the plane in the view and drag. The arrow is the body’s velocity, measured against the body that pulls on it hardest at that point; the dashed line is the path it will take.
There are 24 bodies, the most this simulator runs. Remove one to add another.
Circular orbit
Place with numbers
Selected body
Select a body by clicking or tapping it in the view, or in the list of bodies.
Measurements
- Simulated time
- 0 s
- Bodies
- 2
- Total energy
- –
- Energy drift
- 0
Relative change since the last time bodies were added, removed, merged or edited.
Bodies
There are no bodies. Choose a scene or add a body.
| Name | Mass | Strongest pull | Distance | Relative speed |
|---|
How it works
I first wrote a version of this years ago with canvas. It had only the Earth and the Moon, moved by Newton’s law of gravitation, with atan2 to find the direction of the pull. This simulation is built on the same idea.
Newton’s law of universal gravitation
Two bodies of mass m1 and m2 a distance r apart pull on each other with a force of
F = G m1 m2r2
Here G = 6.6743 × 10⁻¹¹ m³ kg⁻¹ s⁻² is the gravitational constant. The simulation uses real values: the Earth’s mass is 5.972 × 10²⁴ kg, the Moon’s 7.342 × 10²² kg, and their mean distance 384,400 km. Every body pulls on every other body, and the forces add up.
Direction and components
The force points from one body toward the other. With Δx and Δy the differences between them, the angle of that direction comes from atan2:
θ = atan2(Δy, Δx)
Fx = F cos θ, Fy = F sin θ
atan2 is used rather than atan(Δy/Δx) because the division loses the sign of Δx, while atan2 looks at both signs and gets the angle right in all four quadrants. Since cos θ = Δx/r and sin θ = Δy/r, this is exactly the same as multiplying the force by the unit vector. Each component is then divided by the mass (F = ma):
ax = Fx/m, ay = Fy/m
The velocity Verlet step
With the accelerations known, positions and velocities move forward in small time steps Δt. Each step gives the velocity half a kick, moves the position a full step with it, works out the accelerations at the new positions, and finishes the velocity with the other half:
vn+½ = vn + an Δt/2xn+1 = xn + vn+½ Δtvn+1 = vn+½ + an+1 Δt/2
This method is symplectic: it keeps the geometry of the motion. The simple Euler method adds a little energy every turn, and the Moon slowly spirals outward. With Verlet the total energy only wobbles slightly and does not build up. The energy drift under “Measurements” checks that as you watch: in the Earth and Moon scene it stays well below one part in a million.
A step is never longer than 0.02 times the shortest dynamical time of any pair, √(r³/G(m1 + m2)), about 300 steps per circular orbit. So that a very close pass cannot give an enormous kick, the distance is taken as √(r² + ε²) with ε = 10 km; real bodies collide long before this softening matters. When two bodies touch, using their real radii from mass and density, they merge into one, keeping their total mass and momentum.
Circular orbits and periods
To circle a body of mass M at distance r, gravity has to supply exactly the force circular motion needs, mv²/r. That gives the circular speed:
v = √GM/r
The “Circular orbit” button gives this speed at right angles to the radius (counting the added mass too, with M + m for M). Faster than √2 times it, a body escapes. The period of the selected body comes from its energy (vis-viva), treating it and the body that pulls on it hardest as a pair on their own, with a the semi-major axis of the orbit:
T = 2π √a³/GM
In the view, bodies are drawn larger than they are. To scale, the Moon would be a dot at its distance from the Earth, so every body has a minimum size on screen that grows with its mass, and shrinks to its real size when you zoom in. Distances, masses and motion are real.
Tips
Getting a circular orbit. The easiest way is “Circular orbit” under “Add a body”: choose the body to go around and a distance. By hand, throw at right angles to the line toward the planet and adjust the length of the arrow until the dashed line closes into a circle.
Slower or faster than a circle. Throw a little slower and the body falls into an ellipse that swings closer in, speeding up as it passes. Throw faster and the ellipse grows; at √2 times the circular speed it is no longer bound and leaves for good. The label next to the arrow tells you which.
Try the figure eight. Three equal masses following each other along one curve is one of the few three-body orbits that repeat. Add a Moon close to it and see how long the dance lasts.
Why the energy number moves. The simulation takes small steps instead of following the motion exactly, so the total energy wobbles a little. Velocity Verlet keeps that wobble bounded instead of letting it grow, which is why orbits do not slowly spiral out. A collision really does lose energy: it would turn into heat.
Follow a body. Select a body and tick “Follow selected”: the camera moves with it. Follow the Earth to see the Moon go round it; the trails stay drawn in the frame of the whole scene, so they show how the Earth itself wobbles.
Zoom in to see true sizes. Bodies are drawn with a minimum size on screen so they stay visible. Zoom in close and they shrink to their real radius: the Earth’s radius is 6,371 km, and the geostationary satellite is more than six Earth radii away.