This 3D Gravity Simulator is a C++ program that visualizes the gravitational interactions between celestial bodies in a simplified solar system model. It uses OpenGL for rendering and GLFW for window management and user input.
The simulator consists of several key components:
Simulator
: Handles the physics calculations and updates the positions of celestial bodies.Renderer
: Manages the 3D rendering of the celestial bodies, trajectories, and grid.CelestialBody
: Represents individual celestial bodies with properties like mass, position, and velocity.
The simulator uses Newton's law of universal gravitation to calculate the forces between celestial bodies. The gravitational force between two bodies is given by:
Where:
-
$F$ is the gravitational force between the two bodies -
$G$ is the gravitational constant ($6.67430 \times 10^{-11} , \text{N} \cdot \text{m}^2 / \text{kg}^2$ ) -
$m_1$ and$m_2$ are the masses of the two bodies -
$r$ is the distance between the centers of the two bodies
The motion of each celestial body is updated using numerical integration. We use a simple Euler method for updating positions and velocities:
- Calculate the net force on each body
- Calculate acceleration:
- Update velocity:
- Update position:
Where
The program uses OpenGL to render the 3D scene:
- Celestial bodies are represented as spheres with sizes proportional to their masses (using a logarithmic scale).
- A grid is drawn to provide a reference plane.
- Trajectories of the bodies are drawn as lines, fading out over time.
- The camera can be controlled using WASD keys for movement and the mouse for orientation.
- The simulation uses a fixed time step, which can lead to inaccuracies in long-term simulations.
- The Euler method for numerical integration is simple but can accumulate errors over time.
- The scale of the celestial bodies and their distances are not to true scale to make visualization easier.
- Relativistic effects are not considered; the simulation uses classical Newtonian mechanics.
- C++ compiler with C++17 support
- CMake (version 3.28 or higher)
- OpenGL libraries
- GLFW3
- GLM (OpenGL Mathematics)
- vcpkg (for managing dependencies)
-
Clone the repository:
git clone https://github.com/Quinta0/gravity.git cd gravity
-
Install vcpkg:
git clone https://github.com/Microsoft/vcpkg.git cd vcpkg ./bootstrap-vcpkg.sh # On Windows, use bootstrap-vcpkg.bat ./vcpkg integrate install cd ..
-
Install dependencies using vcpkg:
./vcpkg/vcpkg install freeglut glew glm vcpkg-cmake opengl glfw3
-
Create a build directory and run CMake:
mkdir build cd build cmake -DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake .. cmake --build .
-
Run the simulator:
./gravity