在使用libgdx开发我的小型2D横向滚动平台游戏时,我遇到了这种情况。主要问题是beginContact(Contact contact)在明确需要时没有被调用。以下是一些简化的代码片段,以提供快速概述:

ContactListener类记录传感器与地面之间的每次接触:

public class WorldContactListener implements ContactListener
{
Player player;

public WorldContactListener(Player player, World world)
{
    this.player = player;
    world.setContactListener(this);
}

@Override
public void beginContact(Contact contact)
{
    Object userDataA = contact.getFixtureA().getUserData();
    Object userDataB = contact.getFixtureB().getUserData();

    if((userDataA == Tomb.UserData.GROUND || userDataB == Tomb.UserData.GROUND)
  && (userDataA == Tomb.UserData.PLAYER_FEET  || userDataB == Tomb.UserData.PLAYER_FEET))
    {
        Gdx.app.log("collision", "start");
    }
}

@Override
public void endContact(Contact contact)
{
    Object userDataA = contact.getFixtureA().getUserData();
    Object userDataB = contact.getFixtureB().getUserData();

    if((userDataA == Tomb.UserData.GROUND || userDataB == Tomb.UserData.GROUND)
  && (userDataA == Tomb.UserData.PLAYER_FEET  || userDataB == Tomb.UserData.PLAYER_FEET))
    {
        Gdx.app.log("collision", "stop");
    }
}

@Override
public void preSolve(Contact contact, Manifold oldManifold)
{
}

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


玩家创建:

    //Main rectangle:
    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(spawnCoordinates);
    bodyDef.type = BodyType.DynamicBody;
    body = world.createBody(bodyDef);
    FixtureDef fixtureDef = new FixtureDef();
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(5 / Tomb.PPM, 14 / Tomb.PPM);
    fixtureDef.shape = shape;
    fixtureDef.friction = FRICTION; //1
    fixtureDef.restitution = RESTITUTION; //0
    body.createFixture(fixtureDef).setUserData(Tomb.UserData.PLAYER);

    //Circle-shaped sensor:
    fixtureDef = new FixtureDef();
    CircleShape feet = new CircleShape();
    feet.setPosition(new Vector2(0, -16 / Tomb.PPM));
    feet.setRadius(10 / Tomb.PPM);
    fixtureDef.shape = feet;
    fixtureDef.isSensor = true;
    body.createFixture(fixtureDef).setUserData(Tomb.UserData.PLAYER_FEET);


玩家动作:

private void handleInput(float delta)
{
    if(Gdx.input.isKeyJustPressed(Input.Keys.UP))
    {
        body.applyLinearImpulse(new Vector2(0, JUMP_POWER), body.getWorldCenter(), true);
    }
    if(Gdx.input.isKeyPressed(Input.Keys.RIGHT) && body.getLinearVelocity().x <= MAX_HORIZONTAL_VELOCITY)
    {
        body.applyLinearImpulse(new Vector2(HORIZONTAL_SPEED, 0), body.getWorldCenter(), true);
    }
    if(Gdx.input.isKeyPressed(Input.Keys.LEFT) && body.getLinearVelocity().x >= -MAX_HORIZONTAL_VELOCITY)
    {
        body.applyLinearImpulse(new Vector2(-HORIZONTAL_SPEED, 0), body.getWorldCenter(), true);
    }
}


.tmx地图的初始化,该地图是从Tiled Map Editor导入的。值得注意的是,整个地形是一个单独的PolygonMapObject,因此在这种情况下,尤其不需要for循环。

protected void defineGround(int layerIndex)
{
    for(PolygonMapObject object : map.getLayers().get(layerIndex).getObjects().getByType(PolygonMapObject.class))
    {
        BodyDef bodyDef = new BodyDef();
        ChainShape chainShape = new ChainShape();
        FixtureDef fixtureDef = new FixtureDef();
        Body body;

        Polygon polygon = object.getPolygon();
        bodyDef.type = BodyType.StaticBody;
        bodyDef.position.set(polygon.getX() / Tomb.PPM, polygon.getY() / Tomb.PPM);
        body = world.createBody(bodyDef);
        float[] scaledVertices = new float[polygon.getVertices().length];
        for(int i = 0; i < polygon.getVertices().length; i++)
        {
            scaledVertices[i] = polygon.getVertices()[i] / Tomb.PPM;
        }
        chainShape.createChain(scaledVertices);
        fixtureDef.shape = chainShape;
        body.createFixture(fixtureDef).setUserData(Tomb.UserData.GROUND);
    }
}


最后,自我解释的图片,显示游戏屏幕本身和带有WorldContactListener日志的eclipse控制台:

java - Libgdx box2d ContactListener非常故障-LMLPHP
java - Libgdx box2d ContactListener非常故障-LMLPHP

java - Libgdx box2d ContactListener非常故障-LMLPHP

尝试爬上斜坡(或绝对是任何非平坦表面)时,也会发生相同的事情:

java - Libgdx box2d ContactListener非常故障-LMLPHP
java - Libgdx box2d ContactListener非常故障-LMLPHP

我已经尝试了所有可能的传感器形状和大小变化,因此此行为不是由CircleShape的大小引起的。可能是什么情况?还是不涉及ContactListener的任何解决方法?

最佳答案

达成的协议是一次有多次碰撞。在最后一张图像上,您可以看到播放器与多边形之间仍然存在(2-1 + 2-1)= 2个碰撞。

这是我的示例解决方案:
使用incCollision()和decCollision()函数创建一些碰撞计数器类,也许还可以通过bool isCollison()告诉您是否确实存在碰撞。

编辑您的ContactListener类:

public void beginContact(Contact contact)
{
...
   if((userDataA == Tomb.UserData.GROUND || userDataB == Tomb.UserData.GROUND)
      && (userDataA == Tomb.UserData.PLAYER_FEET  || userDataB == Tomb.UserData.PLAYER_FEET))
        {
            Gdx.app.log("collision", "start");
            counter.incCollision();
        }
}

public void endContact(Contact contact)
{
...
if((userDataA == Tomb.UserData.GROUND || userDataB == Tomb.UserData.GROUND)
  && (userDataA == Tomb.UserData.PLAYER_FEET  || userDataB == Tomb.UserData.PLAYER_FEET))
    {
        Gdx.app.log("collision", "stop");
        counter.decCollision();
    }
}

10-08 19:17