Jump to content

Rotating sprite causes P2 collision fail [SOLUTION]


onefatman
 Share

Recommended Posts

Hey all,

 

Just a quick heads up to share an odd solution to a problem that had me tearing my head out for a couple of days, thought it may come in handy for anyone else in the same boat.

 

While working on a game where I was using P2 physics to detect a collision event between 2 physics bodies, I found that whenever the target sprite was rotated further than a certain amount all of my collision events would silently fail. I spent AGES trying different collision methods and none of them worked, when trying to implement a hack-ish workaround that I found on the forums (can't remember where) I realised that my collisions were now working fine, even though the method I was using returned something to the effect of "this doesn't work".

 

Long story short - I found that by resetting a physics body's AABB data after every change in rotation made the collision events work again:

//attackingThing.body.onBeginContact.add(reportSomething, this);//function rotateThing() {    //    thing.rotation += 1;    // reportSomething fails unless you do this    thing.body.data.updateAABB();}

If anyone can enlighten me as to what/why/how this works, that'd be awesome.

 

Phase on, dudes!

 

OFM

Link to comment
Share on other sites

For a centered, circular shape this makes no difference, but the way the collision handler keeps cpu use low is to (in a first step, the broad phase) generate candidate pairs (of shapes that may collide) from the overlap of bounding boxes of ALL shapes in the world - that's a fairly cheap test to run so it can afford to do it every frame. If you have a shape that's not a centered circle and it rotates, it now has a different bounding box, and updateAABB stores these values. Your collisions were wonky because the handler was testing against the old bounding box.

 

I think setting the rotation should update the AABB automatically, but maybe only when the setter is used instead of setting the value directly? I don't actually know why it didn't happen in your case. :)

Link to comment
Share on other sites

Wait - I lied! And also omitted some information. Yep the shapes are polygons that I've created in PhysicsEditor, but also I was using this code to rotate them:

thing.angle = x;thing.body.angle = x;

My bad! Not sure if that's the reason that the error was appearing, or if it's just an alternate way of doing it that should have also worked?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...