Use Modulo for Box2D Rotation

Not sure if this is really a bug but while playing with Box2D, which btw is really amazing, I stumbled upon a nasty behaviour that stopped my sprites from working like expected. Just wait few (10+) seconds to see what I am talking about.

Rotation Bug

Someone also posted more details about it. The solution is simple: Use modulo within the enterFrameHandler to assign proper values to the rotation property.The movie is using a b2Body + attached Sprite and a b2RevoluteJoint motor responsible for the rotation + DebugDraw enabled. Suddenly after about 10 seconds the attached sprite stops rotating. Thanks to great support of the Box2D community I finaly realised what was actually going on. Apparently when the Movieclip rotation values pass 32767 (which is about 91 rotations) it either stops rotating or behaves unexpected. Although this is not a Box2D specific issue I believe that the AS3 port of Box2D will offer real world scenarios for this bug.

Actionscript:
  1. public function enterFrameHandler(e:Event):void
  2. {
  3.   b2world.Step(2/15, 10);
  4.   for (var bb:b2Body = b2world.m_bodyList; bb; bb = bb.m_next){
  5.     if (bb.m_userData is Sprite){
  6.       bb.m_userData.x = bb.GetPosition().x * 30;
  7.       bb.m_userData.y = bb.GetPosition().y * 30;
  8.       bb.m_userData.rotation = (bb.GetAngle() *(180/Math.PI))%360 ;
  9.     }
  10.   }
  11. }

To fix this simply use Modulo within the enterFrameHandler!

3 Comments

  1. Bookmarks about 360 2008-08-23, 11:00 pm

    [...] - bookmarked by 5 members originally found by valenessa on 2008-08-06 Use Modulo for Box2D Rotation http://manfred.dschini.org/2008/07/21/use-modulo-for-box2d-rotation/ - bookmarked by 1 members [...]

  2. Ollie 2008-09-14, 2:34 pm

    Hey - I was having the same problem, which I assumed was a Box2D bug, but this solved it for me … thanks for posting!

  3. Recent Faves Tagged With "box2d" : MyNetFaves 2008-10-10, 2:21 am

    [...] public links >> box2d Use Modulo for Box2D Rotation First saved by SiloinSiproche | 2 days ago Box2D Hourglass First saved by lovlygrl101 | 2 days [...]

Add a Comment