how to use the libgdx contactlistener

15,958

Solution 1

Here's a short example for libgdx. It shows how to create a ContactListener to show which fixtures are involved when contacts are made and broken. It also shows the use of world.getContactList() which will return a list of contacts that still exist after the physics step. This may miss contacts that were made and broken during the course of the physics step. If you are interested in these then you will want to implement a ContactListener, using beginContact() to detect when contacts are made and endContact() to detect when they are broken.

package hacks;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
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.Fixture;
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.World;

public class Box2DDemoMain extends com.badlogic.gdx.Game {

    private static final float SCALING = 0.1f;

    private Box2DDebugRenderer debugRenderer;
    private OrthographicCamera camera;
    private World world;

    @Override
    public void create() {
        debugRenderer = new Box2DDebugRenderer();
        camera = new OrthographicCamera();

        createWorld();
        createCollisionListener();
        createGround();
        createBox();
    }

    private void createWorld() {
        Vector2 gravity = new Vector2(0, -10);
        world = new World(gravity, true);
    }

    private void createCollisionListener() {
        world.setContactListener(new ContactListener() {

            @Override
            public void beginContact(Contact contact) {
                Fixture fixtureA = contact.getFixtureA();
                Fixture fixtureB = contact.getFixtureB();
                Gdx.app.log("beginContact", "between " + fixtureA.toString() + " and " + fixtureB.toString());
            }

            @Override
            public void endContact(Contact contact) {
                Fixture fixtureA = contact.getFixtureA();
                Fixture fixtureB = contact.getFixtureB();
                Gdx.app.log("endContact", "between " + fixtureA.toString() + " and " + fixtureB.toString());
            }

            @Override
            public void preSolve(Contact contact, Manifold oldManifold) {
            }

            @Override
            public void postSolve(Contact contact, ContactImpulse impulse) {
            }

        });
    }

    private void createGround() {
        PolygonShape groundShape = new PolygonShape();
        groundShape.setAsBox(50, 1);
        BodyDef groundBodyDef = new BodyDef();
        groundBodyDef.type = BodyType.StaticBody;
        groundBodyDef.position.set(0, -20);
        Body groundBody = world.createBody(groundBodyDef);
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = groundShape;
        groundBody.createFixture(fixtureDef);
        groundShape.dispose();
    }

    private void createBox() {
        PolygonShape boxShape = new PolygonShape();
        boxShape.setAsBox(1, 1);
        BodyDef boxBodyDef = new BodyDef();
        boxBodyDef.position.set(0, 20);
        boxBodyDef.angle = MathUtils.PI / 32;
        boxBodyDef.type = BodyType.DynamicBody;
        boxBodyDef.fixedRotation = false;
        Body boxBody = world.createBody(boxBodyDef);
        FixtureDef boxFixtureDef = new FixtureDef();
        boxFixtureDef.shape = boxShape;
        boxFixtureDef.restitution = 0.75f;
        boxFixtureDef.density = 2.0f;
        boxBody.createFixture(boxFixtureDef);
        boxShape.dispose();
    }

    @Override
    public void resize(int width, int height) {
        super.resize(width, height);
        float cameraWidth = Gdx.graphics.getWidth() * SCALING;
        float cameraHeight = Gdx.graphics.getHeight() * SCALING;
        camera.setToOrtho(false, cameraWidth, cameraHeight);
        camera.position.set(0, 0, 0);
    }

    @Override
    public void render() {
        super.render();

        world.step(Gdx.graphics.getDeltaTime(), 8, 3);

        int numContacts = world.getContactCount();
        if (numContacts > 0) {
            Gdx.app.log("contact", "start of contact list");
            for (Contact contact : world.getContactList()) {
                Fixture fixtureA = contact.getFixtureA();
                Fixture fixtureB = contact.getFixtureB();
                Gdx.app.log("contact", "between " + fixtureA.toString() + " and " + fixtureB.toString());
            }
            Gdx.app.log("contact", "end of contact list");
        }

        Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        camera.update();
        debugRenderer.render(world, camera.combined);
    }

    public static void main(String[] args) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();

        config.title = Box2DDemoMain.class.getName();
        config.width = 800;
        config.height = 480;
        config.fullscreen = false;
        config.useGL20 = true;
        config.useCPUSynch = true;
        config.forceExit = true;
        config.vSyncEnabled = true;

        new LwjglApplication(new Box2DDemoMain(), config);
    }
}

Solution 2

you dont have to call those methods anywhere. just create a class and implement ContactListener in it. now in your code just use world.setContactListener(ContactListener listener) .

Whenever collision occur in your world, all 4 methods will be called . You will get fixtureA and fixtureB of 2 colliding bodies

Share:
15,958
Xerusial
Author by

Xerusial

Electrical engineer with focus on reprogrammable systems, Linux, Android.

Updated on July 23, 2022

Comments

  • Xerusial
    Xerusial almost 2 years

    I've just began to work with the Libgdx's Box2d Engine but i simply do not understand when the methods of the Contactlistener should be called. There is on the one hand "begin contact" and on the other "end contact". Where should i call them, to get the Number of of a certain fixture touching others? And how do I implement the Contactlistener? A redirec' to a Tutorial would answer my Question. I didn't find anything while searching google. This one helped me a lot but it is written for C++ and does not refer to the implementation into a main-gamecircle. Thx for helping me ;)

  • Xerusial
    Xerusial about 11 years
    what do the 8 and 3 mean in this Line? world.step(Gdx.graphics.getDeltaTime(), 8, 3);
  • Rod Hyde
    Rod Hyde about 11 years
    They are the velocity iterations and position iterations respectively. These values are recommended by the Box2D manual. See section 2.4 - the manual is here: box2d.org/manual.html
  • xetra11
    xetra11 almost 7 years
    isn't there a way to listen to a contact made by a body only? like body.setContactListener(new ContactListenger{...}). That way I only need to check what the other collision object is and am always sure that the first is the body object. While in world.setContactListener I always have to check both fixture bodies first.