Implementing static (ground) friction in a top-down world

Hey all!
I'm writing a simulator for a top-down world for a robot to interact with obstacles, and i'm stuck on a key feature. Box2D has fantastic support for everything i need EXCEPT static friction for the obstacles.

here's the problem:
My world is top-down, so gravity is off. I can use linear and angular damping to simulate kinetic friction (like in the car demos), but in my world I also need (anisotropic) static friction - the robot has to push on wheeled carts and tables, which resist forces depending on their orientation.

I've attempted to solve this problem by writing an "applyForceConstraint(point, force)" function which does the following:
1) project the force onto the perpendicular axes of the body
2) threshold each axial force vector based on body mass and appropriate friction coefficient
3) add an opposite impulse at the provided point if the threshold isn't met

I then call this function in my collision listener, and during the main runtime loop for each joint which can exert force on the body.

Unfortunately, this solution doesn't work very well.
For collisions, the object moves and then bounces back (which pushes the incident object away). This effect looks ridiculous when I drag objects into the constrained body with the mouse - the constrained body ends up jackhammering against the dragged body. Worse, if the object i'm dragging is massive enough, it somehow overwhelms my constraint and the object moves anyway (not a threshold problem).

Furthermore, when I try to drag the body with the mousejoint directly i also get a weird effect, in which the object pulls away from it's starting location when i drag, and then bounces back when i let go. It also slowly rotates in place to a fixed orientation, which I've noticed depends on where I click.

* This might be a separate issue, but from plotting the relevant vectors, i noticed that the force vector from the mouse joint's "GetReactionForce" doesn't always align with the vector between its anchor points. I spent a log time trying to track this down online and the Box2D source, but couldn't figure it out. Images attached (pink is GetReactionForce, and Blue is vector between anchor points)


This might be related too: I needed to multiply in an arbitrary constant to get the forces to cancel, which seems to depend on the density (mass). I thought that ApplyLinearImpulse accounted for mass though, so this is confusing as well.

So basically I'm stumped. 😐
My approach seems *qualitatively* right, but weird stuff happens with big objects, constantly applied forces, and mouse joints.

If anyone has any suggestions i'd be very grateful!

My implementation happens to be in python (latest from pybox2d SVN), and i've attached the relevant code snippits below.

Calling constraint for collisions:
Code:

def PostSolve
(self, contact, impulse):
    for fixture in [contact.fixtureA, contact.fixtureB]:
        if fixture.body.userData and fixture.body.userData.has_key('sprite'):
            sprite = fixture.body.userData['sprite']
            if type(sprite) == ConstrainedSprite:
                # extract collision force vector in world coords
                normal = contact.worldManifold.normal # from fixtureA to fixtureB
                if fixture == contact.fixtureA: normal = -1*normal # reverse for opposite fixture
                for i in range(contact.manifold.pointCount):
                    point = b2Vec2(contact.worldManifold.points[i])
                    nMag = impulse.normalImpulses[i]
                    tMag = impulse.tangentImpulses[i]
                    R = b2Mat22(math.radians(-90))
                    sprite.applyForceConstraints(point, force = nMag*normal)
                    sprite.applyForceConstraints(point, force = tMag*(R*normal))
 


Calling constraint for mousejoint:
Code:

if self
.mouseJoint:
    body = self.mouseJoint.bodyB # bodyB is always the target object
    if body.userData and body.userData.has_key('sprite') and type(body.userData['sprite']) == ConstrainedSprite:
        pos = self.mouseJoint.anchorB # anchorB is always on the target body, in world coords
        force = self.mouseJoint.GetReactionForce(1/self.TIME_STEP) # force exerted by the mousejoint on the body
        body.userData['sprite'].applyForceConstraints(pos, force*self.TIME_STEP)
 


And here's the code for doing the canceling:
Code:

def applyForceConstraint
(self, point, force = None, torque = None):
    """Applies the anisotropic force constraint according to the current constraint
    parameters.  Equivalent to static friction, and should be called whenever a force
    is acting on the body (even if the body is in motion).
    @param point A b2Vec2 point in world coordinates where the force is acting.  Assumed 
    to be from a contact experienced by the body, or applied by a joint.
    @param force A b2Vec2 describing a force in world coordinates
    """

    if force:
        gain = 1.33
        
# project force vector onto xNormal of constraint
        worldNormal = self.body.GetWorldVector(self.xNormal)
        xForce = Box2D.b2Dot(worldNormal, force) * worldNormal
        if abs
(xForce.length) < self.xMu * self.body.mass:
            # if subthreshold, apply opposite force to cancel it out
            self.body.ApplyLinearImpulse(-gain*xForce, point)
        
        
# project force vector to yNormal of constraint
        worldNormal = self.body.GetWorldVector(self.yNormal)
        yForce = Box2D.b2Dot(worldNormal, force) * worldNormal
        if abs
(yForce.length) < self.yMu * self.body.mass:
            # if subthreshold, apply opposite force to cancel it out
            self.body.ApplyLinearImpulse(-gain*yForce, point)
        
        
# debugging: just try to reverse the original force on the point
        # self.body.ApplyLinearImpulse(-gain*force, point)
 
I'm not sure exactly what behavior you're needing, but it sounds very similar to how a top-down-car simulation cancels lateral velocity to make the car only roll forward/backward. This might be useful: http://www.iforce2d.net/b2dtut/top-down-car
Thanks for the suggestion. It is similar, and that's where I initially got the idea. The difference is that for the car example you only cancel out velocities, but I want to also cancel out forces.

This is important because my simulator needs to handle quasi-static object interactions, which means the constraints have to work when the bodies are at rest or moving slowly, but experiencing large forces. Imagine pushing a wheelchair from the side. Even if you're killing the velocity at each timestep, as long as there's a force on it, it will slowly move.

my problem is that the force-killing approach seems really unstable. I've attached a clip of it in action here:
http://www.youtube.com/watch?v=xcHkl_f78Ls

The table is set up to cancel forces along it's narrow axis, but allow them on the long axis. As you can see, it sorta works, but it doesn't totally kill the big forces, and can be really erratic.

Also, it shouldn't be allowing the object to move at all along the constrained axis, but it actually ends up moving and then bouncing back. ❓ ❓
Perhaps you could look into how the friction joint works, and adapt it to work only along a single axis. Or if these objects should really really not be moving along a given direction, it sounds like you need a prismatic joint. The video seems to be 'private' btw.
Thanks for the quick reply, I'd forgotten about friction joints. They seem to be a little used feature.

Anyway i gave them a shot, and indeed they do smooth out my collision behavior, at the expense of being able to distinguish between two separate axes. I think i can get by with this for now though, and perhaps if I need quasi-static wheels i'll write a "wheelJoint" patch for Box2D (ambitious).

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