top-down friction that depends on the fixtures attached

The current top-down friction joint (FrictionJoint) couples the force and torques between two bodies, but clips the maximum force and torque that one body can apply to the other. Once the force/torque is exceeded, the two bodies slip relative to one another.

This is the behavior I want, but I need it to be aware of the fixture attached on a body. For example, if two objects are not overlapping at all, there can be no friction coupling between them. Also, suppose two squares, A and B, are slightly overlapping, but their centers are not. If square A rotates about its center, the square B should also rotate about A's center. The current behavior of the FrictionJoint is that the squares both rotate about their respective centers.

I have a few questions about this.

It seems like no Box2D joint relies on fixture information. The geometry of a object is used only for computing contacts and collision forces, but not the forces used to maintain joint constraints. Is that right? If so, would it be very unnatural to extend the FrictionJoint so that it does rely on fixture information to produce the behavior I want?

Is the right approach to try to implement this desired behavior outside the library? For example, to compute the friction force between two bodies, first compute the overlap of their fixtures. Then sample a few key points in the overlap and couple the forces between the two objects. The correct torque should fall out of this.
You can use the BeginContact and EndContact callbacks of your contact listener to keep track of when two fixtures are overlapping.
Then you could adjust the maximum force/torque of the friction joint depending on whether the two fixtures are currently overlapping or not.

If you want to gradually adjust the amount of friction depending on the area of the two fixtures that is overlapping, that will require a bit more work. You might find this page useful, which uses polygon clipping to find the overlapping portion of two polygons, and the centroid of that overlapping area: http://www.iforce2d.net/b2dtut/buoyancy

Once you have found the centroid of the overlapping area, you could probably do away with the friction joint and apply a braking/drag force and torque yourself. That is, a small linear force in the opposite direction to movement of the centroid point (you can use b2Body::GetLinearVelocityFromWorldPoint to find the current velocity of the centroid point in each body).

To apply a torque to brake the rotation may be trickier since Box2D only lets you apply torque about the center of mass. You could perhaps just use the friction joint for its rotational part, and set the linear part (maximum force) to zero if you're doing it yourself.

If you really wanted to get fussy, you could take the bounding box of the overlapping area between the two fixtures, and sample points in a tight grid within that region. For each point that falls within the overlapping area of the fixtures, you could apply a small linear force in the opposite direction of current movement as mentioned above. This is what is happening in real life, so it would get you about as accurate a result as could be expected.
thanks for the helpful reply.

This approach basically makes sense to me, but it feels like it assumes that one of the bodies is still. The drag force should only apply if the velocity of one object relative to another is changing.

Is there a change that applying these forces will lead to some bad interaction with the solver? With the real friction joint, the forces are applied in
Code:
SolveVelocityConstraints
, which might be called multiple times within one time step. Should I aim to do something similar?
goretkin wrote:
The drag force should only apply if the velocity of one object relative to another is changing.

ah yes, quite correct 😀
You would need to look at the relative velocity between the bodies at that point, and use that as the direction of the drag force.
No, using ApplyForce should play nicely with all other constraints being solved.

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