How to apply constant force on a Box2D body?

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!