0

我有一个带有一堆圆圈的 2D 物理沙箱,这些圆圈在接触时会调整大小(越大的越大,越小的越小)。我可以很好地调整精灵的大小,而且我知道您无法缩放 B2Body - 您需要销毁它并重新创建它,但我对 Box2D 还不够熟悉,无法做到这一点。

这是我正在做的调整精灵大小的方法:

std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin(); 
    pos != _contactListener->_contacts.end(); ++pos) {
    MyContact contact = *pos;

    b2Body *bodyA = contact.fixtureA->GetBody();
    b2Body *bodyB = contact.fixtureB->GetBody();
    if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
        PaintBlob *spriteA = (PaintBlob *) bodyA->GetUserData();
        PaintBlob *spriteB = (PaintBlob *) bodyB->GetUserData();

        NSLog(@"spriteA: %@ is touching spriteB: %@", spriteA, spriteB);


        if((spriteA.scale > spriteB.scale) && (spriteB.scale > 0)){
            spriteA.scale = spriteA.scale + kSCALE_INCREMENT;
            spriteB.scale = spriteB.scale - kSCALE_INCREMENT;
        }else if (spriteA.scale >0) {
            spriteB.scale = spriteB.scale + kSCALE_INCREMENT;
            spriteA.scale = spriteA.scale - kSCALE_INCREMENT;
        }

    }        
} 

如何调整(销毁/重新创建)Box2D 主体(b2CircleShape?)的大小。

我认为这就是你在 C 中的做法 - 来自emanueleferonato.com(我不够开明,无法理解 C):

// if I selected a body...
if (body) {
                // I know it's a circle, so I am creating a b2CircleShape variable
                var circle:b2CircleShape=body.GetShapeList() as b2CircleShape;
                // getting the radius..
                var r=circle.GetRadius();
                // removing the circle shape from the body
                body.DestroyShape(circle);
                // creating a new circle shape
                var circleDef:b2CircleDef;
                circleDef = new b2CircleDef();
                // calculating new radius
                circleDef.radius=r*0.9;
                circleDef.density=1.0;
                circleDef.friction=0.5;
                circleDef.restitution=0.2;
                // attach the shape to the body
                body.CreateShape(circleDef);
                // determine new body mass
                body.SetMassFromShapes();
            }
            return body;
4

1 回答 1

1

你好,英俊的恶魔。

就是这样:

    //Radius is worked out by scale * kBLOBDIAMETER /2 
    contact.fixtureA->GetShape()->m_radius = (spriteA.scale * kBLOBLDIAMETER / 2) /PTM_RATIO;
    contact.fixtureB->GetShape()->m_radius = (spriteB.scale * kBLOBLDIAMETER / 2) /PTM_RATIO;
    bodyA->ResetMassData();
    bodyB->ResetMassData();
于 2012-04-13T23:35:53.593 回答