简体   繁体   中英

Correctly removing Box2D fixtures and updating b2Body

I have a Box2d body which I'm attempting to break into multiple pieces. To do this, I iterate over its fixtures and generate new bodies for each. Using debug draw, I can see that this seems to be working.

在此处输入图片说明

As you can see in the above image, the primary body is being broken and a secondary body (labelled: 2) is being generated. Based on the shape rendering from the debug layer, they're being represented correctly. The issue I'm having is that the CCSprite I'm associating with my primary b2body isn't being correctly positioned in reference to the new body. It seems as though the associated CCSprite is being positioned (given an anchor point of 0, 0) as if it were still part of a larger shape.

For reference, here's the code I'm using:

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

Here's how I'm setting my CCSprites location each logic tick:

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

This line looks suspicious.

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

The sprite usually has an anchor point of (0.5,0.5). If your body's anchor point is in the middle (can't tell from code above), then an anchor of (0.5,0.5) for the sprite would put it in the middle as well. Putting it at (0,0) puts the sprite's top left corner at the position of the sprite.

My guess is that your body anchor is at the bottom left of body and the sprite anchor is at the top right, giving the effect you are seeing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM