views:

330

answers:

3
+4  Q: 

Friction in Box2d

I am using Box2d for a topdown game. The "ground" is a series of tiles, where each tile is a static body with a sensor shape. Can I make friction take effect for this, even though the objects aren't really "colliding" with the ground?

If Box2d won't let me do this, I considered trying to implement my own by detecting what force is currently moving the object, and applying a force opposite to it, but I'm not quite sure how to detect that force.

A: 

ApplyImpulse() instead of ApplyForce() works much better.

Rosarch
+1  A: 

Another way of doing this is to set linearDamping on your body. You could set this differently depending on the tile your object is on.

Colin Gislason
A: 

Friction is directed against the velocity of the body, regardless of other forces.

If setting linear damping isn't enough or relying on a property of the b2Body is inappropriate, you can easily compute nonlinear friction forces and call ApplyLinearImpulse() or ApplyLinearForce() every frame.

  • Query the velocity with b2Body.GetLinearVelocity(), scale (nonlinearly) the result as desired to get the force, and invert the sign of both components.

  • If you decide to stop the body (when it is slow enough to stick), SetLinearVelocity() does the trick without computations.

Lorenzo Gatti