A program that simulates the gravitational interactions of celestial bodies (an N-Body simulation). Written in C++. Uses the SDL2 library to handle rendering and GUI. Implemented using the Barnes-Hut algorithm.
You can try the simulation in your browser here, where the program's C++ source code has been converted to WebAssembly using Emscripten.
Press z
to zoom in on cursor, LSHIFT z
to zoom out, and arrow keys to move the camera.
Spawn particles (planets/stars) manually in desired locations using the SPAWN PARTICLES
button, or choose one of the example particle configurations that are provided by the program.
First, press the SPAWN PARTICLES
drop-down button:
Select the number of particles you want to spawn at once, and select the desired mass for your particle(s).
Then, press PLACE PARTICLE(s)
to place particles with the selected settings.
The simulation program provides a few example planet/star configurations you can play around with.
STAR SYSTEM
is a generic star system with one star and two planets that orbit it.
BINARY STAR SYSTEM
is a star system with two stars of equal mass orbiting each other.
UNSTABLE 3 BODY
is more interesting. With three bodies of equal mass, this system is highly
chaotic and also extremely sensitive -- using double vs floating point precision in simulation computations leads to hugely different results. Most configurations with three bodies are similarly chaotic and sensitive, and in most three-body systems the masses fling themselves out of orbit in the end and part ways (perhaps why similarly chaotic star systems are so rare in nature). This is a case illustrating the difficulties of the infamous three-body-problem, which seeks to
solve for the motion of a three-body system given each body's initial position and velocity. Unlike the two-body-problem, no closed-form
solution exists to this day for the three-body-problem.
EULER'S 3 BODY
While "most" three-body configurations are highly unstable, there are some special configurations where the three masses lock themselves into a stable and repeating orbit. Leonhard Euler himself has found several such special three-body configurations, and one of them has been included here in this program. There is one mass locked in the system's center of mass, and two opposite masses orbit the center mass in a sideways figure-eight.
Newton's equation for gravitational attraction is given by:
where:
- ( F ) is the magnitude of the gravitational force between two objects,
- ( G ) is the gravitational constant,
- ( m_1 ) and ( m_2 ) are the masses of the two objects, and
- ( r ) is the distance between the centers of the two objects.
Every object in a system exerts gravitational attractive force on every other object in the system, which gives n * (n - 1) = n^2 - n interactions happening every second. A naiive implementation of the simulation that goes through every interaction would therefore have time complexity O(n^2).
But objects that are very far apart exert very little (close to zero) gravity upon each other. As such, we can group nearby bodies and use their center of mass to approximate the force that the group as a whole exerts on some object that's sufficiently far away. This is the core idea of the Barnes-Hut algorithm, usually implemented (here as well) using a quad-tree. By approximating rather than computing exactly forces exchanged by very far away bodies, the simulation becomes more efficient, with time complexity O(nlogn).