Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrectly removing Box2D fixtures and updating b2Body
    primarykey
    data
    text
    <p>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.</p> <p><img src="https://i.stack.imgur.com/BNhvG.png" alt="enter image description here"></p> <p>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.</p> <p>For reference, here's the code I'm using:</p> <pre><code>for (b2Fixture *f = body-&gt;GetFixtureList(); f; f = f-&gt;GetNext()) { NSString *newSpriteFrameName = (NSString *)f-&gt;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-&gt;CreateBody(&amp;bd); b2FixtureDef fixtureDef; fixtureDef.shape = f-&gt;GetShape(); fixtureDef.density = f-&gt;GetDensity(); fixtureDef.restitution = f-&gt;GetRestitution(); fixtureDef.friction = f-&gt;GetFriction(); fixtureDef.userData = f-&gt;GetUserData(); newBody-&gt;CreateFixture(&amp;fixtureDef); // Try to transfer any angular and linear velocity b2Vec2 center1 = [self worldCenter]; b2Vec2 center2 = newBody-&gt;GetWorldCenter(); CGFloat angularVelocity = parentBody-&gt;GetAngularVelocity(); b2Vec2 velocity1 = [self linearVelocity] + b2Cross(angularVelocity, center1 - center1); b2Vec2 velocity2 = [self linearVelocity] + b2Cross(angularVelocity, center2 - center1); newBody-&gt;SetAngularVelocity(angularVelocity); newBody-&gt;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]]; } </code></pre> <p>Here's how I'm setting my CCSprites location each logic tick:</p> <pre><code>b2Vec2 position = body-&gt;GetPosition(); ccNode.position = CGPointMake(PTM_RATIO*position.x, PTM_RATIO*position.y); ccNode.rotation = -1 * CC_RADIANS_TO_DEGREES(body-&gt;GetAngle()); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload