为什么重力秤在 box2d 中不起作用
我正试图关闭我身体之一上的重力。我使用了 bodyDef.gravityScale = 0.0f 但我没有运气。在这里你可以看看我下面的代码。请帮忙。
b2BodyDef monkey1BodyDef;
monkey1BodyDef.position.Set(0, 200/PTM_RATIO);
monkey1BodyDef.type = b2_dynamicBody;
monkey1BodyDef.userData = monkey1;
monkey1BodyDef.bullet = true;
monkey1BodyDef.gravityScale = 0.0f; //Why doesn't this work I get an error that says no member named 'gravityScale' in 'b2BodyDef'
b2Body *monkey1Body = world->CreateBody(&monkey1BodyDef);
I am trying to turn off gravity on one of my bodies. I have used the bodyDef.gravityScale = 0.0f but I am having no luck. Here u can look at my code below. Please help.
b2BodyDef monkey1BodyDef;
monkey1BodyDef.position.Set(0, 200/PTM_RATIO);
monkey1BodyDef.type = b2_dynamicBody;
monkey1BodyDef.userData = monkey1;
monkey1BodyDef.bullet = true;
monkey1BodyDef.gravityScale = 0.0f; //Why doesn't this work I get an error that says no member named 'gravityScale' in 'b2BodyDef'
b2Body *monkey1Body = world->CreateBody(&monkey1BodyDef);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(4)
我也遇到了这个问题。经过一番挖掘,我发现稳定的 Cocos2D 版本不包含 Box2D 的最新版本,因此 b2BodyDef 中缺少gravityScale。这解释了与 Box2D 文档的差异。
有一些解决方法,但我选择将 Box2D 更新到 2.2.1(目前是最新的)。在此过程中,我遇到了以下问题(及其解决方案):
b2PolygonShape.SetAsEdge 方法不再存在。如果您使用它来定义屏幕边界,则需要使用类似“myPolygonShape.Set(lowerLeftCorner, lowerRightCorner);”的内容对于每个屏幕边缘。 程序员的好东西对此进行了精彩的讨论。
b2DebugDraw 已被 b2Draw 取代。只需用 b2Draw 替换对 b2DebugDraw 的任何调用,您就应该设置好了。例如,如果您像我一样使用 Cocos2D Box2D 模板,则需要替换以下内容:
为:
b2Transform 对于位置和旋转具有不同的属性名称。例如,myTransform.position 现在是 myTransform.p(但仍然是 b2Vec2)。 myTransform.R(定义为 b2Mat22)已替换为 myTransform.q(定义为 b2Rot)。同样,如果您使用 Cocos2D Box2D 模板,请替换 GLES-Render.mm 中的以下内容:
为:...
我希望这会有所帮助!
I've hit this problem too. After a little digging I've found that the stable Cocos2D builds don't include recent versions of Box2D, so gravityScale is missing from b2BodyDef. That explains the discrepancy with the Box2D documentation.
There are workarounds, but I've opted to update my Box2D to 2.2.1 (currently the latest). In doing that I encountered the following issues (with solutions):
The b2PolygonShape.SetAsEdge method no longer exists. If you're using that to define screen boundaries you'll need to use something like "myPolygonShape.Set(lowerLeftCorner, lowerRightCorner);" for each screen edge. There's an excellent discussion of this at Programmers' Goodies.
b2DebugDraw has been superseded by b2Draw. Just replace any calls to b2DebugDraw with b2Draw and you should be set. For example, if, like me, you're using the Cocos2D Box2D template, you'll need to replace this:
with this:
b2Transform has different attribute names for position and rotation. For example, myTransform.position is now myTransform.p (but is still a b2Vec2). myTransform.R, which was defined as b2Mat22, has been replaced with myTransform.q, defined as b2Rot. Again, if you're using the Cocos2D Box2D template, replace the following in GLES-Render.mm:
…with:
I hope this helps!
因为“b2BodyDef”中不存在名为“gravityScale”的成员:(
与代码相比,文档已过时
Because no member named 'gravityScale' exists in 'b2BodyDef' :(
documentation is outdated compared with the code
改变世界的重力定义,因为它是有重力的世界,如:
世界是
b2World
Change gravity definition of world, coz it's world, that have gravity, As:
World is
b2World
如果
body.setGravityScale(0);
不起作用,您可以在第二行将其与body.setAwake(false);
一起使用。If
body.setGravityScale(0);
doesn't work, you can use it withbody.setAwake(false);
at second line.