Simulating Top Down Friction

That would make sense but it's weird..

So the force is usually constant, unless the object is standing still? I'd imagine that gives trouble with low speeds ^_^, how do you distinguish between "standing still" and "having a very low speed".

Thanks 😀.

So for fiction in stead of damping, the code would have to be like:
Code:
const frictionForce = 0.5;  //Some constant, I have NO idea what it should be to make it look right, but it's the result of frictionConstant * normalForce.
var v = bb.GetLinearVelocity();
if(Math.abs(v.x) > 0.1 && Math.abs(v.y) > 0.1){ //Check if the object is moving.
   v = v.Normalize();
   v = v.Multiply( -1 * frictionForce); // (apply the constant force, in the opposite direction);
   bb.ApplyImpulse(v, bb.GetPosition());
}
You have to understand, in the real world, time is continuous. That means it makes perfect sense applying a largish force up until the time that force brings something to stop. The given formula is of course just an approximation, but it does still hold at small velocities: you still see the largely constant magnitude of the friction force.

But in the world of Box2D, forces last for the entire length of a frame, so it suddenly seems absurd to apply constant forces like that as you might end up actually reversing the direction of an object with very low velocity. The solution is that you need explicitly check for that case and set the speed to zero.

Disclaimer: in practice you don't need check, as no one can notice the difference anyway at such low speeds, and it will keep on reversing directions so is essentially stationary. So everyones advice before me is correct, just misleading for why it is correct at low velocities.

If it makes you feel better, there is still friction of the same magnitude even after it's come to a stop. This "static" friction behaves slightly differently, e.g. you can lean on an object without affecting it's velocity, but a good push will. Basically friction doesn't behave like a standard force, as it is not additive, so the fact we are expressing it as one is what leads to these discontinuities at zero and other malarkey.
right, so I agree with what boristhebrave said, but I wanted to add that android's discomfort is well founded - any time you see a discontinuity or an infinity in a quantity in any physical "law" it is a good bet that as you near that point the "law" starts to become less and less true. In the case of Coulomb friction it turns out that for many purposes we don't need to worry about it (though there are problems where the law's failures shine through).
BorisTheBrave wrote:
This "static" friction behaves slightly differently, e.g. you can lean on an object without affecting it's velocity, but a good push will.


Oh yeah, that's right. So with either method (damping or 'real' friction) the object will start to move, even with a force that is smaller than the friction force? (Which shouldn't be the case right?) ..

Actually, how does this all work in Box2D (when not used "top-down")? Is the friction of objects touching each other at all similar to real friction? (I'm talking about the friction set by b2Body.friction).
Box2D friction works like this:

- Give the applied forces, compute the new velocity of a body (ignoring friction and other constraints).
- Compute the friction impulse necessary to halt the tangential motion of the contact point.
- Clamp the friction impulse so that it obeys Coulomb's friction law.
- Apply the clamped friction impulse.

You can use this for top-down friction, skipping step one.

- Compute the impulse necessary to halt the motion of the center of mass. This is a vector.
- Clamp the impulse magnitude according to the friction coefficient and weight.
- Apply the clamped impulse.

You will need to do something similar for rotation. First you need to pre-compute some radius value for the body (based on the geometry). This can be approximate.

- Compute the rotational impulse necessary to halt the rotation of the body. This is a scalar in 2D.
- Clamp the rotational impulse according to the friction coefficient, body radius, and weight.
- Apply the clamped impulse.

You can do this outside of b2World::Step and it should give decent results.

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