开发者

Why doesn't gravity scale work in box2d

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 mon开发者_StackOverflow中文版key1BodyDef;
    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'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!


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

documentation is outdated compared with the code


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜