Simulating the Solar System in C++

Hey everyone,

Recently I’ve been working on simulating our solar system and it has been a difficult endeavor. There are a few big problems that I faced in this project:

  • Float Point Imprecision
  • Conversion from actual (metric) values to something smaller

Let’s evaluate each of these items.

Floating Point Imprecision

A float is a 32-bit data type in many languages that allow you to hold decimal data. You may have heard of floats referred to as Singles, this is because floats have a bigger brother known as a double. They are called doubles because it is a 64 bit data type. This allows a double to hold a much larger number, but you have the overhead of using a lot more memory. Due to this, there is a tradeoff in the manipulation of the data. Further, floats are use in many existing libraries and utilities, so now there is build in imprecision. What is imprecision? Imagine working with some really small numbers, something likeĀ 1.0 x 10-10 then dividing this number by say 1000. After these operations, the values will be getting so small that you are bound to lose some digits simply because the data type cannot hold numbers of that size. Imprecision is basically a limit on decimal digits, resulting in “unintended” rounding.

Actual Value Conversion

Because of the previous point having been stated about imprecision, using the real solar values is not preferrable because after all of our calculations, much of the data will be cut off. I found this part very difficult because finding a suitable size was nearly impossible. However, in the end I was able to make a decision and for distances, I decided to go with Astronomical Units and for weight, Earth Masses. Getting these values was easy because all I had to do was perform a simple Google search and I could find the AU’s a given planet was from the sun, and their weight inĀ Earths.

Formula

To perform this simulation, I am using the Newtonian Universal Gravitation Equation

universal gravitation equation

Because of the units that I chose to use to scale the actual values (AUs and Earth Masses), it was extremely difficult to convert Big G to a number that accurately represents Earth Masses and AU’s. For this I used the good old, Guess and Check method. I just tried values that I thought might work really well for what I was doing. Ultimately I was able to find a value that fit well with my solar system.

Notes

This project is programmed in C++ using OpenGL (Libraries: GLFW3, Glut, Glew, and GLM).

Video

Check out the video below to get a better idea of what the project is like