8

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

Jagdeep Singh
  • 1,200
  • 1
  • 16
  • 34
  • Are you certain that the bodies are losing contact? Use the Box2dDebugRenderer to check. – R Hyde Apr 05 '13 at 07:21
  • 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 Apr 05 '13 at 07:33
  • 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? – R Hyde Apr 05 '13 at 07:43
  • That was just for testing reason. I updated it. – Jagdeep Singh Apr 05 '13 at 09:05
  • 2
    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. – R Hyde Apr 06 '13 at 21:29

1 Answers1

0

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

Vikalp Jain
  • 1,419
  • 1
  • 11
  • 20
  • No I am using body.setLinearVelocity() to move them. – Jagdeep Singh Jun 29 '13 at 12:28
  • 1
    have you debugged your code using System.out.println("endcontacr"); in endContact as i can see that you have taken boolean variable Colliding as static and it might be giving callback of both begin contact and endcontact but it may be in same step cycle of box2d so you coudn't detect it. try debugging using system.out.println – Vikalp Jain Jun 30 '13 at 17:41