schteppe / p2.js

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

A way to add own collision strategy #333

Open Meldiron opened 5 years ago

Meldiron commented 5 years ago

There is event "postBroadphase " on world which gives collision pairs. Is there a way to change this array of pairs before world does recalculations? postBroadphase is just an event so I cant return something to change it (I think at least).

Let me know please :)

schteppe commented 5 years ago

You could probably just add and use your own broadphase class. Extend the broadphase class (just like SAPBroadphase does) and then pass it to the World like this:

var world = new World({
    broadphase: new MyCustomBroadphase()
});

You can do whatever you want inside postBroadphase, including modifying the pairs array. The pairs array is passed into the event handler (in evt.pairs) so you can just modify it directly.