How to apply constant force on a Box2D body?

2019-05-05 23:12发布

问题:

I am making a Box2d game for the iPhone. I need to apply a force on a body which represents my main character. The body is actually a rectangle on top of a circle connected using a revolute joint. I am using this as the skeleton for my character who is supposed to be running through the game(any suggestions or feedback on this too would be appreciated).

I need a force to be applied continuously so that it keeps him moving. What would be the best way to do this?

I am currently applying linear velocity inside the tick method.

_world->Step(dt, 10, 10);

if(gamestarted)
{
    b2Vec2 force = b2Vec2(6, -3);
    _bottomBody->SetLinearVelocity(force);
}

But will this force keep adding up and accelerate the body?

回答1:

Seems simple:

_bottomBody->ApplyForce(force, _bottomBody->GetPosition());

If you apply that force in every step the body will accelerate in the direction of the force vector.



标签: iphone box2d