# josh-weston-g/orbit-sandbox architecture

> Architecture diagram of the GitHub repository josh-weston-g/orbit-sandbox, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/josh-weston-g/orbit-sandbox
- Repository: https://github.com/josh-weston-g/orbit-sandbox

## Overview

1. **Repository shape**
   - This is a desktop simulation app, not a web service.
   - The architecture is split into four main areas: app shell (`main.py`), physics/domain (`orbit/`), UI/views (`ui/`), and data/assets (`data/`, `assets/`).

2. **Runtime and UI stack**
   - The app is built in **Python 3.8+** and uses **NumPy** for vector math.
   - Rendering and interaction are handled by **Pygame**, with **pygame_gui** used for menus and UI widgets.
   - The UI is therefore a real-time game/simulation loop rather than a request/response interface.

3. **Application entry and orchestration**
   - `main.py` is the top-level entry point and is responsible for the main loop, resolution selection, and switching between views/screens.
   - It acts as the coordinator between the menu system, system loading, and the live simulation view.

4. **Domain model**
   - `orbit/body.py` defines the core physical entity: a body with mass, position, velocity, acceleration, name, and type.
   - This is the smallest unit shared by the loader, physics engine, and renderer.

5. **Physics layer**
   - `orbit/physics.py` contains the Newtonian gravity logic and orbital mechanics formulas.
   - This layer is the pure math boundary: it computes forces/accelerations without handling UI, file I/O, or rendering.

6. **Simulation engine**
   - `orbit/simulation.py` owns the N-body integration loop and state evolution over time.
   - It uses **Velocity Verlet integration** for position/velocity updates and maintains a history stack for rewind.
   - This is the main stateful core of the app: it consumes bodies and physics, then produces successive simulation states.

7. **Units and constants**
   - `orbit/units.py` centralizes the simulation’s unit system and gravitational constant defaults.
   - The repo uses astronomical units and solar-mass style scaling, which keeps the physics readable and consistent across presets and custom systems.

8. **System loading boundary**
   - `orbit/loader.py` bridges file-based configuration into runtime simulation objects.
   - It reads JSON system definitions from `data/default_systems/` and user-defined systems from `data/custom_systems/`, then instantiates bodies/simulations from those definitions.
   - This is the repository’s key data ingestion boundary.

9. **UI structure**
   - `ui/views.py` defines screen-level views such as the main menu, load-system menu, and simulation view.
   - `ui/visualize.py` contains the live simulation runner, handling drawing, camera controls, overlays, and user interaction during playback.
   - The UI layer consumes simulation state; it should not own the physics rules.

10. **Assets and presentation data**
   - `assets/fonts/`, `assets/images/`, and `assets/themes/` are presentation resources used by the menu and simulation HUD.
   - The theme JSON files are particularly important because they define pygame_gui styling and visual consistency across menus.

11. **Simulation data sets**
   - `data/default_systems/` contains the built-in scenarios such as binary stars, solar-system variants, escape trajectories, and three-body setups.
   - `data/custom_systems/` is the extension point for user-created scenarios, making system definitions content-driven rather than code-driven.

12. **Main data flow**
   - At startup, the app discovers available system JSON files.
   - The user selects a system in the UI.
   - The loader turns JSON into bodies and simulation parameters.
   - The simulation engine advances the system state each frame.
   - The renderer draws the current state and reacts to camera/time-control input.

13. **Architectural boundaries**
   - The strongest boundary is between **pure simulation logic** (`orbit/`) and **presentation/UI** (`ui/`).
   - File-based scenario definitions form a second boundary: the app can add or change systems without changing physics code.
   - There is no evidence of persistence beyond JSON files, and no network or server layer.

14. **Diagram takeaway**
   - For an architecture diagram, show one top-level desktop app loop feeding three subsystems: UI/navigation, system loading, and physics simulation.
   - Under physics, show body model + unit constants + gravitational acceleration + time integration/history.
   - Under UI, show menu screens, simulation visualization, and input controls.
   - Under data, show built-in and custom JSON systems plus theme/image/font assets.

Main entry points: [`ui/views.py`](https://github.com/josh-weston-g/orbit-sandbox/blob/main/ui/views.py) (Views), [`ui/visualize.py`](https://github.com/josh-weston-g/orbit-sandbox/blob/main/ui/visualize.py) (Visualizer), [`orbit/simulation.py`](https://github.com/josh-weston-g/orbit-sandbox/blob/main/orbit/simulation.py) (Integrator), [`orbit/loader.py`](https://github.com/josh-weston-g/orbit-sandbox/blob/main/orbit/loader.py) (Loader)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["App shell"]
  node_main["Main loop<br/>entrypoint<br/>[main.py]"]
end

subgraph group_orbit["Physics core"]
  node_body(("Body<br/>domain model<br/>[body.py]"))
  node_units["Units<br/>constants<br/>[units.py]"]
  node_physics["Gravity<br/>math engine<br/>[physics.py]"]
  node_simulation["Integrator<br/>state engine<br/>[simulation.py]"]
  node_loader["Loader<br/>data bridge<br/>[loader.py]"]
end

subgraph group_ui["UI layer"]
  node_views["Views<br/>screen router<br/>[views.py]"]
  node_visualize["Visualizer<br/>renderer<br/>[visualize.py]"]
end

subgraph group_data["Content"]
  node_default_systems["Default systems<br/>scenario data"]
  node_custom_systems["Custom systems<br/>scenario data"]
  node_themes["Themes<br/>ui assets"]
  node_fonts["Fonts<br/>assets"]
  node_images["Images<br/>assets"]
  node_main_menu_theme["Menu theme<br/>theme file"]
  node_load_system_theme["Load theme<br/>theme file"]
  node_pause_menu_theme["Pause theme<br/>theme file"]
end

node_main -->|"navigates"| node_views
node_main -->|"loads system"| node_loader
node_main -->|"runs"| node_simulation
node_views -->|"lists"| node_default_systems
node_views -->|"lists"| node_custom_systems
node_loader -->|"creates"| node_body
node_loader -->|"applies"| node_units
node_loader -->|"initializes"| node_simulation
node_simulation -->|"uses"| node_physics
node_simulation -->|"updates"| node_body
node_physics -->|"depends on"| node_units
node_visualize -->|"renders"| node_simulation
node_visualize -->|"receives input"| node_views
node_views -->|"opens"| node_visualize
node_views -->|"styles"| node_main_menu_theme
node_views -->|"styles"| node_load_system_theme
node_views -->|"styles"| node_pause_menu_theme
node_visualize -->|"uses"| node_fonts
node_visualize -->|"uses"| node_images
node_views -->|"loads"| node_themes

click node_main "https://github.com/josh-weston-g/orbit-sandbox/blob/main/main.py"
click node_body "https://github.com/josh-weston-g/orbit-sandbox/blob/main/orbit/body.py"
click node_units "https://github.com/josh-weston-g/orbit-sandbox/blob/main/orbit/units.py"
click node_physics "https://github.com/josh-weston-g/orbit-sandbox/blob/main/orbit/physics.py"
click node_simulation "https://github.com/josh-weston-g/orbit-sandbox/blob/main/orbit/simulation.py"
click node_loader "https://github.com/josh-weston-g/orbit-sandbox/blob/main/orbit/loader.py"
click node_views "https://github.com/josh-weston-g/orbit-sandbox/blob/main/ui/views.py"
click node_visualize "https://github.com/josh-weston-g/orbit-sandbox/blob/main/ui/visualize.py"
click node_default_systems "https://github.com/josh-weston-g/orbit-sandbox/tree/main/data/default_systems"
click node_custom_systems "https://github.com/josh-weston-g/orbit-sandbox/tree/main/data/custom_systems"
click node_themes "https://github.com/josh-weston-g/orbit-sandbox/tree/main/assets/themes"
click node_fonts "https://github.com/josh-weston-g/orbit-sandbox/tree/main/assets/fonts"
click node_images "https://github.com/josh-weston-g/orbit-sandbox/tree/main/assets/images"
click node_main_menu_theme "https://github.com/josh-weston-g/orbit-sandbox/blob/main/assets/themes/main_menu_theme.json"
click node_load_system_theme "https://github.com/josh-weston-g/orbit-sandbox/blob/main/assets/themes/load_system_theme.json"
click node_pause_menu_theme "https://github.com/josh-weston-g/orbit-sandbox/blob/main/assets/themes/pause_menu_theme.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main toneBlue
class node_body,node_units,node_physics,node_simulation,node_loader toneAmber
class node_views,node_visualize toneMint
class node_default_systems,node_custom_systems,node_themes,node_fonts,node_images,node_main_menu_theme,node_load_system_theme,node_pause_menu_theme toneRose
```

## Components

### App shell

Entry point and runtime orchestration.

- [Main loop](https://github.com/josh-weston-g/orbit-sandbox/blob/main/main.py): entrypoint · Starts the app, selects resolution, and switches between screens. · `main.py`

### Physics core

Domain model, math, integration, and loading.

- [Body](https://github.com/josh-weston-g/orbit-sandbox/blob/main/orbit/body.py): domain model · Core physical entity shared across loading, simulation, and rendering. · `orbit/body.py`
- [Units](https://github.com/josh-weston-g/orbit-sandbox/blob/main/orbit/units.py): constants · Simulation unit system and gravitational defaults. · `orbit/units.py`
- [Gravity](https://github.com/josh-weston-g/orbit-sandbox/blob/main/orbit/physics.py): math engine · Newtonian gravity and orbital mechanics calculations. · `orbit/physics.py`
- [Integrator](https://github.com/josh-weston-g/orbit-sandbox/blob/main/orbit/simulation.py): state engine · N-body evolution, velocity Verlet updates, and rewind history. · `orbit/simulation.py`
- [Loader](https://github.com/josh-weston-g/orbit-sandbox/blob/main/orbit/loader.py): data bridge · Reads system JSON and builds runtime simulation objects. · `orbit/loader.py`

### UI layer

Menus, screen views, and real-time visualization.

- [Views](https://github.com/josh-weston-g/orbit-sandbox/blob/main/ui/views.py): screen router · Defines the menu and simulation screen-level views. · `ui/views.py`
- [Visualizer](https://github.com/josh-weston-g/orbit-sandbox/blob/main/ui/visualize.py): renderer · Draws live simulation state and handles camera and playback interaction. · `ui/visualize.py`

### Content

Scenario definitions and presentation assets.

- [Default systems](https://github.com/josh-weston-g/orbit-sandbox/tree/main/data/default_systems): scenario data · Built-in JSON presets for common orbital setups. · `data/default_systems`
- [Custom systems](https://github.com/josh-weston-g/orbit-sandbox/tree/main/data/custom_systems): scenario data · User-defined JSON scenarios added without code changes. · `data/custom_systems`
- [Themes](https://github.com/josh-weston-g/orbit-sandbox/tree/main/assets/themes): ui assets · pygame_gui theme files for menus and HUD styling. · `assets/themes`
- [Fonts](https://github.com/josh-weston-g/orbit-sandbox/tree/main/assets/fonts): assets · Typeface resources used by the interface. · `assets/fonts`
- [Images](https://github.com/josh-weston-g/orbit-sandbox/tree/main/assets/images): assets · Image resources for menus and simulation presentation. · `assets/images`
- [Menu theme](https://github.com/josh-weston-g/orbit-sandbox/blob/main/assets/themes/main_menu_theme.json): theme file · Theme configuration for the main menu UI. · `assets/themes/main_menu_theme.json`
- [Load theme](https://github.com/josh-weston-g/orbit-sandbox/blob/main/assets/themes/load_system_theme.json): theme file · Theme configuration for the load-system menu. · `assets/themes/load_system_theme.json`
- [Pause theme](https://github.com/josh-weston-g/orbit-sandbox/blob/main/assets/themes/pause_menu_theme.json): theme file · Theme configuration for the pause menu. · `assets/themes/pause_menu_theme.json`

## Connections

- Main loop → Views: navigates · Routes between menu and simulation screens.
- Main loop → Loader: loads system · Turns the selected scenario into runtime objects.
- Main loop → Integrator: runs · Advances the active simulation each frame.
- Views → Default systems: lists · Shows built-in scenarios in the menu.
- Views → Custom systems: lists · Exposes user-created scenarios in the menu.
- Loader → Body: creates · Builds bodies from system definitions.
- Loader → Units: applies · Uses the shared unit system when constructing simulations.
- Loader → Integrator: initializes · Creates simulation state from loaded scenario data.
- Integrator → Gravity: uses · Computes forces and accelerations for each update.
- Integrator → Body: updates · Mutates body positions and velocities over time.
- Gravity → Units: depends on · Uses the shared constants and scaling choices.
- Visualizer → Integrator: renders · Reads current state for drawing and overlays.
- Visualizer → Views: receives input · Handles playback, camera, and UI interactions.
- Views → Visualizer: opens · Switches into the live simulation view.
- Views → Menu theme: styles · Uses menu styling for the main menu.
- Views → Load theme: styles · Uses menu styling for load-system screens.
- Views → Pause theme: styles · Uses menu styling for pause screens.
- Visualizer → Fonts: uses · Loads fonts for HUD and labels.
- Visualizer → Images: uses · Loads image assets for presentation.
- Views → Themes: loads · Consumes theme resources for pygame_gui widgets.
