nbody-wasm-sim
nbody-wasm-sim copied to clipboard
Refactoring and various tweaks
I spent some time refactoring the structure used for the force calculations. This led to significant performance improvements (with 500 bodies f.e., my fps count went from ~30 fps to ~70 fps before collisions wreck havoc). I also made use of the nalgebra feature to convert its types to glam conveniently.
This refactor also are baby steps towards potential parallelization. I'm not sure it's possible to use rayon currently, but I'd like to try, although compute shaders would be ideal but I have no experience with them.
I also fixed random things clippy was complaining about.
Woah! Awesome work!
I'll do some verification on my side and merge it in when I have a chance. :)
The only thing truly missing from making this extremely performant is using wgpu's compute shaders for the gravity calculation, but Rayon would be an interesting alternative. Give it a go! :)
Not sure if I agree with the change to separate Body and add a new structure called Particle. They are supposed to be the same thing, so I'm trying to figure out what the reasoning there is. Otherwise, everything looks incredible. Ideally I don't want the separation there, and I'm sure we could figure out a way to parallelize gravity computation without the additional structure.
Off the top of my head, there is a 3D nbodysim (https://github.com/timokoesters/nbodysim) and they make 2 arrays - an old and a new - where the old is passed to the GPU compute shader for reference while the new array is filled up. I'd want to take a similar approach :)
Not sure if I agree with the change to separate Body and add a new structure called Particle. They are supposed to be the same thing, so I'm trying to figure out what the reasoning there is. Otherwise, everything looks incredible. Ideally I don't want the separation there, and I'm sure we could figure out a way to parallelize gravity computation without the additional structure.
Off the top of my head, there is a 3D nbodysim (https://github.com/timokoesters/nbodysim) and they make 2 arrays - an old and a new - where the old is passed to the GPU compute shader for reference while the new array is filled up. I'd want to take a similar approach :)
My reasoning for the separation was that it will make it easier to modify the particle structure either for parallelization or any other optimization work like using a Barnes-Hut algorithm. Ideally, it would be in a separate crate but since it's such a simple structure I don't think that it is necessary. It also decouples it from anything to do with Rapier, which I believe is a good thing because it means this could be reused in any project.
But of course as you said, since they basically are the same thing (Particle is just a simple utility struct, it would also be possible to only use the trait AsParticle), they could be merged into a single structure with no problem.
I would want a resolution to the separation of Body and Particle before merging.
I'd merge if the request came with parallelism (then the need is apparent), but since it doesn't, this would muddy the current codebase with an unnecessary constraint. Hope you understand. I think this is excellent otherwise.
You can also merge them back together and I will accept it. :)
I merged Body and Particle as a single structure, I wasn't able to find a way to make rayon work with wasm. I'm sure there is a way but I didn't want to dwell on that too much as the better solution is using compute shaders anyway.
I also made a 'response' function for the BodySet to make the interaction between rapier and the gravitational computation more apparent instead of using the 'Rigidbodies' trait. I believe it's a better option, but I can revert back to the trait if you prefer it.
I want to thank you for your open source contributions. I'll take a look shortly but you've done a great job and I'm happy to see the contributions. :)
You're welcome! It's a fun exercise for me as I'm currently working on a similar project using bevy and rapier so if my work can contribute to other projects than it should!