Contact Solver

Hello,

Comparing Box2D and Chipmunk contact solver, I noticed a small difference in the calculus of the normal and tangent mass factors :

chipmunk uses = 1/b1mass + 1/b2mass + 1/b1i * (r1 x n)² + 1/b2i * (r2 x n)²

which seems the way things are done in the literature.

Whereas box2d uses = 1/b1mass + 1/b2mass + 1/b1i * (r1.r1 - (r1.n)²) + 1/b2i * (r2.r2 - (r2.n)²)

This calculus is a bit more complex since it requires on additional dot product calculus. Is there any reason for this difference ?

Best,
Nicolas
Ooh, you've highlighted a lovely bit of Erin's work.

As far as I can tell, they do the same thing, and Erin's code is actually faster, when unrolled, despite it's complexity. By my reckoning, Box2D's version requires 5 additions and 7 multiplications, while Chipmunk's requires 5 additions and 9 multiplications. The reason is that the cross product is more expensive than dot products, and the square in Chipmunks code is actually a dot product, while the one in Box2D's code is just a multiplication.


EDIT: I am wrong.
The exact Chipmunk code is :

cpFloat r1cn = cpvcross(con->r1, con->n);
cpFloat r2cn = cpvcross(con->r2, con->n);
cpFloat kn = mass_sum + a->i_inv*r1cn*r1cn + b->i_inv*r2cn*r2cn;

cpvcross requires 2 mults and 1 sub, so here we have 3 mults and 1 sub for calculating the inertia factor for each body.

Box2D uses more ops than that, and seems to threat the normals coordinates differently.

Edit : I think that the cost of dot product ~= cross product in 2D
Doy, you're right, I'm being an idiot. How exactly I forgot we were in 2d is impressive. I've got to reign in the late night postings.
For reference sake, here's the Box2d code:

Code:
float32 r1Sqr = b2Dot(ccp->r1, ccp->r1);
float32 r2Sqr = b2Dot(ccp->r2, ccp->r2);
float32 rn1 = b2Dot(ccp->r1, normal);
float32 rn2 = b2Dot(ccp->r2, normal);
float32 kNormal = b1->m_invMass + b2->m_invMass;
kNormal += b1->m_invI * (r1Sqr - rn1 * rn1) + b2->m_invI * (r2Sqr - rn2 * rn2);

Functionality is equivalent, which is much easier to see with some trig than in component form - suppose theta is the angle between r1 and the normal. Assuming normalized normals, cross(r1, normal) = |r1| sin theta, and the Chipmunk expression is

Code:
cpFloat kn = mass_sum + a->i_inv* |r1|^2 * sin^2 theta1 + ... (sim for r2);

And since dot(r1, normal) = |r1| cos theta, the Box2d expression becomes

Code:
kNormal += b1->m_invI * ( |r1|^2 - (|r1|cos theta1)^2 ) + ... (sim for r2)
=> b1->m_invI * |r1|^2 * (1 - cos^2 theta1) + ...
(finally, using sin^2 theta + cos^2 theta = 1)
=> b1->m_invI * |r1|^2 * sin^2 theta1 + ...

So the two evaluate down to the same thing. It does look to me (without actually looking through the Chipmunk source, at least) that the Box2d version requires a bit more calculation, though.
I don't recall how I arrived at those specific formulas. I'll switch to the faster formulas. It should be a win since the velocity solver is the current bottleneck.

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