Simulating Top Down Friction

Hi Guys,

I've looked around this forum a couple of times for ideas on how to simulate Top-Down friction before its implemented in version 2.2.. The best I've found so far is

'As for top-down friction, it's not terribly difficult to simulate that yourself by applying an impulse at every timestep.'

So I've tried to work on this but with no success. Would somebody be able to explain to me the best way of going about this?

My best effort so far has been this:

var v = bb.GetLinearVelocity();
var vX = (v.x*-1)*0.99;
var vY = (v.y*-1)*0.99;
bb.ApplyImpulse(v, bb.GetPosition());

but this goes horribly wrong.

This is only my second day with Box2d so apologies if its a bit of a stupid question, I'll get the hang of it soon 🙂
This is just a dumb coding bug, not anything box2d related: you are setting vY and vX to the correct value, then ignoring them and using v instead.

After that it should work more sensibly. But impulses and velocities are different units, so you may need to change the .99 value to something else.
BorisTheBrave wrote:
This is just a dumb coding bug, not anything box2d related


Don't make Boris angry, you won't like him when he's angry.
Thanks guys, I figured I was so far off the mark that the thinking behind my original code was just way off so didnt bother checking it too much, kind of embarrassed now. Anyway made those changes and changed 0.99 to 0.1 and it now works great 🙂

thanks for your help
pault107 wrote:
Don't make Boris angry, you won't like him when he's angry.


I didn't mean it like that 😐
haha, was good to have my mistake pointed out to me 🙂 all working quite nicely now so thanks for your help
How does friction work actually? How is it different from damping (when implemented like this)?
Technically speaking, the code snippet above produces damping, not friction. The difference is that the speed decay with damping is exponential, whereas with friction you hit zero speed in a finite time. Sometimes it can be a very bad idea to use damping if what you really want is friction, but often the approximation is good enough. Then again, they're both very easy, so it's probably worth figuring out which one you actually want and just using that.

The forms of the force functions explain this better, probably:

Friction: force = frictionConstant * normalForce; (since normalForce is usually constant for a body, this means that there's just a constant slowing force, which gives parabolic motion)
Damping: force = dampingConstant * velocity;

It might not be entirely clear from that code snippet that a damping is being applied, but a vNew = vOld * (1.0f-someSmallNumber) update rule is equivalent to a force proportional to velocity, as above.
ewjordan wrote:
Friction: force = frictionConstant * normalForce;

Wouldn't this cause objects to slow down, stop, and then back up? If the force is not at all dependent on the distance traveled or speed, this formula still applies when the object is standing still?
Quote:
Wouldn't this cause objects to slow down, stop, and then back up? If the force is not at all dependent on the distance traveled or speed, this formula still applies when the object is standing still?

Ah, sorry - the formula I gave (in each case) only provides the magnitude of the force. The direction is always opposite the direction of motion, except that if the velocity is zero the force is zero as well. I know, that's a bit ad-hoc, but the theoretical explanation of the result is messy, and it's really only an approximation anyways. In particular, for very low and very high velocities the approximation breaks down, and even at normal velocities there are certain materials and situations where the law is not accurate. But it's pretty good in most cases.

Edit: see http://en.wikipedia.org/wiki/Friction if you're interested

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