Box2D质量中心

时间:2011-07-05 06:54:44

标签: objective-c ios cocoa-touch box2d game-physics

我正在尝试将质心设置为像对象一样的矛。以下是相关代码:

   // 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>: .

我做错了什么?感谢

1 个答案:

答案 0 :(得分:0)

这是一个棘手的问题...你可以改变海量数据的m_I以伴随CoM位置的变化,但是你可能很难做到正确,你会在幸运之前得到更多的崩溃。你可以在矛的前面添加另一个夹具来衡量它,而不是改变那样的质量,这样就可以更容易调整。如果您不希望它碰到任何东西,第二个夹具可能是一个传感器。