b2WeldJoint unexpected behavior

Hello!

I've been having a problem with the weld joint acting kinda strange. I have provided images of the problem as well as Cord.h for the testbed code and a compiled testbed.exe with the example included. But first I'll try and explain the issue.

What I want to do is to dynamically create a cord/rope kind of line of bodies connected by weld joints. The cord always tries to reach for a target point and "draws" new segments towards that target until it reaches it. The target point is the mouse position (simplified to x-axis in testbed example) so the rotation on the created segments can be anything.

What I experienced after making this in my engine was that the line could make a "jolt" motion and sometimes freak out. After spending some time trying to debug my own code, working my way down the chain of computations, I ended up finding that it was caused by some error with the angle. I then tried setting the angle for all created segments to be 0 and created thousands of segments that worked perfectly (straight line). But when I set the rotation to be pi*2 on all created segments, the cord would make a loop before it would continue on normally as a straight line.

With that I tried pi*4 which would make two loops before becoming normal. And with minus pi*2 it would make one loop (the other way) and continue. Minus pi*4 would make two loops the other way, etc. As you can see there is something weird with the different angles. It's like it "unwinds" its rotation.

I was unsure if this was caused by something in my own code (since I've wrapped box2d in other structures) so I recreated it in the testbed using only box2d objects but the problem was still there.

Well, enough text. The testbed example attached is a simplified version of what I'm trying to do but it still shows the issue.

Expected behavior:

Image


Resulting behavior (when changing rotation between segment creation):

Image


The way you replicate the last image is by changing between the different angles (keys 1-4) between moving the target point (which in turn creates segments trying to reach the point). The "Current rotation" is the angle the new segments will have after it's made.

Hopefully it's just a simple miscalculation by me but I'm past the point where I can understand why this is happening. That's why I'm posting here and I would be grateful for any help I could get.
Try changing:

Code:
inline float32 b2Body::GetAngle() const
{
   return m_xf.q.GetAngle();
}


To:

Code:
inline float32 b2Body::GetAngle() const
{
   return m_sweep.a;
}
After changing the code it seems to be working as expected. I've tested with the testbed example and my engine and I've yet to see the issue happen after the change.

Can I continue using box2d with that change? Are there any tradeoffs using 'm_sweep.a' instead of 'm_xf.q.GetAngle()' ?
That was a bug introduced in 2.2.0. The fix is the correct fix.
Alright. Thank you for the quick response and fix. =)

Original thread: https://box2d.org/forum/viewtopic.php?t=7546