The N-Body Gravity Simulator is an interactive, full-stack application designed to simulate and visualize the gravitational interactions between multiple celestial bodies. The engine relies on Newton's Law of Universal Gravitation and ensures highly stable orbital mechanics over time by utilizing Runge-Kutta 4th Order (RK4) numerical integration.
The project is split into a robust Python FastAPI backend for heavy physics computations and a responsive React frontend powered by Three.js for beautiful 3D rendering.
- Accurate Physics Engine: Uses NumPy-vectorized operations and RK4 integration to prevent artificial energy gain and maintain stable orbits, outperforming standard Euler methods.
- 3D Visualization: Real-time 3D rendering of planets, stars, and orbital paths using
@react-three/fiberand@react-three/drei. - Interactive Dashboard: Control the flow of time by adjusting the simulation time step (
dt), and play/pause the simulation on the fly. - RESTful API: A stateless backend architecture allowing easy integration or headless execution.
- Test Datasets: Pre-configured JSON data files for the Earth-Moon system and the Inner Solar System.
Backend:
- Python 3.x
- FastAPI - High-performance API framework
- NumPy - Fast, vectorized mathematical operations
- Pytest - Unit testing
Frontend:
- React (via Vite)
- Three.js & React Three Fiber - 3D scene rendering
- Axios - API client
- Python 3.8+
- Node.js (v16+)
- npm or yarn
- Navigate to the backend directory:
cd backend - Create and activate a virtual environment (optional but recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
- Install the required Python packages:
pip install -r requirements.txt
- Start the FastAPI server:
The backend will run at
uvicorn main:app --reload
http://127.0.0.1:8000. You can view the interactive API docs athttp://127.0.0.1:8000/docs.
- Open a new terminal and navigate to the frontend directory:
cd frontend - Install the Node dependencies:
npm install
- Start the Vite development server:
The frontend will run at
npm run dev
http://localhost:3000.
- Open
http://localhost:3000in your web browser. - Click "Load Test System" in the Mission Control panel to initialize the API with a central star and an orbiting planet.
- Click "Play" to start the simulation.
- Use the Time Step (dt) slider to speed up or slow down the gravitational calculations.
- Use your mouse to rotate, pan, and zoom around the 3D space.
The backend operates as a stateless FastAPI service, maintaining the universe simulation in memory.
POST /api/simulation/init: Initializes a new universe state with an array of celestial bodies.POST /api/simulation/step: Advances the simulation by a given time delta (dt) and number of micro-steps.GET /api/simulation/state: Retrieves the current spatial coordinates and velocities without advancing time.
See docs/api_reference.md and docs/physics_engine.md for deeper technical details.
Contributions, issues, and feature requests are welcome!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Distributed under the MIT License. See LICENSE for more information.