"simulating" friction

I'm currently using two raycasts to simulate legs, much like @irresistible force did (check this thread).
So I have the legs and my player is walking, but now I have to apply friction or else my player will keep moving forever. Note however that this is not air friction, just the same friction that would be applied if the shape was in the ground (the shape is just a box).

This is currently what I'm doing:

Code:
   b2Vec2 gravity = hero.world->GetGravity();
   float xvel = hero.body->m_linearVelocity.x;
   if (xvel != 0.0f) {
      b2Vec2 frictionForce = b2Vec2(-gravity.y, -gravity.x);
      b2Vec2 frictionPoint = hero.body->GetWorldCenter();
      frictionPoint.x = point.x;
      frictionForce *= b2MixFriction(fixture->GetFriction(), hero.body->m_fixtureList[0].GetFriction()) * hero.body->m_mass;
      if (xvel > 0.0f)
         frictionForce *= -1.0f;
      // frictionForce *= (xvel > 0.0f) ? -1.0f : 1.0f;
      b2Vec2 current_force = hero.body->m_force;
      if ((current_force.x >= 0.0f && current_force.x + frictionForce.x <= 0.0f) ||
         (current_force.x <= 0.0f && current_force.x + frictionForce.x >= 0.0f)) {
         hero.body->m_force.SetZero();
         hero.body->m_torque = 0.0f;
      }
      else {
         hero.body->ApplyForce(frictionForce, frictionPoint);
      }
   }


My physics are really, really rusty 🙂... I'm doing this: frictionForce = <u> x <N> where <u> is the friction coefficient and <N> is the normal (in this case, it would be the mass of my body, since the body never rotates).
This sort of works, I mean, it slows down the body, but the body never really stops... I'm totally sure I'm not calculating (or applying for that matter) the friction force correctly.
Any pointers on this?

thanks!
I think something floating would experience more of a drag, rather than what you're describing.

What you're describing is a dry friction(stuff flying experiences a friction force relative to their velocity squared, whereas something on the ground experiences a constant friction force (dry friction)). I think at least.

Anyways, dry friction is hard to do in box2d. I just spent a couple days trying to integrate it, and it took a while to do it properly. You can see the discussion here viewtopic.php?f=3&t=3902

You're algorithm (when the math is fixed) will work for the most part for the body, unless it's being acted on by joints or you want it not to move at all when force is applied to it. Here's why:

You have two bodies, A and B.
A and B are attached by a friction joint
B has friction.
A is pushing B with less force than the friction force is (which means B shouldn't move).

In your algorithm, what will happen is you'll only apply enough counterforce on the body to only counteract A's momentum, not B's force/momentum combined, even though there's enough friction force to stop it.


I think you might have some conversion problems in your math. (I could be wrong, I'm really rusty at physics too). What you're calling frictionForce is actually momentum. You need to divide that by the time step to get the force:

momentum = mass * velocity
force = momentum / delta_time
Thanks Mike!

I run through your code (and thread) and now I have something working just like I wanted! 😛
thanks a lot!
Glad I could help. I cleaned up the code a lot and put a clean patch in the other thread, and I don't recommend using what I had before.

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