Friction in Box2d

I use Box2d to play top down. "Earth" is a series of tiles, where each tile is a static body with the shape of a sensor. Can I make friction take effect for this, even if the objects do not really β€œcollide” with the ground?

If Box2d does not allow me to do this, I decided to try to implement my own, discovering what force is currently moving the object and applying the force opposite to it, but I'm not quite sure how to detect this force.

+7
physics box2d
source share
3 answers

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

0
source share

Another way to do this is to install linearDamping on your body. You can set this differently depending on the tile your object is on.

+6
source share

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

If setting linear attenuation is not enough or relying on the b2Body property is not suitable, you can easily calculate nonlinear friction forces and call ApplyLinearImpulse () or ApplyLinearForce () for each frame.

  • Request speed using b2Body.GetLinearVelocity (), scale (non-linearly) the result as desired to obtain strength, 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 computation.

+1
source share

All Articles