Sign in to follow this  
onefatman

Rotating sprite causes P2 collision fail [SOLUTION]

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

Share this post


Link to post
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. :)

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.