schteppe / cannon.js

A lightweight 3D physics engine written in JavaScript.
http://schteppe.github.com/cannon.js
MIT License
4.67k stars 709 forks source link

Multithreading #368

Open SET001 opened 6 years ago

SET001 commented 6 years ago

Is it possible to use more than one CPU cores for simulation calculation?

I'm experimenting with server to calculate and stream physic for supposedly multiplayer game. Everything working fine at this stage but I'm playing with limits. Having CPU i7-7820X X-Series with 16 cores, I generated world of ramdonly positioned in area spheres.

To simulate one step for world with 10k objects it takes ~900ms. (with no collisions)

When I split those objects to be simulated separatedly on each of my 16 cores - the result was 32ms. And then I can simulate 70k objects in about same time (even less) than 10k on single core. Profits are obvious - this making a way to extend my architecture, maybe in clouds.

The problem is - in my experiment, separated objects are calculated in separated worlds so coliisions between them wount be calculated. So I need multithreading supported internally by cannon.js library.

How hard would it be to implement? Where can I start?

schteppe commented 6 years ago

Probably difficult. As far as I know, the only way to do multithreading in JS is Web Workers. This means that each parallel job or api call needs to be asynchronous instead of synchronous... which would mean you would have to redesign the whole api :(

You could also try splitting your simulation into a grid, and each grid cell would run in its own thread. When an object crosses a cell border, move it to the new cell. You’ll have to figure out collisions between objects living in different cells, but if you got that working you have a pretty scalable simulation.

amir-arad commented 6 years ago

nodejs 10.5 has workers as an experimental feature. basically works (TM)

SET001 commented 6 years ago

@schteppe there are maybe situations when single object should be presented in few cells so that collision with different part of that object be calculated: collisionindifferentcells

But I think this is not a big problem and the implementation would not be difficult. Thank you for the advice )

robrohan commented 6 years ago

Can confirm @schteppe suggestion works. I started trying to implement something like it a few weeks ago and just got it to work: https://www.youtube.com/watch?v=plVz9GH81q4&feature=youtu.be at least for basic collision detection, I haven't tested anything hard core yet.

SET001 commented 6 years ago

Nice job, @robrohan . Can you provide a test where there would be about 20-30 objects randomly running in area, passing from cell to cell and colliding with each other?

robrohan commented 6 years ago

@SET001 It's still quite new and not working perfectly at the moment. Because of the way it was done originally, when a dynamic body jumps threads it loses it's velocity - stops, and then just has gravity applied and falls down. However, it does interact with the new bodies in the new thread, but it's all a bit loose at the moment.

untitled

(In the gif, a texture change is a different thread)

tugrul512bit commented 2 years ago

Can't javascript use C++ compiled dll to compute critical parts of collision? In C++, a single cpu thread can compute 30k AABBs' all-pair collisions at 60FPS using 3D octree-like structure. Even brute-force for 10k AABBs can be completed at 60FPS using AVX2/AVX512.