Destroy object after collision

Hi

I am making a pinball game using box2d for my course project. I want to destroy the ball when it collides with a particular object. I cannot write this code to destroy the ball inside the b2ContactListener class, but i make a boolean true inside this class, so that i can destroy the ball after i come out of ContactListener class. Viewing some previous answers made me learn that i have to write the DestroyBody() function inside the Step funciton.

Can someone tell me how exactly to overwrite Step function. It would be great if you could give me the code to do this.
You would put the DestroyBody after the Step, not inside it. For example:
Code:
world->Step( ... );
if ( needToDestroySomething )
    world->DestroyBody( whatever );
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
           }
       }
}

Original thread: https://box2d.org/forum/viewtopic.php?t=9724