Skip to content

About

A real-time 3D simulation of the Sun and its eight planets: gravity is integrated numerically and everything is rendered with OpenGL 3.3 from Python.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Solar System Simulation

A real-time 3D simulation of the Sun and the eight planets in Python. Newtonian gravity is integrated with velocity Verlet, and the scene is rendered with OpenGL 3.3 through ModernGL. The relative energy error stays below 3 · 10⁻⁶ over 100 simulated years.

The whole solar system seen from above with complete orbit trails

The system from above after 164 simulated years. Every orbit trail is complete, including Neptune's. Planet sizes are enlarged.

The Sun with the inner planets, Earth with day and night side, Saturn with its rings and shadow

Left to right: the Sun with the inner planets, Earth (day and night side), Saturn (rings with shadows).

Usage

Requirements: Python 3.10 or newer and a GPU driver with OpenGL 3.3.

git clone https://github.com/philipp-sz/solar_sim_python.git
cd solar_sim_python
python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txt
python main.py

python energy_demo.py plots the relative energy error of explicit Euler and velocity Verlet over one simulated year.

Controls

Input Action
Mouse drag rotate the camera around the target
Mouse wheel zoom
1 to 9 follow Sun, Mercury, Venus, Earth+Moon, Mars, Jupiter, Saturn, Uranus, Neptune
0 back to the free view around the origin
R switch between exaggerated and real radii
C switch the limit on the visible rotation speed
T Venus only (while following it): atmosphere or surface map
W Earth only: clouds on or off
O orbit trails on or off
Up / Down double or halve the simulated time speed (30 to 1920 days per second, shown in the window title)
P pause or resume the simulated time
Space (hold) inspection mode: the simulation pauses and all bodies rotate slowly

Higher-resolution textures

The repository contains 2K textures. To use larger versions, download the files listed in assets/textures/CREDITS.txt into assets/textures_8k/ with the same folder structure, then set TEXTURE_QUALITY = "8k" in config.py. The Sun, Venus' atmosphere, Jupiter and Saturn exist only in 4K, Uranus and Neptune only in 2K. The large set needs about 1 GiB of GPU memory. The start then takes about 3.5 s instead of 0.4 s on the test machine (RTX 4070).

Features

  • Gravity between the Sun and eight planets (Earth and Moon as one body). Start positions, velocities and masses come from NASA/JPL ephemerides.
  • Velocity Verlet integration with a fixed step of one day.
  • Simulated time from 30 to 1920 days per second, with pause.
  • Textured bodies lit by the Sun. Each body spins with its real rotation period and axial tilt.
  • Earth shows the day map on the sunlit side and city lights on the night side, with optional clouds.
  • Venus switches between the atmosphere and the surface map.
  • Saturn's rings are translucent and lit by the Sun. They receive Saturn's shadow and cast a shadow onto the planet.
  • A Milky Way background, rotated into galactic coordinates so the band lies where it does in the real sky.
  • An orbit camera that can follow any body, orbit trails, and a switch between exaggerated and real radii.

How it works

Physics

State is kept in astronomical units, days and solar masses, so the gravitational constant is G = 2.959 · 10⁻⁴ AU³ / (M☉ · day²). Before the first step, positions and velocities are shifted so that the center of mass is at rest at the origin. Accelerations are computed for all pairs at once with NumPy broadcasting:

a_i = G · Σ_j m_j (r_j − r_i) / |r_j − r_i|³

Velocity Verlet advances one step of length Δt as follows:

  1. r(t + Δt) = r(t) + v(t) Δt + ½ a(t) Δt²
  2. compute a(t + Δt) from the new positions
  3. v(t + Δt) = v(t) + ½ (a(t) + a(t + Δt)) Δt

Relative energy error |ΔE / E₀| with Δt ≈ 1 day, computed with the functions in energy_demo.py:

Simulated time Explicit Euler Velocity Verlet (maximum)
1 year 8.8 · 10⁻³ 2.6 · 10⁻⁶
100 years 1.3 · 10⁻¹ 2.7 · 10⁻⁶

Rendering

All bodies share one UV sphere mesh (32 × 64) and get their own model matrix: translation, axial tilt, spin and scale. The fragment shader lights each body from the direction of the Sun and blends Earth's day, night and cloud textures. The sky is drawn first with the view rotation only and no depth test. The ring is drawn last with alpha blending. The ring's shadow on Saturn is found by intersecting the ray to the Sun with the ring plane. Saturn's shadow on the ring is found by intersecting that ray with the planet sphere.

Project structure

main.py             entry point
app.py              window, camera, simulation loop and drawing
config.py           paths, window, camera, time stepping and other settings
utils.py            loading of textures and shaders
energy_demo.py      energy error of Euler against velocity Verlet
calculations/
  bodies.py         data of the nine bodies
  calc_physics.py   gravity, integrators, energy
  calc_graphics.py  meshes and matrices
shaders/            GLSL shaders (color, texture, ring, sky)
assets/textures/    2K textures and CREDITS.txt
docs/images/        images for this README

Limitations

  • Tested only on Windows 11 with an NVIDIA RTX 4070.
  • Earth and Moon are simulated as one body. The moons of other planets are not included.

Implemented by the author. Claude Code was used to explain concepts.

License and credits

  • Code: MIT License, see LICENSE.md.
  • Textures: Solar System Scope, licensed under CC BY 4.0 and based on NASA imagery. Details in assets/textures/CREDITS.txt.
  • Data: start positions, velocities and masses from NASA/JPL ephemerides. Rotation periods and axial tilts from the NASA planetary fact sheets. Ring dimensions from the NASA Saturn ring fact sheet. The transformation to galactic coordinates follows the Hipparcos definition.

About

A real-time 3D simulation of the Sun and its eight planets: gravity is integrated numerically and everything is rendered with OpenGL 3.3 from Python.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages