Skip to content

ykaylani/najm.dot

Repository files navigation

najm.dot

High-Performance N-Body Simulation

This simulation leverages Unity's Data-Oriented Tech Stack (DOTS) and the Barnes-Hut algorithm to efficiently compute gravitational forces for tens of thousands of bodies in real-time.

Features:
    High-Performance Barnes-Hut Algorithm: Reduces complexity from O(n²) to O(n log n) using a morton-encoded octree built with a parallel radix sort.

    Data-Oriented Design: Struct-Of-Arrays used for Processing to achieve High-Performance while also converting the SoA into AoS for easy editing.

    Physically Accurate Initial Conditions: Generates stable Keplerian orbits from standard orbital elements (semimajor axis, eccentricity, inclination, etc.).

    Massively Parallel CPU Computation: Fully multithreaded using Unity's Job System and Burst compiler for optimal performance.

    Custom Tooling: An intuitive custom inspector for managing simulation parameters and body properties.

30,000-Body Plummer Simulation 30,000-Body Realtime Star Cluster (Plummer Model, θ = 0.5)

Getting Started

Firstly, clone this repository:

git clone https://github.com/ykaylani/najm.dot.git

Secondly, to open it in Unity:

  1. Open Unity Hub.
  2. Click "Add Project" and select the cloned repository folder to open it.
  • The version this project was developed in is Unity 6000.1.1f1 and that is the version where the best experience lies, but all Unity 6 versions will most likely work with this.

Finally, to open the example Scene in your Unity editor:

  • Navigate to Assets/Scenes then Click on the SampleScene to open it!

Running The Simulation

  • Press the play button in the Unity editor

Technical Details

  • The Barnes-Hut Algorithm plays a role in making the simulation more performant by making the physics calculations O(n log n) instead of O(n²).
  • Double Precision was used instead of float because of Small Precision errors accumulating over time, causing approximation error over longer periods of time. (doubles also provide a bigger range than floats which allows for things like realistic masses)

Roadmap

Major:

  • Delinking Propagation from Unity's FixedUpdate (Predictive Simulation)
  • Custom Mesh Generation for Orbit Trails
  • Fast Multipole Method Implementation

Minor:

  • Floating Origin for Flexibility
  • Non-Singleton Propagator for Multiple Simulations / Scene
  • Major Event Triggers (Body Collisions, Orbit Escape, etc.)
  • Yoshida Integration (4th and 6th Order)

Credits and Resources

This project was made possible with the help of these amazing resources:
Barnes-Hut Algorithm Documentation on arborjs.org
Arcane Algorithm Archive's Verlet Integration Documentation
Semi-Implicit Euler - Wikipedia
Orbital Mechanics - Wikipedia
Perifocal coordinate system - Wikipedia
John Sietsma - Morton Order
W3 Schools - DSA Radix Sort