Skip to content

About

Where circles move, physics breaks, and orbit simulations are born ๐Ÿš€

Resources

Stars

4 stars

Watchers

1 watching

Forks

Latest commit

ย 

History

40 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŒŒ 2D Orbital Simulator (Python + Pygame)

๐Ÿš€ Getting Started

Prerequisites

โœ… Tested Environment

This project has been tested with:

  • Python 3.13.14
  • Pygame 2.6.1

Note: Python 3.14 may currently encounter installation issues due to upstream Pygame compatibility.

  • Python 3.x installed

1. Clone the repository

git clone https://github.com/NeilNNP45-dev/orbit_simulator.git
cd orbit_simulator

2. Create a virtual environment (recommended)

Windows

python -m venv .venv

Activate it:

.venv\Scripts\activate

macOS / Linux

python3 -m venv .venv

Activate it:

source .venv/bin/activate

3. Install the required packages

pip install -r requirements.txt

4. Run the simulator

python main.py

A physics-based 2D orbital simulator built using Python and Pygame, where celestial bodies interact through gravity and orbit each other in real time.

What started as a small gravity experiment slowly evolved into a full simulation sandbox featuring stable orbital mechanics, camera controls, simulation controls, and real-time diagnostics.


1000080486 ๐Ÿ“ธ IMAGE 1 โ€” Main Simulation Sandbox


โœจ Features

Physics

  • ๐ŸŒ True n-body gravity simulation (every body attracts every other body)
  • ๐Ÿ“ˆ Leapfrog integration for improved long-term orbital stability
  • ๐Ÿช Stable multi-body orbital system
  • ๐ŸŒ  Orbit trail rendering
  • ๐Ÿงน Automatic cleanup for escaped bodies

1000080490๐Ÿ“ธ IMAGE 2 โ€” Time Acceleration / Deceleration (Simulation Speed Control)

Camera & Controls

  • ๐ŸŽฅ Camera panning
  • ๐Ÿ” Zoom controls
  • โ™ป๏ธ Full universe reset
  • โธ๏ธ Pause / resume simulation
  • โšก Adjustable simulation speed

1000080488๐Ÿ“ธ IMAGE 3 โ€” Time Stop (Pause / Resume Simulation) (Yes, it's a JoJo reference :3)

Interaction & UI

  • ๐Ÿชจ Spawn new bodies with mouse clicks
  • ๐Ÿ–ฅ๏ธ Real-time HUD with simulation statistics
  • ๐Ÿ”” Timed notification system for simulation events

1000080487๐Ÿ“ธ IMAGE 4 โ€” User-Spawned Asteroids


๐Ÿง  Physics Used

This simulator uses Newton's Law of Universal Gravitation to compute gravitational forces between bodies.

Gravitational Force

F = G ร— (mโ‚ ร— mโ‚‚) / rยฒ

Each body experiences gravitational attraction from every other body.

Acceleration

a = F / m

To improve orbital stability, the simulator uses Leapfrog Integration (Velocity Verlet style) instead of basic Euler integration.

This significantly reduces:

  • Orbital drift
  • Numerical instability
  • Energy loss / gain over long simulations

1000080492๐Ÿ“ธ IMAGE 5 โ€” Universe Reset (Restart Simulation from the Beginning โ€” another JoJo reference :3)


๐Ÿ“ Project Structure

orbit_simulator/
โ”‚
โ”œโ”€โ”€ main.py              # Main simulation loop and rendering
โ”œโ”€โ”€ body.py              # Body class and default solar system creation
โ”œโ”€โ”€ physics.py           # Gravity calculations and Leapfrog integration
โ”œโ”€โ”€ camera.py            # Camera movement, zoom, and coordinate conversion
โ”œโ”€โ”€ requirements.txt     # Project dependencies
โ””โ”€โ”€ README.md            # Project documentation

The project follows a modular structure, separating physics, rendering, camera logic, and simulation management into individual modules to keep the codebase easier to maintain and extend.

โ˜€๏ธ Default Simulation Setup

The current simulation contains:

  • โ˜€๏ธ 1 Sun
  • ๐Ÿช 5 orbiting planets

Each body stores:

  • Position (x, y)
  • Velocity (vx, vy)
  • Acceleration (ax, ay)
  • Mass
  • Radius
  • Color
  • Trail history

๐ŸŽฎ Controls

Camera

  • W / A / S / D โ†’ Move camera
  • I โ†’ Zoom in
  • O โ†’ Zoom out

Simulation

  • Space โ†’ Pause / Resume simulation
  • R โ†’ Reset universe
  • โ†‘ โ†’ Increase simulation speed
  • โ†“ โ†’ Decrease simulation speed

Interaction

  • Left Click โ†’ Spawn asteroid / new body

๐Ÿ“Š HUD Information

The HUD displays:

  • FPS
  • Number of active bodies
  • Zoom level
  • Simulation speed
  • Simulation state
  • Physics integrator

๐Ÿ› ๏ธ Built With

  • Python ๐Ÿ
  • Pygame ๐ŸŽฎ
  • Physics
  • Math
  • Lots of debugging ๐Ÿ˜ตโ€๐Ÿ’ซ

๐Ÿš€ Future Ideas

Possible future additions:

  • Follow-body camera
  • Collision / merger physics
  • Save & load simulation states
  • Named celestial bodies
  • Barnesโ€“Hut optimization for large-scale simulations

This project has been a major learning experience in:

  • Physics simulation
  • Numerical integration
  • Software design
  • Debugging complex systems
  • Writing modular Python code

Still evolving, but already a fun little universe to experiment with ๐ŸŒŒ

About

Where circles move, physics breaks, and orbit simulations are born ๐Ÿš€

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages