本文介绍了如何阻止在box2d中作用在身体上的力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iphone上使用box2d来创建游戏。我有一个身体受重力影响向下移动,不向右或向左。它会被另一个身体撞击,然后会向右或向左移动。然后我有一个复位按钮,将身体移回到其起始点。唯一的问题是它仍然向右或向左移动。我如何抵消球已经移动的力量?

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?

推荐答案

box2d自动清除每个模拟步骤的力量。我认为你只是改变你身体的位置,当复位,但不是它的速度。将此代码添加到您的重置方法中:

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);

这篇关于如何阻止在box2d中作用在身体上的力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 04:59