2014-02-07 67 views
1

无论我尝试什么,我都无法在Box2d中获得电机接头旋转(具体而言,我使用的是liquidfun,它是Box2d的超集) 您可以看到下面的代码没有任何原因生产..
1.A圆形静态的“支点”
2.A动态的“木板”,这是由旋转关节电机Box2D:旋转电机接头不工作

我得到的两具尸体,但“板”旋转不旋转。

 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

这是范围问题吗?或者是世界上持久的? – mlatu

+1

另外,http://box2d.org/manual.pdf第8.4章说使用类似b2RevoluteJointDef :: Initialize(myBodyA,myBodyB,myBodyA-> GetWorldCenter());试过吗? – mlatu

+0

@mlatu当“木板”停留在原地时,这个联合是坚持不懈的。它不会在重力下落入无限的空间。我尝试使用b2MotorJoint b2MotorJoint * m_joint初始化; \t b2MotorJointDef mjd; \t mjd.Initialize(pivot,plank); \t mjd.maxForce = 10000.0f; \t mjd.maxTorque = 10000.0f; \t m_joint =(b2MotorJoint *)world-> CreateJoint(&mjd); –

回答

2

好的,问题是我没有为身体定义密度。我只是假设密度总是默认为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); 
    }