schteppe / p2.js

JavaScript 2D physics library
Other
2.64k stars 330 forks source link

GridBroadphase #274

Closed pkieltyka closed 7 years ago

pkieltyka commented 7 years ago

Hi @schteppe thanks so much for this amazing engine, its very well designed and the code is easy to follow.

I was just wondering why you removed the GridBroadphase in https://github.com/schteppe/p2.js/commit/e90c672 ?

I notice you still have it in cannon.js, and for certain game styles a grid would be much better than a sap broadphase I beleive. Also the benchmarks in https://0fps.net/2015/01/23/collision-detection-part-3-benchmarks/ show the p2-grid performed quite well.

Also, what do you think about this... let me know if you think this would be reasonable to augment p2 to support this kind of world behaviour, I'm just getting into this stuff but this is what seems logical.

In my multiplayer space shooter I have a very large world, and in order to reduce the amount of work necessary across the world, I'm going to partition the entire game into a grid of large cells, and only compute the step/render for the grid the player resides, and maybe a few adjacent cells to the player. Then, the networking would similarly only send events for objects in the vicinity. This is beyond just a GridBroadphase collision test, it would entail bucketing all bodies. However, gridbroadphase would still be helpful I believe within those spaces.

if you have any insights or tips that would be awesome. thanks.

pkieltyka commented 7 years ago

perhaps even making a SpatialBroadphase class and using https://github.com/mourner/rbush for the backend could work out well

schteppe commented 7 years ago

Hi, There were a number of reasons why I removed GridBroadphase.

See https://github.com/schteppe/p2.js/issues/199

If you want to add it back, PRs are welcome.

Optimization idea: static bodies don't have to be added/removed to bins each step.

pkieltyka commented 7 years ago

@schteppe thanks for the info and I think you're right, a dynamic tree algorithm like a quad tree or r-tree would be much better. Perhaps one day I'll have a chance to contribute it.