Guye Incognito
Guye Incognito

Reputation: 2844

Box2D: revolute motor joint not working

No matter what I try I cant get a motor joint to rotate in Box2d (specifically I am using liquidfun which is a superset of Box2d) Can you see any reasons why the code below does not produce..
1.A circular static "pivot"
2.A dynamic "plank" which is being rotated by the revolute joint motor

I get the two bodies but the "plank" does not rotate.

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

Upvotes: 1

Views: 2010

Answers (1)

Guye Incognito
Guye Incognito

Reputation: 2844

Ok, the problem was that I had not defined a density for the bodies. I was just assuming that the density always defaults to 1.0 if you dont enter it yourself. Here is the corrected code.

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

Upvotes: 3

Related Questions