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:
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!
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);
}
}
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!