# P2 - Detect side from a collision

## Recommended Posts

I'm using P2. Is it possible to detect which side of the body my player collided with?

Thanks!

##### Share on other sites

Someone like Wayfinder will probably be able to further elaborate on this but I believe that because P2 uses arbitrary polygons for collision, it has no concept of 'sides'. There are however many mathematical approaches such as dot/cross products to determine the relative angle between two colliding objects, as well as of course just using the difference of the x and/or y positions to determine if an object is to the left, right, up or down of another.

##### Share on other sites

Thanks @lewster32

I wasn't aware that P2 didn't have a built in method for this. I'll probably use your suggestion by finding the differences in the x/y. I'm also going to look into the ContactEquation that is returned from the collision handler.

##### Share on other sites

I don't know whether P2 has a method for this or not, it could well do - but it seems slightly illogical and at odds with how I understand P2's collisions work. Four sides makes sense for AABB collisions, but not for rotatable polygons.

##### Share on other sites

The easiest way (although perhaps not the most reliable for outlier shapes with extreme measurements) would be to check the angle of the vector between the two centers of mass of the colliding shapes.

Looking for reliable data in the contact equations is sadly not a good way to deal with this specific problem - p2 is extremely optimized and will stop checking as soon as it is certain two bodies overlap (which can mean that when you hit a corner with some overlap, it will usually only return a contactEquation for one of the two points of collision), and in some cases will not detect a collision that is taking place (for example, when a "spike" goes through a rectangle without any of either body's vertices contained in the respective opponent's area).