OpenTK Gravity Simulation

N-body particle simulator with efficient CPU and GPU computation with a custom renderer.

This project has three main components: CPU computation, GPU computation, and rendering. For CPU computation, I use the [Barnes-Hut algorithm](http://arborjs.org/docs/barnes-hut) in 3 dimensions for a significant speedup. A naive particle simulation requires O(n^2) calculations to update the simulation. The BH algorithm speeds this up significantly by treating sufficiently distant and tightly grouped particles as a single particle for force calculations. Speeding up to something like O(n * log(n)). A size-distance ratio is set to determine how far and tightly bound the groups need to be. The tradeoff is simulation accuracy. A higher ratio is faster but less accurate. The algorithm works by building a spacial octree each time step where each node contains the total mass and center of mass of all its children, and the leaf nodes each contain a single particle. After the tree is built, the force on each particle is calculated by depth first traversal of the tree until a sufficiently small and distance node is found (based on the size-distance ratio), or a leaf node is found. The force for that node is applied to the particle. The traversal moves on to the node's sibling, skipping the computation for all its children. The GPU computation is just a compute shader that updates the particles with the naive algorithm. It still runs very fast because of the massive parallelization for large numbers of particles, but slows down significantly compared to the CPU for very large amounts. Rendering is done using OpenTK, which has bindings for OpenGL for C#. The particles are rendered as points with a radius, and the simulation has performance issues long before the rendering does. I also included some cool effects like redshift based on particle speed towards or away from the camera.