1
votes

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.

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?

If there is a better way of doing any of the below, any advice would be appreciated :)

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<FixtureDef> fixtureArray = new ArrayList<FixtureDef>();

    public GameObject(){}

    public Body addToWorld(World world){
        if(bodyDef == null)
            return null;

        body = world.createBody(bodyDef);

        while(fixtureArray.size() > 0){
            body.createFixture(fixtureArray.remove(0));
        }

        body.setMassData(massData);

        return body;
    }

}
3

3 Answers

0
votes

Try add this line in your code (at coin constructor):

bodyDef.userData = this;

And your contact listener, get userdata and casting into GameObject, then, for example, call event collision.

Greetings

0
votes

You have to check collision of Sensor body in the Being contact or End Contact of box2d collisionlisiter method

0
votes

Two things that can be wrong:
1) OP did not correctly set the ContactListener after instantiating the box2d.World
2) the category and mask bits are not set correctly so it will not trigger the ContactListener

Solution:
1) Put this right after instantiating the physics world which is missing from the example. This will set the ContactListener for your box2d.World so it will have something to trigger.

physicsWorld = new World(new Vector2(0,-9),true);
physicsWorld.setContactListener(
    new ContactListener() {
        @Override
        public void beginContact(Contact contact) {
            Fixture fA = contact.getFixtureA();
            Fixture fB = contact.getFixtureB();
            Gdx.app.log("beginContact", "between " + fA.toString() + " and " + fB.toString());
        }

        @Override
        public void endContact(Contact contact) {
            Gdx.app.log("endContact", "between " );
        }

        @Override
        public void preSolve(Contact contact, Manifold oldManifold) {
            //you can leave this empty
        }

        @Override
        public void postSolve(Contact contact, ContactImpulse impulse) {
            //you can leave this empty
        }
    });

2) set the category and mask bits this way so you can test it if works (OP did not show the specific values used in his PhysicsLayers )

public CoinCollectible(Vector2 position, float angle){
    // ...
    itemFixture.filter.categoryBits = 1;
    itemFixture.filter.maskBits =  2;
    sensorFixture.filter.categoryBits = 2; 
    sensorFixture.filter.maskBits = 1;
}