Saturday, April 28, 2012

Box2d Not Staying Put with Prismatic Joint

I had a problem where i wanted a dynamic b2Body to not be affected when another moving dynamic body would hit it no matter how hard.  I set multiple joints to reinforce that I did not want that body to move, but it still moved once in a great while.  It would take several ticks for it to get back to it's proper position and by that time, the app pretty much crashed.

The image on this Box2d Joints Tutorial touches on this problem.  The author says that the heavier object is not corrected much by the impulses and so it can take many steps to correct the mis-alignment.  Similarly in a real world situation if this were to happen one of the objects would give in.  In Box2d objects don't just give in.  The author says to make the densities of the two bodies the same.  Doing that however, worsened my problem for me.  Doing the opposite, the stationary body having 10000 times more density than the moving object, also did not seem to help.

Finally after accepting that I would not be able to resolve this problem I came up with a workaround.  I calculate where the stationary body should be and if it is not there, I manually move the b2body there like so:
if (stationaryBodyPoint != calculatedPoint) {
    body->SetActive(FALSE);
    body->SetTransform(calculatedPoint, 0);
    body->SetActive(TRUE);
}

This seems to work for my app.  You'll still see that the stationary body does move, but quickly readjusts.



No comments:

Post a Comment