Box2D:旋转电机接头不工作

时间:2014-02-07 13:44:08

标签: c++ box2d

无论我尝试什么,我都无法让一个马达关节在Box2d中旋转(具体来说我使用liquidfun这是Box2d的超集) 你能看出为什么下面的代码不会产生任何原因。 1.圆形静态“枢轴”
2.由旋转关节电机旋转的动态“木板”

我得到两个尸体,但“木板”不会旋转。

        b2BodyDef bd2;
        bd2.type = b2_staticBody;
        bd2.position.Set(0, 0);
        b2Body* pivot = world->CreateBody(&bd2);
        {
            b2CircleShape circleShape;
            circleShape.m_radius = 0.5f;
            b2FixtureDef myFixtureDef;
            myFixtureDef.shape = &circleShape;
            pivot->CreateFixture(&myFixtureDef);
        }

        b2BodyDef bd3;
        bd3.type = b2_dynamicBody;
        bd3.position.Set(0, 0);
        bd3.angle = 1.0f;
        b2Body* plank = world->CreateBody(&bd3);
        {
            b2PolygonShape boxShape;
            boxShape.SetAsBox(2, 0.5f);
            b2FixtureDef myFixtureDef2;
            myFixtureDef2.shape = &boxShape;
            plank->CreateFixture(&myFixtureDef2);
        }

        {
            b2RevoluteJointDef revoluteJointDef;
            revoluteJointDef.bodyA = pivot;
            revoluteJointDef.bodyB = plank;
            revoluteJointDef.collideConnected = false;
            revoluteJointDef.localAnchorA.Set(0, 0);
            revoluteJointDef.localAnchorB.Set(0, 0);
            revoluteJointDef.enableMotor = true;
            revoluteJointDef.maxMotorTorque = 100000.0f;
            revoluteJointDef.motorSpeed = 2.0f;
            b2RevoluteJoint* m_joint = (b2RevoluteJoint*)world->CreateJoint(&revoluteJointDef);
        }

1 个答案:

答案 0 :(得分:3)

好的,问题是我没有为身体定义密度。我只是假设密度总是默认为1.0,如果你自己不输入它。这是更正后的代码。

b2BodyDef bd2;
    bd2.type = b2_staticBody;
    bd2.position.Set(0, 0);
    b2Body* pivot = world->CreateBody(&bd2);
    {
        b2CircleShape circleShape;
        circleShape.m_radius = 0.5f;
        b2FixtureDef myFixtureDef;
        myFixtureDef.density = 1.0f;
        myFixtureDef.shape = &circleShape;
        pivot->CreateFixture(&myFixtureDef);
    }

    b2BodyDef bd3;
    bd3.type = b2_dynamicBody;
    bd3.position.Set(0, 0);
    bd3.angle = 1.0f;
    b2Body* plank = world->CreateBody(&bd3);
    {
        b2PolygonShape boxShape;
        boxShape.SetAsBox(10.0f, 0.5f);
        b2FixtureDef myFixtureDef2;
        myFixtureDef2.shape = &boxShape;
        myFixtureDef2.density = 1.0f;
        plank->CreateFixture(&myFixtureDef2);
    }

    {
        b2RevoluteJointDef revoluteJointDef;
        revoluteJointDef.bodyA = pivot;
        revoluteJointDef.bodyB = plank;
        revoluteJointDef.collideConnected = false;
        revoluteJointDef.localAnchorA.Set(0, 0);
        revoluteJointDef.localAnchorB.Set(0, 0);
        revoluteJointDef.enableMotor = true;
        revoluteJointDef.maxMotorTorque = 100000.0f;
        revoluteJointDef.motorSpeed = 2.0f
        b2RevoluteJoint* m_joint = (b2RevoluteJoint*)world->CreateJoint(&revoluteJointDef);
    }