i am testing collision between two bodies in box2D using contactListener
this is my whole contactListener class i used world.setContactListener(new Box2DWorld(this)) to add it
public class Box2DWorld implements ContactListener{
World world;
GameScreen gameScreen;
Car car;
public enum BodyName{
frontWheel,rearWheel,carShell,ground,finishSensor
}
/**
* this class handles collisions and body userdata
* @param gameScreen
*/
public Box2DWorld(GameScreen gameScreen) {
this.gameScreen = gameScreen;
world = gameScreen.world;
this.car = gameScreen.car;
}
/*
* the debug should be like
*
* BEGONCONTACT,preSolve,postSolve,......postsolve,ENDCONTACT,BEGINCONTACT,preSolve,postSolve
*
* but i am getting debug like
*
* BEGINCONTACT,preSolve,postSolve,......postsolve,BEGINCONTACT,presolve,ENDCONTACT,preSolve,postSolve...
*
* i can't understand why begincontact appears twice without endcontact in between and also bodies are in contact
* even after endcontact
*
*/
@Override
public void beginContact(Contact contact) {
BodyName bodyA = (BodyName) contact.getFixtureA().getBody().getUserData();
BodyName bodyB = (BodyName) contact.getFixtureB().getBody().getUserData();
if((bodyA == BodyName.frontWheel && bodyB == BodyName.ground )||(bodyA == BodyName.ground && bodyB == BodyName.frontWheel)){
car.frontWheel.onGround = true;
System.out.println("begin contact");
}
}
@Override
public void endContact(Contact contact) {
BodyName bodyA = (BodyName) contact.getFixtureA().getBody().getUserData();
BodyName bodyB = (BodyName) contact.getFixtureB().getBody().getUserData();
if((bodyA == BodyName.frontWheel && bodyB == BodyName.ground )||(bodyA == BodyName.ground && bodyB == BodyName.frontWheel)){
car.frontWheel.onGround = false;
System.out.println("contact end");
}
}
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
BodyName bodyA = (BodyName) contact.getFixtureA().getBody().getUserData();
BodyName bodyB = (BodyName) contact.getFixtureB().getBody().getUserData();
if((bodyA == BodyName.frontWheel && bodyB == BodyName.ground )||(bodyA == BodyName.ground && bodyB == BodyName.frontWheel)){
System.out.println("PreSolve");
}
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
BodyName bodyA = (BodyName) contact.getFixtureA().getBody().getUserData();
BodyName bodyB = (BodyName) contact.getFixtureB().getBody().getUserData();
if((bodyA == BodyName.frontWheel && bodyB == BodyName.ground )||(bodyA == BodyName.ground && bodyB == BodyName.frontWheel)){
System.out.println("postSolve");
}
}
my dedub goes like this
beginContact preSolve postSolve . . . . . . preSolve postSolve beginContact preSolve endcontact postSolve presolve . . . .
i can't understand why begincontact is appearing before endcontact
both ground and frontWheel bodies have only 1 fixture each
i can't figure out this from a week pls help me