c++ - Box2D:旋转电机关节不工作

标签 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。这是更正后的代码。

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);
    }

关于c++ - Box2D:旋转电机关节不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21629346/

相关文章:

java - 在 LibGDx 中添加 rayhandler 会导致 InputListener 出现问题

c++ - 莱克斯/柔性 :Regular expression for string literals in C/C++?

architecture - Box2D 碰撞回调

android - setScaleCenter() 在缩小 Sprite 时导致问题 - AndEngine

c++ - 重新使用文件指针会导致内存泄漏吗?

ios - 像小翅膀一样的硬币收藏品

c++ - 在 Ubuntu 12.04 上编译 Box2D HelloWorld

c++ - 无法从文本文件中读取

c++ - 为什么这个 'for' 循环无效?

c++ - 从 Doxygen 隐藏模板实现细节