Skip to content

cyprienti/gravity_sim

Repository files navigation

Gravity Simulation (OpenGL / C++)

A real-time 3D gravity simulator written in C++ and OpenGL that demonstrates several core physics and numerical methods concepts through interactive visualization.


What it does

The simulation integrates Newton's law of universal gravitation across multiple bodies and renders them as shaded spheres in a 3D scene with motion trails and a relativistically-warped spacetime grid.

Physics concepts demonstrated

Concept Description
Velocity Verlet integration A symplectic (time-reversible) integrator that conserves total mechanical energy far better than naïve Euler. Each frame the terminal prints total E_mech — watch it stay nearly constant.
Real Solar System Sun, Mercury, Venus, Earth, and Mars placed at their true NASA orbital radii and velocities. Kepler's three laws emerge from the simulation with no extra code — just Newton's inverse-square gravity.
Gravitational slingshot A probe passes close to a massive moving body and exits faster — the same technique used by Voyager, Cassini, and New Horizons. The terminal reports probe speed before and after the closest approach.
Live physics HUD Every second the terminal prints each body's velocity (km/s), gravitational force with the central body (N), and kinetic energy (J).
Spacetime grid The grid plane is warped using the Schwarzschild embedding formula dz = 2√(rₛ·(r−rₛ)), giving a visual representation of spacetime curvature.

Coordinate system

Quantity Scale
1 render unit 50,000 km
Simulation time at 1× speed 1 real second ≈ 4 simulation days
Earth orbital period ~90 real seconds

Controls

Key / Input Action
W A S D Move camera forward / left / back / right
Space Move camera up
Left Shift Move camera down
Scroll wheel Zoom in / out
Mouse Look around
T Toggle between Solar System scene and Gravitational Slingshot scene
= or numpad + Increase simulation speed (max 10×)
- or numpad - Decrease simulation speed (min 0.1×)
K (hold) Pause simulation
Left click (hold) Place a new body; drag to position with arrow keys
Right click (hold, while placing) Increase mass of body being placed
Q Quit

Dependencies

Library Purpose
GLFW 3 Window creation and input handling
GLEW OpenGL extension loading
GLM Math (vectors, matrices)
OpenGL 3.3 Rendering

How to compile

The project ships with local include/ and lib/ directories containing GLEW and GLM headers and the GLFW/GLEW import libraries, so no separate installation of those is needed.

Requires: MSYS2 MinGW-w64 (or any MinGW-w64 g++ ≥ 9).

g++ -g gravity_sim.cpp -o gravity_sim.exe \
    -I include \
    -L lib \
    -lglfw3dll \
    -lglew32 \
    -lopengl32 \
    -lgdi32

The glew32.dll included in the project root must be in the same directory as the executable (or on PATH) at runtime.

VS Code

A tasks.json build task is included. Open the project folder in VS Code and press Ctrl+Shift+B to build.


Credits

This project is inspired by and based on the original work of kavan010: https://github.com/kavan010/gravity_sim

Extensions added on top of the original include Velocity Verlet integration, the real Solar System scene, the gravitational slingshot demo, the live physics HUD, and motion trails with the spacetime grid warp.

About

3D Gravity Simulation in OpenGL/C++

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages