In box2d it is very important that you safely destroy objects. Objects must be destroyed outside of the world's step method. Otherwise if you have a fixture that collides with two others at the same time, and you destroy the body right when it collides it wont be there to calculate the second fixture collision.
So what you need to do is have a list of bodies that need to be destroyed. When two fixtures collide, and you have those references in your contact listener class, you add the body to that destroy list. Then after the world step method you destroy each body in that list.
Heres how I do it. To easily have a reference to the body in my contact listener that corresponds to an entity in my game world, I set each bodies user data variable to a reference to the entity that has that body. Like so (Java w/Libgdx psuedocode):
Code:
public class Entity {
public Body body;
public Entity(World world) {
BodyDef bodyDef... // set up body def
FixtureDef fixtureDef... // set up fixturedef
body = world.createBody(bodyDef);
body.createFixture(fixtureDef);
body.setUserData(this); // right here we setup our reference for the contact listener
}
public class MyContactListener implements ContactListener {
private Game game; // so we can call game's destroy method
public MyContactListener(Game game) {
this.game = game;
}
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
// if two entities collide, destroy body A
if (contact.getFixtureA().getBody().getUserData() instanceof Entity
&& contact.getFixtureB().getBody().getUserData() instanceof Entity) {
game.destroy(contact.getFixtureA().getBody().getUserData());
}
}
public class Game implements Screen {
private World world;
Array<Entity> activeEntities;
Array<Entity> destroyEntities;
MyContactListener myContactListener = new MyContactListener();
// initialize, called at start of application
@Override
public void show() {
world.setContactListener(new MyContactListener(this));
activeEntities = new Array<Entity>();
destroyEntities = new Array<Entity>();
... // make some active entities
}
// update, called every frame
@Overrride
public void render() {
world.step(...);
// for each entity in the destroy list, safely destroy it outside of world.step
for (Entity entity : destroyEntities) {
world.destroyBody(entity.body);
}
destroyEntities.clear();
}
public void destroy(Entity entity) {
if (!destroyEntities.contains(entity) {
destroyEntities.add(entity.body);
activeBodies.remove(entity, true); // true for == comparison
}
}
}