Simulating Newtonian gravity using a Velocity-Verlet-Integrator and visualizing it with a Gravity-Well-Curve
This is a small project I coded myself as a small challenge and to deepen my understanding in C++. I coded this project myself, from the GPU rendering with OpenGL to the gravitation calculations.
- N-Body simulation
- Newtonian Gravity
- Velocity Verlet Integration
- Interactive Camera
- Dynamic Gravity Well
- OpenGL Rendering
- Multiple Simulations
To build the project just clone the repository and then in your console type:
mkdir build
cd build
cmake ..
cmake --build .
- C++20
- OpenGL
- GLFW
- GLAD
- GLM
- CMake
The Project consists of the following files:
These are the simulations I programmed. They are mostly copies of each other, just with different object and variable values to simulate different things.
This is a simulation of the solar system we all know. It consists of 13 different objects:
- sun
- mercury
- venus
- earth
- mars
- jupiter
- saturn
- uranus
- neptune
- moon
- ganymede
- europa
- titan
Although it is quite realistic, using scaled variable values (Scaled so the Sun has Mass = 1.0), the only unrealistic thing are the displayed sizes of the Objects, because if they were correctly scaled aswell, almost all object would be invisible. It is also correctly scaled to time, so if you want you can run it for an entire year and watch the earth do one rotation around the sun. Or you just adjust the simulation speed :D
This is a simpler simulation of a sun, earth and moon only. The values are not scaled to any real world references, so it is a bit unrealistic. Having that said, the visualization achieved with this simulation is much better then of the whole solar system, because in this simulation you can see the influence of the gravities much better. You can notice how nice the Earth "surfs" along the edge of the gravity-well-curve created by the mass of the sun.
This is a simulation of two generic spheres with equal size, masses and initial velocities. In this simulation you can see the gravity-well-curves of both bodies very good. It is also really interesting to see, how the bodies' gravities / the curves influence the other object. This one is my favourite.
This is the default object file, containting all the position, velocity and acceleration data of the object. It also contains parts of the Velocity-Verlet Integrator to update the positions and velocities more accurately than with a standard explicit Euler. Another interesting thing in this file is the draw function of the Sphere. It uses Polar coordinates to generate triangles along the longitude and latitude of a sphere and then render it.
Pretty standard camera file. Used to update the position and rotation of the camera so the user can control it with mouse and keyboard. (WASD to move, Q and E to move up/down and mouse to look around)
Physics file of the project. Uses Newtons gravitational law to calculate the gravitational forces between all elements.
Gravity-Well-Curve generator. First generates a generic grid parallel to the x-z-plane. Then updates the y-value of each grid intersection/node to draw a 3D Curve to visualize the gravitational force each object has.





