Gravity Simulation of the Solar System

I was curious about how easy this would be to do, so the other day I took the leap and wrote a simple gravity simulator with the Earth and Sun. I googled for "planet table" and used the data from NASA's public planetary fact sheet. I used the average distance from the Sun and average orbital velocity to set initial conditions for the Earth. After applying the law of gravity, I got a nice circular orbit. The masses, distances, and velocities I used are all accurate, but the diameters are much larger than in the actual solar system. With the diameters at the proper scale, the sun would be a tiny spec and the earth would be invisible.

But once I simulated two bodies, it was too easy to resist adding more. So I took the data for Mercury, Venus, and Mars, and added them to the simulation. Each body is compared to every other body when computing accelerations due to gravity, but the Sun's mass is so much greater that it overshadows the planets' gravity no matter how close together they seem.

The orbits are still all circular however, while in real life they are elliptical. I might have been accurately simulating some solar system, but it was not ours. So I looked online again and found a NASA tool called HORIZONS, which can give accurate position and velocity information about celestial bodies in (x, y, z) Cartesian coordinates. I set the coordinate origin to be the Sun and chose data from today, August 28, 2016 to use as the initial conditions for my simulation. The result is a simulation of the actual elliptical orbits of the inner planets, with 1000-second granularity (I re-calculated accelerations due to gravity for every 1000 simulated seconds). I have included a date counter so that you can trace the Earth around the Sun and see that it does in fact cross the same point on the same date.

Then I added some cool comet-looking tails just because.

Here's the simulation with the outer planets instead of the inner planets. The granularity is 100,000 seconds and it is sped up 100x from the earlier simulations since the outer planets have such long orbital periods.

Robert Fotino

Robert Fotino

I'm a former computer science student at UCLA, and a game developer in my free time. This site is a place for me to write about whatever I happen to be working on.

Read More