2011-07-05 76 views
0

我想將質量中心設置爲類似物體的矛。下面是相關代碼:Box2D質量中心

// Create spear body and shape 
    b2BodyDef spearBodyDef; 
    spearBodyDef.type = b2_dynamicBody; 
    spearBodyDef.position.Set(100.0/PTM_RATIO, 100.0/PTM_RATIO); 
    //spearBodyDef.angle = 45.0 * (180.0f/b2_pi); 
    spearBodyDef.userData = _spear; 
    _spearBody = _world->CreateBody(&spearBodyDef); 


    b2PolygonShape spearShape; 
    spearShape.Set(verts, num); 

    b2FixtureDef spearShapeDef; 
    spearShapeDef.shape = &spearShape; 
    spearShapeDef.density = 100.0f; 
    spearShapeDef.friction = 0.9f; 
    spearShapeDef.restitution = 0.1f; 
    _spearBody->CreateFixture(&spearShapeDef); 

    b2MassData massD; 
    _spearBody->GetMassData(&massD); 
    b2Vec2 centerV; 
    centerV.x = 25.0; 
    centerV.y = -1.0f; 
    massD.center = centerV; 
    _spearBody->SetMassData(&massD); 

這崩潰,因爲斷言失敗的應用程序:

Jul 5 02:52:23 unknown UIKitApplication:com.youcompany.box[0xb15c][10311] <Notice>: Assertion failed: (m_I > 0.0f), 
Jul 5 02:52:23 unknown UIKitApplication:com.youcompany.box[0xb15c][10311] <Notice>: function SetMassData, file /User 
Jul 5 02:52:23 unknown UIKitApplication:com.youcompany.box[0xb15c][10311] <Notice>: s/~/Desktop/box/box/libs/Box 
Jul 5 02:52:23 unknown UIKitApplication:com.youcompany.box[0xb15c][10311] <Notice>: 2D/Dynamics/b2Body.cpp, line 357 
Jul 5 02:52:23 unknown UIKitApplication:com.youcompany.box[0xb15c][10311] <Notice>: . 

我在做什麼錯?謝謝

+0

由於某些原因,慣性矩小於零 – Andrew

回答

0

這是一個棘手的問題......你可以改變海量數據的m_I以伴隨CoM位置的變化,但它很可能很難得到正確的結果,並且在得到更多的崩潰之前幸運。不要像這樣改變質量,你可以在矛的前部增加另一個夾具來減輕重量 - 這將更容易調整。如果你不想讓它與任何東西碰撞,這第二個夾具可能是一個傳感器。