8

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.

Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
  • in game physics programming, forces are generally hard to work with, so developers work with velocities/impulses. As for why, that is a longer story, but you can refer to Erin's GDC talks for that – user3471786 Jun 26 '19 at 02:30

3 Answers3

7

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
  • 5,449
  • 2
  • 27
  • 22
1

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.

-1

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

Nick Heiner
  • 119,074
  • 188
  • 476
  • 699