Skip to content

This is a Barnes-Hut N-Body gravity simulation. I iterate over the project and try to improve it as much as i can. its for fun and learning

Notifications You must be signed in to change notification settings

mazzeaar/gravity_sim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NOTE: WILL PROBABLY ONLY WORK ON APPLE SILICON

Gravity Simulation

This project is an implementation of a 2D gravity simulation using the Barnes-Hut algorithm and a quadtree data structure. The simulation is written in C++ and utilizes the SFML library for rendering.

Please note that the current implementation is not optimal, and there are numerous improvements that can be made.

  • use MVC design pattern
  • fix the quadtree instead of reconstructing it
    • tried it, i think its slower but i have to look at it again some other time
  • change datastructure from an array of objects (AoO) to an object of arrays (OoA), this should improve memory access
  • lastly implement real multithreading and if possible do calculations directly on the gpu
    • kinda did it i think, it runs with $5-7$ fps with $n=500'000$
  • some graphics API, not SFML lol
  • implement spacial hashing and some integrator for the physics

Explanation

The Barnes-Hut algorithm is a method used to compute the gravitational forces between bodies in a system, where each body has a mass, position, and velocity. A quadtree is used to divide the 2D space containing the bodies into distinct sections. This allows the algorithm to calculate gravitational forces more efficiently.

Animation of the Algorithm

This simulation is done with $n=400'000$ bodies, $\theta=0.8$, and a time step of $\Delta t=0.1$. It takes about $1$ second to calculate the forces and update the positions of the bodies for each frame. The simulation is run for $\pm1200$ frames which took about $25$ minutes to complete.

./images/galaxy.gif

Issues

The Barnes-Hut algorithm is not perfect; therefore, this simulation only provides an approximation of a real gravitational system. In a real $2D$-gravitational field, the gravitational force is not calculated with the inverse square law $F_g = G \cdot \frac{m_1\cdot m_2}{r^2}$, but rather with the inverse distance law, where $F_g = G \cdot \frac{m_1\cdot m_2}{r}$.

Unfortunately, the two-dimensional gravitational force appears incorrect and is not as interesting as its three-dimensional counterpart. As a result, this simulation represents just a $2D$ slice of a $3D$ gravitational field.

Getting Started

To run the simulation, you will need a compiler that supports C++11 or later and the SFML library.

Prerequisites

  • GCC, Clang, or another C++11-compatible compiler
  • SFML library

Building and Running

To build and run the simulation:

  1. Clone the repository:
git clone https://github.com/mazzeaar/gravity_sim.git
  1. Navigate to the project directory:
cd gravity_sim
  1. Build the project:
cmake .
  1. Run the simulation:
make run

or

make && ./gravity_sim

Honorable Mentions

  • myself
  • gpt4
  • the guy who invented the barnes-hut algorithm

License

Imagine having a license lol

About

This is a Barnes-Hut N-Body gravity simulation. I iterate over the project and try to improve it as much as i can. its for fun and learning

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published