How to create a fixed joint - Solved with code

Hello,

I'm trying to create a fixed joint (removing all DOF for the second body). I know that the joint will never be perfectly rigid, that's fine for my application.

I think I got the math right, but when implementing and attaching two bodies, the whole simulation blows up immediately.

Did someone implement a fixed joint before? Could someone take a look at my equations?

I've created a PDF with the equations that I derived and attached it to this post. I know that the angle constraint works, it's the position constraint that blows things up.

Thanks for any answers!

Jorrit.
Figured it out.

J_x = [-1 -A_x 1 0 ] instead of [-1 A_x 1 0]. This results in an extra - in front of A_x in the equation for v2 and then it works!
If you have implemented this, can you possibly upload the finalized code for the sake of community?
No problem, I will in a couple of days (have to fix issues like center of mass not being the position of the object).
Everything can be downloaded from:

http://www.jrouwe.nl/fixedjoint/

Installation instructions are in the ZIP file. Let me know if you find any bugs.
This is on the todolist (or at least my mental one). Would you mind if I commited it to the SVN (zlib licence).
Yes please do! You can alter whatever you want, but it would be nice if the file still included my name somewhere 🙂

Please check the fixed point float implementation before submitting. I didn't do that. Also I only compiled on VC2008, so there might be warnings/errors with other compilers.
Another thing that might be interesting:

If you replace the b2DistanceJoint with a b2FixedJoint in the breakable body demo (and increase the iteration count)
then you get much cooler results (see http://www.box2d.org/forum/viewtopic.php?p=4757).
Awesome, thanks! I think breakable bodies is the perfect (only?) example for fixed joints. I think we might have to change the name though - people often say fixed when they mean static in Box2D terminology, or a host of other things, and it's best not to confuse that. How about b2WeldJoint?

FixedPoint may be broken anyway, but I'll have a look.

I haven't had a chance to look at your code yet, but I'll ask anyway. You paper suggests that you treat the joint as 3 seperate constraints to be solved separately. But for existing joints with two constraints, I got the impression they were solved simultaneously, so as to improve the speed of convergence of the whole system of constraints. Do you do that? Does it make a difference? I don't know, I've never really got the joint code properly.
I think in literature and other physics engines they're called fixed joint mostly, so I would keep the name as is.

As for solving the constraint as 3 constraints: I don't know if it is even possible to write down the equation so that it becomes 1 equation (I tried but couldn't get that to work). If you'll look at the code you'll see that the 2 position constraints are actually solved at the same time (you do the same calculation for x and y so you can use b2Vec2 for them and). The angle constraint is evaluated first and does influence the position constraint through the angular velocity term.

The actual math involved in the joint is pretty minimal, you only need 1 sin and 1 cos and some vector math:

Code:
   // Angle constraint: w2 - w1 = 0
   float32 Cdot_a = b2->m_angularVelocity - b1->m_angularVelocity;
   float32 lambda_a = -m_inertia * Cdot_a;
   m_lambda_a += lambda_a;
   b1->m_angularVelocity -= b1->m_invI * lambda_a;
   b2->m_angularVelocity += b2->m_invI * lambda_a;

   // Position constraint: v2 - v1 - d/dt R(a1) d = v2 - v1 - A w1 = 0
   float32 s = sin(b1->m_sweep.a), c = cos(b1->m_sweep.a);
   b2Vec2 A(-s * m_d.x - c * m_d.y, c * m_d.x - s * m_d.y);
   b2Vec2 Cdot_p = b2->m_linearVelocity - b1->m_linearVelocity - b1->m_angularVelocity * A;
   b2Vec2 mc_p(1.0f / (b1->m_invMass + b1->m_invI * A.x * A.x + b2->m_invMass), 1.0f / (b1->m_invMass + b1->m_invI * A.y * A.y + b2->m_invMass));
   b2Vec2 lambda_p(-mc_p.x * Cdot_p.x, -mc_p.y * Cdot_p.y);
   m_lambda_p += lambda_p;
   float32 lambda_p_a = b2Dot(A, lambda_p);
   m_lambda_p_a += lambda_p_a;
   b1->m_linearVelocity -= b1->m_invMass * lambda_p;
   b1->m_angularVelocity -= b1->m_invI * lambda_p_a;
   b2->m_linearVelocity += b2->m_invMass * lambda_p;

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