Gravity Simulator
Set the mass, position and velocity of 2 to 50 bodies, with labelled fields or, while paused at t 0, by dragging a body or its velocity arrow on the drawing, and watch them move under Newtonian gravity with G = 1. Each run is stepped by kick-drift-kick leapfrog with the time step and optional softening you choose, and beside the orbits the page keeps the drift readouts a fixed step makes necessary: the largest relative energy error so far, the change in net momentum, and how far the center of mass has left its straight line. Pause, step once, stop at a set time, start again from the current state, draw trails and velocity vectors, and save the run as CSV or the setup as a project file.
numbers mark bodies, lines are trails drag a body to edit it at t 0 G = 1
| body | mass | x | y | vx | vy |
|---|---|---|---|---|---|
| 1 | 1 | 0.9700 | -0.2431 | 0.4662 | 0.4324 |
| 2 | 1 | -0.9700 | 0.2431 | 0.4662 | 0.4324 |
| 3 | 1 | 0.0000 | 0.0000 | -0.9324 | -0.8647 |
t 0, 0 steps of dt 0.01, leapfrog softening 0, energy E0 -1.28714 energy drift 0 max so far momentum |p| 0 now, change 0 max center of mass off its straight line by 0 max
- Bodies
- 3, figure-eight
- Time
- t 0, dt 0.01
- Energy drift
- 0 max
- Net momentum
- 0
Units are dimensionless with G = 1: a mass of 1 pulls a body 1 unit away with an acceleration of 1. Energy drift is the largest |E - E0| / |E0| over every step so far.
Common questions
- How does a gravity simulator work?
- Every body pulls every other with a force of G times the two masses over the distance squared, here with G = 1 in dimensionless units. The page adds up the pulls for every pair, then moves the bodies forward one time step at a time with kick-drift-kick leapfrog: half a step of velocity change, a full step of motion, then the other half with the new pulls. The two pulls in each pair are equal and opposite, so net momentum is conserved to rounding.
- What is energy drift, and why does it matter?
- The total energy of an isolated system, kinetic plus potential, should not change, but finite time steps make it wander. The page records the starting energy E0 and shows the largest |E - E0| / |E0| over every step so far, or the plain change when E0 is within 1e-12 of 0. For the circular pair, 444 steps of 0.01 reach a largest error of 1.0e-8, and 888 steps of 0.005 over the same time reach 6.2e-10, 16 times smaller. Above 1% the page notes that a close pass between two bodies is the usual cause.
- What does softening do?
- Softening replaces the distance squared with the distance squared plus the softening length squared, in both the force and the potential energy, so two bodies passing very close no longer pull each other with a near-infinite force. Close passes become calmer, but the orbits differ from the unsoftened result. With softening 0, two bodies starting at the same point are refused, and a run in which two bodies meet at one point stops at the last finite step with a message.
- Why do the bodies fly apart in the Pythagorean 3-4-5 example?
- Masses of 3, 4 and 5 start at rest at the corners of a 3-4-5 right triangle and make very close passes. A fixed step of 0.001 cannot follow those passes accurately: by t 20 the page shows an energy drift of 1.2e+2, an error about 120 times the starting energy, so the paths after the first close passes are not a faithful solution. The example is there to show what the drift readout is for.
- Can this predict real orbits or plan a spacecraft trajectory?
- No. Units are dimensionless with G = 1, bodies are points moving in a plane, and the step size is fixed. The simulator is for exploring how gravity and numerical integration behave, not for astronomy, navigation or mission planning.
- How many bodies can it simulate, and what values are allowed?
- From 2 to 50. Add body places a body of mass 0.001 just outside the others, moving at circular-orbit speed for their total mass around their center of mass, and Remove deletes the selected body. Mass must be above 0 and at most 1000, positions and velocities lie between -1000 and 1000, the time step between 0.00001 and 1, and softening between 0 and 10.
- What do the CSV and project files contain?
- The CSV has one row per body per recorded sample, with step, t, body, mass, x, y, vx, vy, energy, energy_drift, momentum_x and momentum_y. Samples are taken on every step from t 0; once there are more than 1,000 the page keeps every other one and doubles the interval, and the last rows are always the current state. The project file is JSON holding the bodies, time step and softening at t 0, which Open project loads back; files over 1 MB are refused.
- Can I use it without a mouse, and does it remember my setup?
- Yes to both. Bodies are edited in labelled fields, and with the drawing focused Space plays or pauses, period steps once, plus and minus zoom, the arrow keys pan and F fits the view. The setup, trails, vectors, speed and stop time stay in this browser's storage for your next visit, and the run starts again from t 0. The page opens running, or paused when your device asks for reduced motion.
A dimensionless teaching simulation with G = 1: motion comes from stepping Newton's law of gravity forward in finite time steps, so the step size and any softening change the result, which is why energy and momentum drift are shown beside the orbits. It is not an astronomy, orbit-planning or navigation tool and does not predict the motion of real bodies.