libgdx box2D contactlistener

时间:2014-08-24 10:14:24

标签: libgdx box2d

我正在使用contactListener

测试box2D中两个物体之间的碰撞

这是我的整个contactListener类,我使用 world.setContactListener(新的Box2DWorld(this))来添加它

 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");    
    }

}

我的dedub就像这样

beginContact preSolve postSolve 。 。 。 。 。 。 preSolve postSolve 的 beginContact preSolve 的 endcontact postSolve presolve 。 。 。

我无法理解为什么begincontact出现在endcontact之前

地面和前轮身体每个只有一个夹具

我无法从一周内弄清楚这一点请帮助我

1 个答案:

答案 0 :(得分:0)

请记住,联系人介于两个固定装置之间,而不是两个固定装置。

我猜你的地面是由许多固定装置制成的,它们都在同一个地面上吗?当滚轮在固定装置上滚动时,它一次可以触摸多个固定装置,因此您可以按顺序开始(fixture1),开始(fixture2),结束(fixture1)。这很正常。我想如果你打印出每个联系活动中涉及的地面夹具,你会看到我的意思。

如果您想知道车轮何时接触地面,您需要跟踪当前接触的灯具总数。你可以通过增加一个计数器来做到这一点,当你开始'开始'事件,并在你得到一个结束时减少它。事件。然后你可以随时检查计数器,如果它大于零,则车轮接触地面。