7
votes

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

1
Are you certain that the bodies are losing contact? Use the Box2dDebugRenderer to check.Rod Hyde
Yes, they are losing contact because I am moving one body(Dynamic) with help of TouchPad coordinates and when the body goes through another bodies(Static), endContact() is not called everytime.Jagdeep Singh
Fair enough. The reason I suggested using the debug renderer is that it draws the bodies themselves, rather than anything you might be drawing which may or may not match the bodies. Also, is there any reason why you are calling setEnabled(true) inside the callbacks?Rod Hyde
That was just for testing reason. I updated it.Jagdeep Singh
Do you have a minimum piece of example code that will demonstrate the problem as what you have here looks reasonably sensible. But what you aren't showing us might be equally relevant, for example, how often do you call world.step() and what parameters do you pass it, etc.Rod Hyde

1 Answers

0
votes

You might be using setTranform() for moving the object... Box2d doesn't give callback when we use set transform