How to stop the forces acting on a body in box2d(如何在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 the forces each simulation step. I think you are just changing your body's position when resetting, but not its velocity. Add this code to your reset method:
body->SetLinearVelocity(b2Vec2(0,0));
body->SetAngularVelocity(0);
这篇关于如何在box2d中停止作用在身体上的力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在box2d中停止作用在身体上的力


基础教程推荐
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01