schteppe / p2.js

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

Get local contact point for both bodies? #232

Closed AndrewRayCode closed 5 years ago

AndrewRayCode commented 8 years ago

I'm analyzing contacts in my scene by looping over world.narrowphase.contactEquations for player contacts. The problem is I don't know if the contact will be in bodyA or bodyB, and depending on which it is, I might the get local contact point (in contactPointA as documented here) from the "wrong" body. To calculate the normal for the "right" body, I have to do some translation math for every contact. Is there a better way to always get the local contact point for either body? I can assign an event listener to my specific player body, but I'm wondering if there's a nice way to do it with just reading a generic contact event and not knowing where it came from?

schteppe commented 8 years ago

I guess you always will have to check whether it's bodyA or B, and react differently depending on the result. Maybe you could make it nicer by implementing a handleContact method on each body instance, see the example here: http://www.iforce2d.net/b2dtut/collision-callbacks

When you know which one of the bodies you are interested in you may need to flip the normal. You can do this by just reversing the vector... Alternatively there could be a method on the event object for doing this. If you go for the handleContact method, pass in the reversed vector to it. Or the vector + a multiplier which is -1 or 1.

Other than this I'm out of ideas. I checked the box2d docs but its API don't help with this part either. If you have any suggestions on how to improve the API I'm all ears.