How to stop the forces acting on a body in box2d

爱⌒轻易说出口 提交于 2019-12-22 03:16:06

问题


I am using box2d on the iphone to create a game. I have a body that is effected by gravity to move down and not right or left. It will get hit by another body and will then be moving right or left. I then have a reset button which moves the body back to its starting point. The only problem is that it is still moving right or left. How can I counteract the forces that a ball is already traveling? How can I get rid of this right and left movement when I reset my game?


回答1:


box2d automatically clears forces each simulation step. I think you are just changing you body's position when reseting, but not it's velocity. Add this code to your reset method:

body->SetLinearVelocity(b2Vec2(0,0));
body->SetAngularVelocity(0);


来源:https://stackoverflow.com/questions/7543764/how-to-stop-the-forces-acting-on-a-body-in-box2d

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