There use to be a nifty orbit simulator for Mac Sytem 6 or so. It let users input an arbitrary system, then it would simulate how the planets and stars would orbit each other. It was all black and white, and was rather limited. But it was always fun to try and create the most complicated but stable system you could. I remember this program fondly, but can't find it anywhere. Nor can I find such an easy to use simulator like it.
This is my attempt to create an orbit simulator that is intuitive, easy to use and fun, while remaining as accurate as possible.
- Full N-body simulation in Javascript
- Three different algorithms to choose from
- Play, pause and reset a simulation
- Record a simulation to an animated GIF
- Accelerate simulation either by calculating more between frames (accurate, but slower), or by skipping more time (less accurate, but fast)
- Save a simulation to your browser's Local Storage
- Save and reload a simulation via JSON files
- Export positional data to CSV for analysis
- Touch accessible
- A number of presets demonstrating capibilities
- Calculate Lagrange points (approximatly)
- Generate random objects to simulate evalution of a system
- Collisions between objects
This simulator implements three different iterative algorithms to simulate gravity. By default, we use Runge-Kutta, as it has shown to be much more stable than Euler or Verlet, with only a small speed difference.
The code supports a 3D simulation, but display an input are limited to 2 dimentions at this time.
By default, new objects are created at the distance from the center that was clicked, and given a speed to make a circular orbit, assuming a single star at the center point as the first object. Values can be modified to suite, but this seemed like a good estimate to start.
It is capable of storing the current state into your browser's Local Storage, or exporting to a JSON file. You can then choose to load the state or setup from either the last local store, or a selected JSON file. State loads all the history and places the object at the last location. Setup loads the objects and places them in their initial locations without history. Note that no data is sent for either local storage or file storage to a remote server. All remains local to your browser.
This simulation works by iteritivly calculating the forces on objects. Universal Gravitation says that:
where G is the gravitational constant, M1 and M2 are masses being attacted, and r is the distance between them. Newton's second law also says:
or
so we know that the force acting on an object changes the acceleration based on the mass of the object. Therefore
which means that the acceleration experinced by an object is based on the mass of the other object divided by the square of the distance between them, multiplied by a constant. Do this for every object acting on the current one, and you have the new acceleration. Update the position and velocity based on the time between calculations.
Additionally, because the gravitational constant G is very very small, and most values in space are very very very big, values here have been scaled by G to reduce the computation needed, and the deal in smaller numbers. They are still really big, so things where scaled again further shrink numbers without loosing accuracy. So don't use these numbers to launch a space ship, or do your homework.
Easy, right?
The trick is the acculated error from large enough time steps. There are three methods considered for this simulator:
- Euler
- Simple, just do the calculation at every step
- Verlet
- Estimates a half way value to correct for some error
- First iteration bootstraps based on velocity, while the rest just use the previous position
- Runge-Kutta
- Estimates, based on differential equations, 4 sub steps, to correct for more error
- where accel(x) is the acceleration of the object being considered at point x at the current time.
This simulator defaults to Runge-Kutta, as it is reasonably fast, and very accurate. It also implements Euler and Verlet as options as they are faster, but these tend to be unstable for tight orbits.
When adding a new object, the panel is initialized with a resonable first guess of a circular orbit. This is because the velocity of a circular orbit is approximatly:
Lagrange points are simi-stable orbits that remain fixed in reference to a primary and secondary (sun and planet, planet and moon) object. At these points, the gravitation of the two objects, combined with the cretrifgual force experienced in orbit, cancel each other out. These are approximatly on either side of the secondary object inline with the primary (L1 and L2), in the opposite side of the secondary's orbit (L3), and at 60 degrees ahead and behind the secondary's orbit (L4 and L5).
The full derivation becomes quite complicated and results in a 5th order equation that can't be generally solved. So either estimates are made, assuming the secondary object is much smaller than the primary, or it is solved iterativly, searching for these equalibrium points with finer and finer grain passes. This simulator uses the second approach.
L4 and L5 are trivial to calculate, as they are at the same disance from the barrycenter as the secondary, and in the same orbit, just offset by 60 degrees either side.
The primary orbits the barrycenter at
Using that, you can calculate the distance from both the primary and secondary objects, depending on which point you want to solve for, with L1 being between primary and secondary, L2 being near the secondary but with both primary and secondary on the same side, and L3 being on the opposite side of the primary from the secondary.
The gravitation experienced at each of these points depends on which point to consider, some adding and some subtracting, but the base is the same, so we'll consider the L1 point.
that is, the gravitational force of the primary, minus the gravitational force of the secondary. For L2 and L3, the forces add.
The object also has centrifigual forces, as it orbits, based on it's speed. As the object short orbit in the same period as the secondary, we can calculate speed for the proposed orbit.
To solve for the proper radius, itterativly propose a radius, and find out when the forces over balance, and then try again, with a smaller step, until you find approximatly 0 difference. Then you have your solution. Much easier than solving this: