2012-02-03 14 views
2

私は複数の部分に分割しようとしているBox2d本体を持っています。これを行うために、私はそのフィクスチャを繰り返し実行し、それぞれに対して新しいボディを生成します。デバッグドローを使用すると、これが動作しているように見えます。Box2Dフィクスチャを正しく削除してb2Bodyを更新する

enter image description here

上記の画像で見ることができるように、主要本体が破壊されており、二次体(標識:2)が生成されます。デバッグレイヤーからのシェイプレンダリングに基づいて、それらは正しく表現されています。私が抱えている問題は、私のプライマリb2bodyと関連しているCCSpriteが、新しいボディを参照して正しく配置されていないということです。あたかもそれがより大きな形状の一部であるかのように、関連付けられたCCSpriteが配置されているかのように見えます(0、0のアンカーポイントが与えられています)。参考のため

は、ここで私が使用しているコードです:

for (b2Fixture *f = body->GetFixtureList(); f; f = f->GetNext()) 
{ 
    NSString *newSpriteFrameName = (NSString *)f->GetUserData(); 

    // Steal some of our parent bodies properties 
    b2BodyDef bd; 
    bd.type = b2_dynamicBody; 
    bd.position = [self physicsPosition]; 
    bd.angle = [self angle]; 

    b2Body *newBody = _world->CreateBody(&bd); 

    b2FixtureDef fixtureDef; 
    fixtureDef.shape = f->GetShape(); 
    fixtureDef.density = f->GetDensity(); 
    fixtureDef.restitution = f->GetRestitution(); 
    fixtureDef.friction = f->GetFriction(); 
    fixtureDef.userData = f->GetUserData(); 
    newBody->CreateFixture(&fixtureDef); 

    // Try to transfer any angular and linear velocity 
    b2Vec2 center1 = [self worldCenter]; 
    b2Vec2 center2 = newBody->GetWorldCenter(); 

    CGFloat angularVelocity = parentBody->GetAngularVelocity(); 
    b2Vec2 velocity1 = [self linearVelocity] + b2Cross(angularVelocity, center1 - center1); 
    b2Vec2 velocity2 = [self linearVelocity] + b2Cross(angularVelocity, center2 - center1); 

    newBody->SetAngularVelocity(angularVelocity); 
    newBody->SetLinearVelocity(velocity2); 

    // Create a new destructable entity 
    CCSprite *newSprite = [CCSprite spriteWithSpriteFrameName:newSpriteFrameName]; 
    SIDestructableEntity *newEntity = [[SIDestructableEntity alloc] initWithBody:newBody node:newSprite]; 
    [[newEntity ccNode] setAnchorPoint:CGPointMake(0, 0)]; 
    [game.entities addObject:newEntity]; 
    [game.entityLayer addChild:[newEntity ccNode]]; 
} 

は、ここで私は各論理ティック私のCCSpritesの場所を設定しています方法は次のとおりです。

b2Vec2 position = body->GetPosition(); 
ccNode.position = CGPointMake(PTM_RATIO*position.x, PTM_RATIO*position.y); 
ccNode.rotation = -1 * CC_RADIANS_TO_DEGREES(body->GetAngle()); 

答えて

0

この行は疑わしいです。

[[newEntity ccNode] setAnchorPoint:CGPointMake(0, 0)]; 

通常、スプライトのアンカーポイントは(0.5,0.5)です。あなたの体のアンカーポイントが真ん中にある場合(上記のコードからは分かりません)、スプライトの(0.5,0.5)のアンカーが中央に配置されます。それを(0,0)に置くと、スプライトの左上隅がスプライトの位置に置かれます。

私の推測では、あなたのボディアンカーはボディの左下にあり、スプライトアンカーは右上にあり、あなたが見ている効果を与えていると思います。

関連する問題