Box2D Forums

It is currently Thu May 23, 2013 9:25 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Fri Feb 03, 2012 4:33 am 
Offline

Joined: Sun Aug 03, 2008 2:36 pm
Posts: 6
I have a Box2d body which I'm attempting to break into multiple pieces using the Box2D Cocos2d framework. 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.

Image

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:

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group