N-Body Simulation
High-throughput gravitational simulation using Barnes–Hut acceleration.
Overview
This project simulates gravitational interactions between over one million particles in real time. The goal was to maximize throughput while preserving physical correctness and numerical stability at scale.
The simulation demonstrates the emergent behavior of gravitational systems, from galaxy formation to planetary orbits, all computed with high fidelity on consumer hardware.
Architecture
- Barnes–Hut quadtree for force approximation (O(n log n))
- Center-of-mass aggregation for distant nodes
- Softened gravitational potential to avoid singularities
- Parallel force evaluation using Java concurrency
Implementation
The simulation uses a fixed timestep integrator with decoupled position and velocity updates. Tree reconstruction occurs each frame to ensure correctness under chaotic motion.
Key optimization techniques include spatial hashing for collision detection, memory pooling to reduce garbage collection overhead, and cache-friendly data structures that maximize CPU throughput.
Performance
Sustains 144 FPS with over one million particles on consumer hardware. The Barnes–Hut algorithm reduces computational complexity from O(n²) to O(n log n), making real-time simulation of massive systems feasible.
Future Work
- GPU acceleration via OpenCL or Vulkan compute
- Adaptive timestep integration for improved accuracy
- 3D octree extension for full spatial simulation
- Collision detection and particle merging
- Relativistic effects for black hole simulations