LIBGDX CollisionListener不会对所有碰撞做出反应

时间:2014-02-13 11:33:56

标签: java libgdx box2d collision-detection

你好,我的游戏中有三节课。具有动态身体设定器的玩家,具有静态身体的平台和具有动态身体的VerticalPlatform因为它正在上下移动。垂直平台正在扩展平台。游戏是无尽的跳投类型。当玩家在平台或VerticalPlatform上跳跃时,他应该跳跃,那部分对我来说是正常的。问题是我想要检测VertivalPlatform何时与其他平台发生碰撞,以便它可以改变它的移动方向。 我测试了我的CollisionFilter,它似乎正常工作,当两个平台发生冲突时它会检测到它但是在ContactListener中,beginContact方法不起作用。

CollisionFilter:

@Override
public boolean shouldCollide(Fixture fixtureA, Fixture fixtureB) {
    //initializing variables
    Player player = null;
    Platform platformA = null;
    Platform platformB = null;  
    boolean playerContact = false;

    //checking collision with Ground

    //checking who is colliding
    //two platforms
    if(fixtureA.getBody().getUserData() instanceof Platform &&
            fixtureB.getBody().getUserData() instanceof Platform){
        return true;
    }else

    //player and platform
    if(fixtureA.getBody().getUserData() instanceof Player){
        player = (Player)fixtureA.getBody().getUserData();
        playerContact = true;
    }else
    if(fixtureB.getBody().getUserData() instanceof Player){
        player = (Player)fixtureB.getBody().getUserData();
        playerContact = true;
    }

    if(playerContact == true){
        //playerContact = false;
        if(fixtureA.getBody().getUserData() instanceof Platform)
            platformA = (Platform) fixtureA.getBody().getUserData();else
        if(fixtureB.getBody().getUserData() instanceof Platform)
            platformA = (Platform) fixtureB.getBody().getUserData();else
                return false;
        playerContact = false;

        if(platformA.getPosition().y - platformA.getHEIGHT()*0.2 
        <= player.getBody().getPosition().y - player.getHEIGHT() / 2 )
        return player.getBody().getLinearVelocity().y <= 0; else return false;
    }

    return false;
}

CollisionListener:

public class MyContactListener implements ContactListener{

private boolean playerCollide = false;
private boolean platformsCollide = false;
Player player = null;
Platform platformA = null;
Platform platformB = null;  

@Override
public void beginContact(Contact contact) {

    //Platforms collision
    if(contact.getFixtureA().getBody().getUserData() instanceof Platform &&
            contact.getFixtureB().getBody().getUserData() instanceof Platform){
        platformA = (Platform) contact.getFixtureA().getBody().getUserData();
        platformB = (Platform) contact.getFixtureB().getBody().getUserData();
        platformsCollide = true;
    }

    //checking if player collide    
    if(contact.getFixtureA().getBody().getUserData() instanceof Player){
        player = (Player)contact.getFixtureA().getBody().getUserData();
    }else
    if(contact.getFixtureB().getBody().getUserData() instanceof Player){
        player = (Player)contact.getFixtureB().getBody().getUserData();
    }

    // Player - Platform collision
    if(player != null){
    if(contact.getFixtureA().getBody().getUserData() instanceof Platform)
        platformA = (Platform) contact.getFixtureA().getBody().getUserData();else
    if(contact.getFixtureB().getBody().getUserData() instanceof Platform)
        platformA = (Platform) contact.getFixtureB().getBody().getUserData();
    playerCollide = true;
    }


}
@Override
public void endContact(Contact contact) {
    player = null;
    platformA = null;
    platformB = null;
}
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
    if(platformsCollide){
        platformA.changeSpeed();
        platformB.changeSpeed();
        platformsCollide = false;
        System.out.println("Dong");
        //contact.setEnabled(false);
    }else
    if(platformA != null && player != null &&
            playerCollide && player.state != player.JUMPING && player.state != player.BEGIN_JUMP)
        if((contact.getWorldManifold().getPoints()[0].y - platformA.getHEIGHT()*0.2 
                <= player.getBody().getPosition().y - player.getHEIGHT() / 2) ){
                player.state = player.BEGIN_JUMP;
                playerCollide = false;
        }
    if(player!= null && platformA == null){
        player.state = player.BEGIN_JUMP;
    }
    //contact.setEnabled(false);


}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
}

}

提前致谢。

0 个答案:

没有答案