This n-body gravity simulator is a project I released on 14 May 2020. It allows the user to spawn
particles with mass and velocity and uses a simple algorithm which implements Newton's Law of Universal
Gravitation, calculating and applying the gravitational forces between every object.
While this method results in an inefficient n(n-1) time complexity, it also presents the most accurate
result. Algorithms that would run the simulation faster, such as Barnes-Hut, use approximations which
decrease the accuracy of the simulation.
The simulator uses a gravitational constant of 0.06674 and a particle diameter equal to the eighth root
of the mass.
Click on the banner image or the following link to view this program's itch.io page: n-Body Gravity Simulator
Features
- Compute Shaders. The use of compute shaders enables the simulation to handle over
5,000 simultaneous particles in the scene. Performance is best when no particles are in direct
contact, and many times more particles can be handled when this is the case. The compute shaders
currently use a brute force O(n^2) time complexity algorithm (what is O(n^2)?). This brute
force approach is the most accurate, but also the most resource intensive. However, even with this
naïve approach, the FPS-bottleneck is particle contact interactions, not gravitational force
calculations.
- User Interface. The user interface features a total particle count and mass readouts,
as well as separate sections for adjusting settings related to spawning particles via the Click
'N' Drag and Spawn Field methods.
- Object Types. By using the drop-down menu, it is possible to select pre-made
conglomerations of particles to spawn. These only work with the Click 'N' Drag method. Be
careful, some object types only work properly within certain mass constraints.
- Custom Spawn Fields. It is possible to customize the number of particles, their range
of masses, and the range of speeds of those particles within a spawn field prior to spawning.
Note: Particles are assigned a random value from
-SPEED
to
SPEED
in both their X and Y velocity vector components. This results in an
actual velocity magnitude of ±SPEED√(2)
(more on vector
magnitude).
- Speed of Light. With the defining of a finite speed of light comes a plethora of
potential strange near-speed-of-light interactions and behaviors. Currently, however, the only
effect is the existence of the Schwarzschild Radius. This
means
that once a particle exceeds a certain mass, it will be spawned as a black hole. Note: It is also
possible to force the spawning of a black hole of any mass via the object types drop-down
menu.
- Barnes-Hut Algorithm. This algorithm treats groups of far-away particles as a single
body, reducing the time complexity of computations from a brute force O(n^2) to
O(nlogn) (more on
time complexities). This algorithm is not used by default in the simulator at
this time.
- Toggleable Computational Modes. Computation can be toggled between Compute Shaders
Mode and CPU Mode (Compute Shaders Mode by default). While in CPU Mode, the Barnes-Hut algorithm can
be toggled (off by default).
Controls
KEY |
ACTION |
WASD |
Scroll Camera |
Scroll Wheel |
Zoom In or Out |
Click |
Spawn Object |
Click and Drag |
Spawn Object with Velocity |
Escape |
Menu |