Simulating orbital bodies

Was recently reading about the Trappist-1 solar system with it’s multiple potentially habitable planets.  There was also a related article I found describing how the game Elite Dangerous’ procedural galaxy generation had managed to create a very similar system to Trappist-1.

That got me interested in trying to see if I could make a simulated solar system on my own and maybe build and experiment on that simulation. I started off with refreshing my concepts on Newtonian gravity. The formula is fairly straight forward, the force F applied to a stellar body is equal to the mass of the first body (M1) multiplied by the mass of the second body (M2) divided by the square of the distance between the two bodies (R). There is also the gravitational constant (G), but since this is a closed system with all values being relative to one another, we can simple assume this to be 1 and remove it.

F = G * (M1 * M2)  / (R ^ 2)

To test this out I created a simple scene in Unity with a couple of spheres that were to be my planets and stars. I added a sphere collider and a rigid body component on them so i could use the built in physics engine to manage forces and collisions.

Next I created a custom “space gravity” component to manage the mass and velocity of each stellar body. I also created a “Gravity System Manager” component to act as a singleton that would keep track of all stellar object in my simulation. Each time a new stellar body was created the space gravity component would go an register itself with the Gravity system manager. The every update frame each stellar body could go an get a list of all stellar objects in the simulation and calculate the force each would apply to it and update itself based on that calculation.

Here is a video of the basic simulation I ended up with.