为什么重力秤在 box2d 中不起作用

发布于 2024-12-06 06:25:00 字数 508 浏览 7 评论 0原文

我正试图关闭我身体之一上的重力。我使用了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不气馁 2024-12-13 06:25:00

我也遇到了这个问题。经过一番挖掘,我发现稳定的 Cocos2D 版本不包含 Box2D 的最新版本,因此 b2BodyDef 中缺少gravityScale。这解释了与 Box2D 文档的差异。

有一些解决方法,但我选择将 Box2D 更新到 2.2.1(目前是最新的)。在此过程中,我遇到了以下问题(及其解决方案):

  1. b2PolygonShape.SetAsEdge 方法不再存在。如果您使用它来定义屏幕边界,则需要使用类似“myPolygonShape.Set(lowerLeftCorner, lowerRightCorner);”的内容对于每个屏幕边缘。 程序员的好东西对此进行了精彩的讨论。

  2. b2DebugDraw 已被 b2Draw 取代。只需用 b2Draw 替换对 b2DebugDraw 的任何调用,您就应该设置好了。例如,如果您像我一样使用 Cocos2D Box2D 模板,则需要替换以下内容:

    // 调试绘制函数
    m_debugDraw = new GLESDebugDraw( PTM_RATIO );
    _world->SetDebugDraw(m_debugDraw);
    
    uint32 标志 = 0;
    标志+= b2DebugDraw::e_shapeBit;
    标志+= b2DebugDraw::e_centerOfMassBit;
    m_debugDraw->SetFlags(flags);
    

为:

    // Debug Draw functions
    m_debugDraw = new GLESDebugDraw( PTM_RATIO );
    _world->SetDebugDraw(m_debugDraw);

    uint32 flags = 0;
    flags += b2Draw::e_shapeBit;
    flags += b2Draw::e_centerOfMassBit;
    m_debugDraw->SetFlags(flags);
  1. b2Transform 对于位置和旋转具有不同的属性名称。例如,myTransform.position 现在是 myTransform.p(但仍然是 b2Vec2)。 myTransform.R(定义为 b2Mat22)已替换为 myTransform.q(定义为 b2Rot)。同样,如果您使用 Cocos2D Box2D 模板,请替换 GLES-Render.mm 中的以下内容:

    void GLESDebugDraw::DrawTransform(const b2Transform& xf)
    {
    b2Vec2 p1 = xf.position, p2;
    const float32 k_axisScale = 0.4f;
    
    p2 = p1 + k_axisScale * xf.R.col1;
    DrawSegment(p1,p2,b2Color(1,0,0));
    
    p2 = p1 + k_axisScale * xf.R.col2;
    DrawSegment(p1,p2,b2Color(0,1,0));
    }
    

为:...

    void GLESDebugDraw::DrawTransform(const b2Transform& xf)
    {
    b2Vec2 p1 = xf.p, p2;
    const float32 k_axisScale = 0.4f;

    p2 = p1 + k_axisScale * xf.q.GetXAxis();
    DrawSegment(p1,p2,b2Color(1,0,0));

    p2 = p1 + k_axisScale * xf.q.GetXAxis();
    DrawSegment(p1,p2,b2Color(0,1,0));
    }

我希望这会有所帮助!

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

  1. 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.

  2. 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:

    // Debug Draw functions
    m_debugDraw = new GLESDebugDraw( PTM_RATIO );
    _world->SetDebugDraw(m_debugDraw);
    
    uint32 flags = 0;
    flags += b2DebugDraw::e_shapeBit;
    flags += b2DebugDraw::e_centerOfMassBit;
    m_debugDraw->SetFlags(flags);
    

with this:

    // Debug Draw functions
    m_debugDraw = new GLESDebugDraw( PTM_RATIO );
    _world->SetDebugDraw(m_debugDraw);

    uint32 flags = 0;
    flags += b2Draw::e_shapeBit;
    flags += b2Draw::e_centerOfMassBit;
    m_debugDraw->SetFlags(flags);
  1. 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:

    void GLESDebugDraw::DrawTransform(const b2Transform& xf)
    {
    b2Vec2 p1 = xf.position, p2;
    const float32 k_axisScale = 0.4f;
    
    p2 = p1 + k_axisScale * xf.R.col1;
    DrawSegment(p1,p2,b2Color(1,0,0));
    
    p2 = p1 + k_axisScale * xf.R.col2;
    DrawSegment(p1,p2,b2Color(0,1,0));
    }
    

…with:

    void GLESDebugDraw::DrawTransform(const b2Transform& xf)
    {
    b2Vec2 p1 = xf.p, p2;
    const float32 k_axisScale = 0.4f;

    p2 = p1 + k_axisScale * xf.q.GetXAxis();
    DrawSegment(p1,p2,b2Color(1,0,0));

    p2 = p1 + k_axisScale * xf.q.GetXAxis();
    DrawSegment(p1,p2,b2Color(0,1,0));
    }

I hope this helps!

蓝海似她心 2024-12-13 06:25:00

因为“b2BodyDef”中不存在名为“gravityScale”的成员:(

与代码相比,文档已过时

Because no member named 'gravityScale' exists in 'b2BodyDef' :(

documentation is outdated compared with the code

初懵 2024-12-13 06:25:00

改变世界的重力定义,因为它是有重力的世界,如:

b2Vec2 gravity = b2Vec2(0.0f, -10.0f);
bool doSleep = false;
world = new b2World(gravity, doSleep);

世界是b2World

Change gravity definition of world, coz it's world, that have gravity, As:

b2Vec2 gravity = b2Vec2(0.0f, -10.0f);
bool doSleep = false;
world = new b2World(gravity, doSleep);

World is b2World

凯凯我们等你回来 2024-12-13 06:25:00

如果 body.setGravityScale(0); 不起作用,您可以在第二行将其与 body.setAwake(false); 一起使用。

If body.setGravityScale(0); doesn't work, you can use it with body.setAwake(false); at second line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文