2010-11-29 48 views
1

我是box2d的新手,我嘗試在兩個主體之間創建關節。 我寫聯名像Box2d與cocos2d的聯合

b2RevoluteJointDef jointDef; 

     jointDef.bodyA=worm_head; 
     jointDef.bodyB=worm_body; 

     jointDef.lowerAngle = -0.25f * b2_pi; // -45 degrees 

     jointDef.upperAngle = 0.25f * b2_pi; // 45 degrees 
     jointDef.enableLimit=true; 
     jointDef.maxMotorTorque = 10.0f; 

     jointDef.motorSpeed = 10.0f; 

     jointDef.enableMotor = true; 
     joint=(b2DistanceJoint*)_world->CreateJoint(&jointDef); 

但是當頭部運動物體沒有移動。

我剔方法是

- (void)tick:(ccTime) dt { 

    //we update the position of the b2body based on the sprite position 
    for (b2Body* body = _world->GetBodyList(); body != nil; body = body->GetNext()) 
    { 
     if (body->GetUserData() != NULL) { 
      CCSprite *spritedata = (CCSprite *)body->GetUserData(); 

      if(spritedata.tag==1) 
      { 
       b2Vec2 b2Position = b2Vec2(SCREEN_TO_WORLD(spritedata.position.x), 
            SCREEN_TO_WORLD(spritedata.position.y)); 
       float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(spritedata.rotation); 
       body->SetTransform(b2Position,b2Angle); 
      } 
      else { 
       spritedata.position = ccp(body->GetPosition().x * PTM_RATIO, 
             body->GetPosition().y * PTM_RATIO); 
       spritedata.rotation = -1 * CC_RADIANS_TO_DEGREES(body->GetAngle()); 
      } 

     } 

    } 
} 

爲什麼不動?我應該如何更改我的代碼?

回答

1

在b2RevoluteJointDef中,一個物體是靜態物體,另一個物體是動態物體。我的問題是使用兩個動態問題。現在,它解決了。