I am new in box2d and trying to implement it in a LibGDX Game. What I want to do is to detect the collision between the various bodies. So,I made a class collisionDetection and implement ContactListener
in it which gives me 4 overridden methods i.e. beginContact()
and endContact()
which I have to deal with. Also I am passing the object of collisionDetection Class in the world.setcontactListner(collisionDet)
by which the overridden methods of collisionDetection class will be called when the bodies in the world class collide with each other. But the problem is occuring when the bodies collide beginContact()
method is called everytime but endContact()
method is not called everytime when the bodies lost the contact.So, what are possible ways we can detect the endContact()
everytime.
The code of collisionDetection class is as follow:
public class CollisionDetection implements ContactListener {
static Fixture fixtureA;
static Fixture fixtureB;
public static boolean Colliding=false;
World world;
protected CollisionDetection(World world, long addr) {
this.world = world;
// TODO Auto-generated constructor stub
}
@Override
public void beginContact(Contact contact) {
// TODO Auto-generated method stub
fixtureA = contact.getFixtureA();
fixtureB = contact.getFixtureB();
Colliding=true;
}
@Override
public void endContact(Contact contact) {
// TODO Auto-generated method stub
Colliding=false;
}
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
// TODO Auto-generated method stub
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
// TODO Auto-generated method stub
}
And World Class
collisionDet = new CollisionDetection(worldbox, 0);
worldbox.setContactListener(collisionDet);
Thanks
endContact()
is not called everytime. – Jagdeep Singh