lonelydatum Posted July 14, 2014 Share Posted July 14, 2014 I'm using P2. Is it possible to detect which side of the body my player collided with? Thanks! Link to comment Share on other sites More sharing options...
lewster32 Posted July 14, 2014 Share Posted July 14, 2014 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. Link to comment Share on other sites More sharing options...
lonelydatum Posted July 14, 2014 Author Share Posted July 14, 2014 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. Link to comment Share on other sites More sharing options...
lewster32 Posted July 14, 2014 Share Posted July 14, 2014 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. Link to comment Share on other sites More sharing options...
wayfinder Posted July 14, 2014 Share Posted July 14, 2014 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). lewster32 1 Link to comment Share on other sites More sharing options...
Recommended Posts