A real-time 3D planetary simulation engine built with C++ and OpenGL. The project combines a simple N-body gravity system with textured planet rendering, orbital trails, a skybox, and free-camera exploration.
Photos are taken from the default solar_system.txt preset, which aims to mimic our own solar system.
Distances are scaled down by 1e10, masses are scaled to units of Earth masses. G is set to 0.00174 and orbital velocities are computed upon these values.
For better visibility, the radius of the sun and planets are scaled up (non uniformly).
Full solar system view with lighting, a skybox, and orbital trails.
Close-up of a planetary body, showcasing textures and dynamic lighting from the sun.
An example of a chaotic system generated by randomizing initial conditions.
- Simulates gravitational attraction between celestial bodies in real time.
- Renders planets and the sun as 3D textured spheres.
- Draws orbital trails so motion is easier to read over time.
- Uses a skybox and dynamic lighting to give the scene more depth.
- Loads planetary systems from preset text files instead of hardcoding them.
- N-body gravity simulation using pairwise force updates between bodies.
- Fixed-timestep physics loop for more stable simulation updates.
- 3D OpenGL rendering with custom shaders for planets, trails, the sun, and the skybox.
- Wavefront OBJ model loading for sphere geometry.
- Textured celestial bodies loaded through a central resource manager.
- Orbital trail rendering backed by dynamic OpenGL buffer updates.
- Free camera controls for navigating the scene in first-person.
- Preset-driven world building through editable files in
presets/. - 16:9 letterboxed viewport to keep presentation consistent when resizing.
- C++17
- OpenGL 3.3 Core Profile
- GLFW
- GLAD
- GLM
- FreeType (for text rendering, not implemented yet)
- stb_image
- CMake
src/- engine, physics, rendering, resources, and object creation.include/- shared headers and core types.shaders/- GLSL shader programs for scene rendering.textures/- planet, sun, and skybox textures.models/- mesh assets used for rendering spheres and other geometry.presets/- text-based simulation setups such assolar_system.txt.
Make sure these are available before building:
CMake3.10+OpenGL3.3+GLFW3.3FreeTypeGLADincluded ininclude/gladGLMinstalled system-wide or otherwise available to the compilerstb_imageincluded ininclude/stb_image.h
git clone <repository-url>
cd Planetary-Simulation
mkdir build
cd build
cmake ..
makeYou can run the executable from any directory. For example, from the project root:
./build/PlanetarySystemW,A,S,D- move the camera- Mouse - look around
Left Ctrl- release or lock the cursorEsc- quit the application
The default simulation is loaded from presets/solar_system.txt. You can create new systems by editing that file or adding another preset with the same format.
constants
G <gravitational_constant>
<sun>
pos <x> <y> <z>
radius <radius>
mass <mass>
vel <vx> <vy> <vz>
color <r> <g> <b>
<planet_name>
pos <x> <y> <z>
radius <radius>
mass <mass>
vel <vx> <vy> <vz>
color <r> <g> <b>
sun- reserved identifier for the system's light-emitting starpos- starting positionradius- rendered object sizemass- value used in gravitational calculationsvel- starting velocitycolor- base tint passed into rendering
If a planet name does not match a loaded texture, the engine falls back to a default plain texture.
This project has been a good hands-on exercise for learning:
- how a basic game or simulation loop separates input, update, and render stages
- how fixed timesteps improve consistency in real-time physics
- how Newton-style gravity can be approximated in an interactive N-body simulation
- how to organize rendering, physics, and world state into separate systems
- how OpenGL resources such as shaders, textures, VAOs, and VBOs are loaded and managed
- how dynamic vertex buffer updates can be used to draw orbital trails
- how camera movement and mouse look work in a 3D environment
- how external data files can drive scene creation without recompiling the project
- how CMake ties together source files and third-party graphics dependencies
- add UI controls for pausing, resetting, or changing simulation speed
- support selecting different preset files at launch
- add collision handling or body merging
- improve the physics integrator for better long-term orbital stability
- add labels, overlays, or body-specific information panels
- The simulation is designed as a learning project and can be extended into a more advanced engine or sandbox.