Box2D 对特定物体的重力?
我看到每个人都说在 Box2D 世界中像这样添加重力:
b2Vec2 gravity = b2Vec2(0.0f, -10.0f);
bool doSleep = false;
world = new b2World(gravity, doSleep);
问题是,如果我只想在包含来自 CCSprite 的 userData 的特定 b2Body 上施加重力怎么办? AFAIK 这会将重力应用于世界上我不想要的所有东西,所以有人可以向我解释一下如何将这种重力仅应用于特定的 b2Body 吗?
谢谢!
编辑1: 我可以只做这一行,
_bottomBody->ApplyForce(gravity, _bottomBody->GetPosition());
而不是世界=新的b2World...等等...难道这不是只在b2Body上使用重力吗?
I see everyone saying that you add gravity like so in a Box2D world:
b2Vec2 gravity = b2Vec2(0.0f, -10.0f);
bool doSleep = false;
world = new b2World(gravity, doSleep);
The thing is though, what if I want gravity only on a specific b2Body which contains userData from a CCSprite? AFAIK this will apply gravity to everything in the world which I do not want, so can someone explain to me how I can apply this gravity only to a specific b2Body?
Thanks!
Edit1:
Can I just do this line,
_bottomBody->ApplyForce(gravity, _bottomBody->GetPosition());
Instead of the world = new b2World... etc... Wouldn't that work with gravity only on that b2Body?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需每帧对特定的 b2Body 施加力/脉冲即可。它将模拟重力。
一个主题与您密切相关的问题:
如何在 Box2D 主体上施加恒定力?
Just apply a force/impulse to the specific b2Body every frame. It will emulate gravity.
A thread with a question closely related to your:
How to apply constant force on a Box2D body?