使用box2d cocos2d不改变体重

时间:2012-01-27 20:11:05

标签: cocos2d-iphone box2d box2d-iphone

我只是无法弄清楚这种大众的运作方式。 我已经阅读了box2d文档,(其中有许多命令目前不在cocos2d中),我试图理解质量的东西。

我知道,如果我改变身体密度,它就不会改变一件事:

    //add body
b2BodyDef spriteBodyDef;
spriteBodyDef.type =b2_dynamicBody; //b2_staticBody;
spriteBodyDef.position.Set(pilot1.position.x/PTM_RATIO,pilot1.position.y/PTM_RATIO);
spriteBodyDef.userData = pilot1;
pilot1Body = world->CreateBody(&spriteBodyDef);    
b2CircleShape spriteShape; //b2polygon-for box shape
spriteShape.m_radius = 15.0/PTM_RATIO;
b2FixtureDef spriteShapeDef;
spriteShapeDef.restitution = 0.4f;
spriteShapeDef.shape = &spriteShape;
//spriteShapeDef.density=10.0f;

b2Fixture *fix=pilot1Body->GetFixtureList();
fix->SetDensity(1.5f);
pilot1Body->ResetMassData();
NSLog(@"mass2: %f", pilot1Body->GetMass());  
spriteShapeDef.isSensor = false;
pilot1Body->CreateFixture(&spriteShapeDef);
    [self addChild:pilot1];

非常感谢。

1 个答案:

答案 0 :(得分:2)

您需要使用b2Fixture::SetDensity为贴在身体上的部分或全部灯具设置新的密度,然后调用b2Body::ResetMassData,例如:

    NSLog(@"mass1: %f", body->GetMass());
    b2Fixture *fix = body->GetFixtureList();
    while (fix) {
        fix->SetDensity(1.5f);
        fix = fix->GetNext();
    }
    body->ResetMassData();
    NSLog(@"mass2: %f", body->GetMass());