The LibGdx ContactListener Interface is used to detect the contact between two game objects, once the contact has been established, the information about these two game objects will be kept inside a contact object which can later be used to retrieve the data about those two game objects.

Below is an example of the Java class which implements the ContactListener Interface.

package com.gamingdirectional.lonemoon;

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.Manifold;

public class ContactDetectionManager implements ContactListener {

    @Override
    public void beginContact(Contact contact) {

    }

    @Override
    public void endContact(Contact contact) {

        boolean enemy_hit_a = contact.getFixtureA().getBody().getUserData().getClass().equals(Enemy.class);
        boolean enemy_hit_b = contact.getFixtureB().getBody().getUserData().getClass().equals(Enemy.class);
        boolean player_hit_a = contact.getFixtureA().getBody().getUserData().getClass().equals(Player.class);
        boolean player_hit_b = contact.getFixtureB().getBody().getUserData().getClass().equals(Player.class);
        boolean player_missile_hit_a = contact.getFixtureA().getBody().getUserData().getClass().equals(PlaneMissile.class);
        boolean player_missile_hit_b = contact.getFixtureB().getBody().getUserData().getClass().equals(PlaneMissile.class);
        boolean enemy_missile_hit_a = contact.getFixtureA().getBody().getUserData().getClass().equals(EnemyMissile.class);
        boolean enemy_missile_hit_b = contact.getFixtureB().getBody().getUserData().getClass().equals(EnemyMissile.class);

        if(enemy_hit_a && (player_hit_b))
            ((Enemy) contact.getFixtureA().getBody().getUserData()).destroy();
        else if(enemy_hit_b && (player_hit_a))
            ((Enemy) contact.getFixtureB().getBody().getUserData()).destroy();
        else if(enemy_hit_a && (player_missile_hit_b)) {
            ((Enemy) contact.getFixtureA().getBody().getUserData()).destroy();
            ((PlaneMissile) contact.getFixtureB().getBody().getUserData()).destroy();
        }
        else if(enemy_hit_b && (player_missile_hit_a)) {
            ((Enemy) contact.getFixtureB().getBody().getUserData()).destroy();
           ((PlaneMissile) contact.getFixtureA().getBody().getUserData()).destroy();
        }
        else if(enemy_missile_hit_b && (player_missile_hit_a)) {
            ((EnemyMissile) contact.getFixtureB().getBody().getUserData()).destroy();
            ((PlaneMissile) contact.getFixtureA().getBody().getUserData()).destroy();
        }
        else if(enemy_missile_hit_a && (player_missile_hit_b)) {
            ((EnemyMissile) contact.getFixtureA().getBody().getUserData()).destroy();
            ((PlaneMissile) contact.getFixtureB().getBody().getUserData()).destroy();
        }
    }

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

    }

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

    }
}

The above class is used to detect the contact between two game objects within Box2D.

Click to rate this post!
[Total: 0 Average: 0]
Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *