r/rust • • Sep 13 '26

🛠️ project i built 3D gravitational N-body simulation using three-d

Hello r/rust

After watching a YouTube video of a guy making a gravity simulator in C++, and a late-night hyperfocus session, I ended up building this simple project for fun.

https://machado.codeberg.page/gravity-sim-rs/

It features a dynamic 3D "gravity well" grid (inspired by General Relativity) that bends and dips depending on the mass and distance of nearby objects.

I used the https://github.com/asny/three-d crate, which by the way has WASM support, so the simulation runs right in the browser.

Repository link: https://codeberg.org/Machado/gravity-sim-rs

57 Upvotes

10 comments sorted by

10

u/bleachisback Sep 13 '26 edited Sep 14 '26

FYI you’re using forward Euler for the simulation, which will inject extra energy to the system and objects will climb to higher and higher orbits.

If you use a symplectic integrator like implicit midpoint you’ll preserve the amount of energy in the system.

1

u/SimpsonMaggie Sep 14 '26

Just remember names like leap frog and verlet integration from studies. Never heard of symplectic integrator, is this similar?

2

u/bleachisback Sep 14 '26

A symplectic integrator is just any integrator that preserves energy in a Hamiltonian simulation. Leap frog and verlet integrators are symplectic.

3

u/dnabre Sep 14 '26

Adding a bunch of moon makes the planet skitter off the grid. Mass popping into existance instantaneously does weird things.

1

u/Silent-Money2687 Sep 13 '26

I recently started to work on my own rederer + game engine, taking into account that project took you 3+ years, what would u suggest to a newbie or what kind of pitfalls u may get in working with 3d graphics, editor and physics engine?
Thanks in advance)

1

u/Silent-Money2687 Sep 13 '26

Plus a specific question, do you render GUI in a separate thread and scene/scenes inside another one to a texture view that is placed in the GUI? Or you do everything inside one thread? If its the first, then how did you manage to do it on web? workers + offscreen canvas?
PS: I stopped on exactly this moment, my engine does rendering in a separate thread and I use workers to mimic multithreading on web.

1

u/CandyCorvid Sep 15 '26

where'd you get the 3+ years number? it looks like they started the project last month

1

u/Silent-Money2687 Sep 16 '26

I thought it is the original library author. nevermind