2012-04-13 4 views
0

Ich habe eine 2D-Physik-Sandbox mit einem Kreis von Kreisen, die sich bei Kontakt ändern (die größere wird größer, die kleinere wird kleiner). Ich kann die Größe des Sprites ändern, und ich verstehe, dass Sie keinen B2Body skalieren können - Sie müssen ihn zerstören und neu erstellen, aber ich kenne Box2D noch nicht genug, um es zu tun.Wie ändere ich die Größe einer b2CircleShape auf cocos2d iPhone mit Box2D

Hier ist, was ich tue den Sprites, um die Größe:

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; 
     } 

    }   
} 

Wie kann ich resize (zerstören/recreate) den Box2D Körper (a b2CircleShape?).

Ich denke, das ist, wie Sie es in C tun - von emanueleferonato.com (Ich bin nicht erleuchtet genug C zu verstehen):

// 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; 

Antwort

1

Hallo zusammen du gut aussehende Teufel.

Hier ist, wie:

//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(); 
Verwandte Themen