Weird Behaviour

I made a simple motorcycle/vehicle and it pointed out some odd behaviour, I'm not sure if it's me or a bug in the engine, but here goes anyway.

Code:
      {
         b2CircleDef sd1;
         sd1.radius = 0.5f;
         sd1.density = 2.0f;
         sd1.friction = 5.0f;

         b2BodyDef w1;
         w1.AddShape(&sd1);
         w1.position.Set(-0.75f, 20.0f);
         b2Body *b1 = m_world->CreateBody(&w1);

         w1.position.Set(0.75f, 20.0f);
         b2Body *b2 = m_world->CreateBody(&w1);

         w1.position.Set(0.0f, 20.6f);
         b2Body *b3 = m_world->CreateBody(&w1);

         b2DistanceJointDef jointDef;
         jointDef.collideConnected = true;
         jointDef.body1 = b1;
         jointDef.body2 = b2;
         jointDef.anchorPoint1 = b1->GetCenterPosition();
         jointDef.anchorPoint2 = b2->GetCenterPosition();

         m_world->CreateJoint(&jointDef);

         b2RevoluteJointDef revoluteDef;
         revoluteDef.collideConnected = false;
         revoluteDef.body1 = b3;
         revoluteDef.body2 = b2;
         revoluteDef.anchorPoint = b2->GetCenterPosition();

         m_world->CreateJoint(&revoluteDef);

         revoluteDef.body2 = b1;
         revoluteDef.enableMotor = true;
         revoluteDef.motorSpeed = 50.0f;
         revoluteDef.motorTorque = 1000.0f;

         revoluteDef.anchorPoint = b1->GetCenterPosition();

         m_world->CreateJoint(&revoluteDef);
      }


Just paste that into any of the tests, start the testbed and when the vehicle spawns try to grab the spinning wheel with the mouse, preferably closer to the outer edge, after a few seconds the whole system explodes and the two wheels become nulled, not reacting to anything.

Any clues?
I think the distance joint is over-constraining it.

If you take it out, it works fine. As far as I can tell it's redundant anyway.

Hope that helps.
-Matt
I'm still a bit disturbed by the bad behavior. Thanks for the post.
A pleasure as always, Erin, sorry I keep breaking it 😳
😀
I looked at this a while and here's what I found:
- the model went unstable with the motor on or off
- the model was stable when the distance joint was off
- the model could go unstable at large angular velocities when the distance joint was on
- the model was stable at low angular velocities
- if I turned off position correction, the system was stable but the joints easily fell apart

So it seems that the constraints fight with each other at large angular velocities. With position constraints disabled the bike stretches out quickly and the angular velocity is diminished.

I looked at the code a lot and didn't find any bugs, so I guess this is a limitation of the solver. Fortunately the work-around is simple: just remove the redundant distance constraint.
[As I finished writing this, I noticed that Erin already replied, correctly finding that there's not much to do about this bug - nevertheless, I'll post this since it may explain a bit of what's going on here and why.]

I noticed that with the motor off, despite the freakout that occurs, the whole assembly still seems to follow a proper ballistic trajectory. So here's one possible source of the problem: distance constraints usually at least minimally violate angular momentum/energy conservation, even if they are explicitly derived to conserve linear momentum. The reason is simple: angular momentum is a function of both position and velocity, and most solvers have separate position and velocity correction phases. There is no angular momentum conserving metric to project w.r.t. on either space that doesn't involve the other, so to conserve angular momentum you'd need a coupled solver. This is the same kind of problem as resolving interpenetration by projection without increasing potential energy - it's impossible to do without altering the velocity based on the change in position (and even then it's sometimes impossible). This angular stuff even causes problems in Verlet integration, where velocity and position changes _are_ unified - two particles connected by a stick will slow down over time because while the first order impulse is along the stick, there are higher order components perpendicular to it which are not corrected for. If you add an inwards force to each particle that more than counteracts the centripetal force (which, keep in mind, is zero if the thing is not rotating), the apparatus will speed up until the applied force is matched by the centripetal one, so you end up with an always-rotating object. I came across this in a game I was working on once (not exactly like that, but that's the essence of the bug), and it took a long while to figure out that the cause of the chaos was not a mistake in coding, but a "flaw" (avoidable, luckily) with the constraint approach as a whole.

I'm not sure that all this is the exact cause of the problem here, but when things get overconstrained (or inconsistent, which is almost the same thing, since nontrivial overconstrained joints will usually be slightly off with finite precision), you're almost dealing with the stick-fights-spring situation above, except that the "spring" (the redundant constraint) is very strong, so the equilibrium angular speed is extremely high. When I've done simulations and things blow up, it's almost always angular momentum at fault, because unsatisfied/fighting constraints always conserve linear momentum pairwise (hence globally). I've had minor luck reapplying angular momentum conservation at the end of the step, but it's tough to figure out how to project correctly onto the conserving region without botching your other constraints. I never quite got it working to my liking. It's possible that handling higher order approximations of the impulses could help, but I'm not sure since I never bothered checking. Didn't seem worth the effort just to allow myself to do Bad Things. 🙂

That said, non-conservation of any sort, especially the kind where energy creeps in, is a worthwhile thing to think about.
Thanks for looking into it guys 😀

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