N-Body Simulation

Coded in C++

For one of my school assignments, I was tasked with creating an N-body simulation, animating a system of particles themed after the planets in the solar system and under the influence of mutual gravitational forces. The assignment consisted of two parts; I first loaded a static image of my simulation based on an input file containing data such as the size and radius of the mock system, along with the position, velocity and GIF files for each of the planets included. The second part of the assignment focused on incorporating the actual physics and animation.

My implementation involved creating two classes named CelestialBody and Universe. The CelestialBody class includes variables and functions that retrieve the GIF files associated with the planet names passed into the object, insert them into a sprite, and render them to the window. Accessor and mutator functions were developed for both classes' overloaded operators, parsing in the planet's coordinates and filenames for easier access when coding each class's drawing function. The Universe class assembles a vector of CelestialBody objects, which is used to output data from the input file to the terminal while also enabling the display of each planet on the screen.

To introduce real-time motion into the simulation in the second part of the assignment, the CelestialBody class was updated to include methods of tracking the velocity and position of each planet, with the visual representation of the planets on screen adjusted by flipping the y-coordinate, taking into account the coordinate system used by the SFML library. In the Universe class, a newly added function iterates over every possible pair of planets in the vector, calculates their gravitational force and distance based on the pairwise formula, and subsequently updates their positions and velocities. For extra credit, I added an elapsed time counter to the top left corner of the screen.