【发布时间】:2017-07-08 04:50:47
【问题描述】:
我在使用 libgdx 开发我的小型 2D 横向滚动平台游戏时遇到了这种情况。主要问题是beginContact(Contact contact) 在明显需要时没有被调用。以下是一些简化的代码 sn-ps 以提供快速概览:
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);
}
}
从 Tiled Map Editor 导入的 .tmx 地图的初始化。值得注意的是,整个地形是一个单一的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控制台:
尝试爬上斜坡(或绝对任何非平坦表面)时也会发生同样的情况:
我已经尝试了所有可能的传感器形状和尺寸变化,所以这种行为不是由CircleShape 的尺寸引起的。可能是什么情况?或者任何不涉及ContactListener的解决方法?
【问题讨论】:
标签: java libgdx box2d collision