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 Integrator used in this simulation is Symplectic (Semi-Implicit) Euler.
  • 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 a big error. Double Precision also has a bigger range for massive force (10-324 to 10308) while float has 10-38 to 1038

Roadmap

Major:

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

Minor:

  • Floating Origin for Flexibility
  • Non-Singleton Propagator for Multiple Simulations / Scene
  • Major Event Triggers (Body Collisions, Orbit Escape, etc.)
  • Multiple Integrators (Velocity Verlet, Symplectic Euler, Leapfrog + Yoshida)

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