Hi!
I'm using Box2d to create a 2D-physics game.
The pieces in the game consists of a couple of fixtures attached to a body and sometimes a few fixtures should be removed and the piece should break.
For example lets say I have a piece that looks like this: OOOO and this fixture should disappear: OXOO two new bodies will be created, one looking like O and one looking like OO.
Well that part works (splitting the bodies) except for a minor problem. Sometimes a piece will move and get stuck in another piece and the game will break.
I do not know if this is because the piece actually moves or if the engine just doesn't like me placing bodies so close.
Anyway, here's the code for creating new bodies:
Is there a better way of doing this or have I simply overlooked something?
Best Regards
Nicklas
I'm using Box2d to create a 2D-physics game.
The pieces in the game consists of a couple of fixtures attached to a body and sometimes a few fixtures should be removed and the piece should break.
For example lets say I have a piece that looks like this: OOOO and this fixture should disappear: OXOO two new bodies will be created, one looking like O and one looking like OO.
Well that part works (splitting the bodies) except for a minor problem. Sometimes a piece will move and get stuck in another piece and the game will break.
I do not know if this is because the piece actually moves or if the engine just doesn't like me placing bodies so close.
Anyway, here's the code for creating new bodies:
Code:
// body - The old body which will be splitted
// fixtures - contains all the fixtures that should belong to the same body
b2BodyDef newBodyDef;
newBodyDef.userData = FALLING;
newBodyDef.type = b2_dynamicBody;
newBodyDef.position = body->GetPosition();
b2Body *newBody = world -> CreateBody(&newBodyDef);
// Attach the fixtures to the new body
foreach(fix, fixtures) {
b2PolygonShape s;
b2Vec2 tmp = fGetCenter(*fix);
tmp.x -= 0.5f;
tmp.y -= 0.5f;
s.SetAsBox(0.5f, 0.5f, tmp, 0.0f);
b2Fixture *newFix = newBody -> CreateFixture(&s, 1.0f);
newFix -> SetRestitution(0.2f);
newFix -> SetUserData((*fix)->GetUserData());
body->DestroyFixture(*fix);
}
// fixtures - contains all the fixtures that should belong to the same body
b2BodyDef newBodyDef;
newBodyDef.userData = FALLING;
newBodyDef.type = b2_dynamicBody;
newBodyDef.position = body->GetPosition();
b2Body *newBody = world -> CreateBody(&newBodyDef);
// Attach the fixtures to the new body
foreach(fix, fixtures) {
b2PolygonShape s;
b2Vec2 tmp = fGetCenter(*fix);
tmp.x -= 0.5f;
tmp.y -= 0.5f;
s.SetAsBox(0.5f, 0.5f, tmp, 0.0f);
b2Fixture *newFix = newBody -> CreateFixture(&s, 1.0f);
newFix -> SetRestitution(0.2f);
newFix -> SetUserData((*fix)->GetUserData());
body->DestroyFixture(*fix);
}
Is there a better way of doing this or have I simply overlooked something?
Best Regards
Nicklas