8

Libgdx Box2D - Collision detection

 3 years ago
source link: https://www.codesd.com/item/libgdx-box2d-collision-detection.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Libgdx Box2D - Collision detection

advertisements

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;
    }

}


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


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK