문제

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.

도움이 되었습니까?

해결책 3

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

다른 팁

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.

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top