This N-Body Simulation is compiled in C++ and Raylib.
Feel free to use this repo for your own use.
- Barnes-Hut model
- Adjustable simulation bounds by modifying
minX
,maxX
,minY
, andmaxY
variables in the source code. - Dynamic visuals with color-coded particles representing energy levels (blue to red), grid overlays, and a contrasting UI.
- Inelastic collisions (modifiable via code)
- Spawn particles in-game with velocities and directions of your choosing
- Hold mouse down to select what mass of particle you would like to spawn
- Adjustable number of particles to spawn on start
- In-game gravity manipulation
- Multi-threading capabilities
The project was compiled with -Ofast flags, significantly increasing performance, but this means that your system must have AVX instructions. Most CPUs made in the last decade have AVX instructions.
- Left click anywhere to spawn a particle.
- Left click and drag to spawn a particle with a direction and velocity.
- Hold down Left click in order to select your particle's mass.
- Hold right click to pan the camera.
- You can also use the arrow keys to pan the camera.
- Use the scroll wheel to zoom in and out with the camera.
- The '[' button halves Gravity while ']' doubles it.
- Press C to toggle collisions.
- Press P to toggle whether or not you want to automatically spawn particles on start/restart.
- Press R to restart the simulation.
- Esc button to leave the game.
- This is an amateur project.
- This project is EXTREMELY terribly made.
- Particles in the center of large[1] clumps may exhibit instability due to unoptimized handling of extreme interactions. This is a known limitation and may be addressed in future updates.
[1] Smaller clumps collide perfectly correctly.
This is a todo list of improvements I am planning on implementing as the project grows.
- Barnes-Hut model (Allowing for millions of particles)
- Better parameter adjustment UI
- Coloured controls in UI for readability and accessibility
- Multi-threading support (currently it has a very basic implementation)
- Sound effects on collision
- Individual particle manipulation
- MacOS/Linux export
- Web export (Extremely difficult to do, decreases performance significantly, limitations and issues with multithreading)
This project contains a .vscode folder with configurations. Just make sure that you have Raylib and g++ (a C++ compiler) properly installed. Also, add this code to your .vscode/c_cpp_properties.json
file, create it if it doesn't exist:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C://raylib/raylib/src"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.22000.0",
"compilerPath": "cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
Also, these directions assume you have everything installed in the default location.
You can find Raylib's Notepad++ inside of the raylib installation directory. Then you can find notepad++.exe
under the npp
folder.
- Open main.cpp (with Raylib's version of NP++)
- Press f6
- You'll see a list of commands. One of them should be
SET CC=gcc
. Replacegcc
withg++
. If it's already like that, continue to the next step. - Click on 'OK'.
- You're done!
gcc
must be replaced withg++
because the project does not work with a C compiler (gcc
), and thus you must compile it with a C++ compiler (g++
) instead.
I have not compiled this project under any other version of NP++, so I suggest that you either use Raylib's NP++, or you search for help elsewhere online.
Unfortunately, if your IDE or text editor is not mentioned here, that means I have not compiled my project with it. You can find articles online detailing how to compile it on your system or IDE.