title: Libdgx Box2D - Detecting collisions
body: <p>I am trying detect player collisions with a sensor body. Imagine a collectible in a platformer... I want to run an event when the player collides with the collectible.</p>
  
  <p>As you will see from my code, I am attaching a dynamic body to the collectible object as well as a sensor body which I am hoping to attach a player collision 'event' to when the player collides with the sensor. It seems there is a "ContactListener" interface, but implementing the methods don't seem to do anything. How would I go about doing this?</p>
  
  <p>If there is a better way of doing any of the below, any advice would be appreciated :)</p>
  
  <pre><code>import com.badlogic.gdx.math.Vector2;
  import com.badlogic.gdx.physics.box2d.Contact;
  import com.badlogic.gdx.physics.box2d.ContactImpulse;
  import com.badlogic.gdx.physics.box2d.ContactListener;
  import com.badlogic.gdx.physics.box2d.FixtureDef;
  import com.badlogic.gdx.physics.box2d.Manifold;
  import com.badlogic.gdx.physics.box2d.PolygonShape;
  import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
  
  public class CoinCollectible extends GameObject{
  
      public CoinCollectible(Vector2 position, float angle){
          bodyDef.type = BodyType.DynamicBody;
          bodyDef.position.x = position.x;
          bodyDef.position.y = position.y;
          bodyDef.angle = angle;
  
          PolygonShape poly = new PolygonShape();
          poly.setAsBox(1, 1);
          FixtureDef itemFixture = new FixtureDef();
          itemFixture.shape = poly;
          itemFixture.density = 1;
          itemFixture.filter.categoryBits = PhysicsLayers.LAYER_ITEM;
          itemFixture.filter.maskBits = PhysicsLayers.MASK_PLAYER;
          fixtureArray.add(itemFixture);
  
          massData.mass = 1f;
  
          // Attach a collision sensor
          FixtureDef sensorFixture = new FixtureDef();
          sensorFixture.shape = poly;
          sensorFixture.isSensor = true;
          sensorFixture.filter.maskBits = PhysicsLayers.LAYER_PLAYER;
  
          fixtureArray.add(sensorFixture);
  
          // Clean up
          poly.dispose();
  
      }
  }
  
  import java.util.ArrayList;
  import java.util.List;
  
  import com.badlogic.gdx.physics.box2d.Body;
  import com.badlogic.gdx.physics.box2d.BodyDef;
  import com.badlogic.gdx.physics.box2d.FixtureDef;
  import com.badlogic.gdx.physics.box2d.MassData;
  import com.badlogic.gdx.physics.box2d.World;
  
  public class GameObject{
  
      public Body body = null;
      public BodyDef bodyDef = new BodyDef();
      public MassData massData = new MassData();
  
      protected List&lt;FixtureDef&gt; fixtureArray = new ArrayList&lt;FixtureDef&gt;();
  
      public GameObject(){}
  
      public Body addToWorld(World world){
          if(bodyDef == null)
              return null;
  
          body = world.createBody(bodyDef);
  
          while(fixtureArray.size() &gt; 0){
              body.createFixture(fixtureArray.remove(0));
          }
  
          body.setMassData(massData);
  
          return body;
      }
  
  }
  </code></pre>