gpt4 book ai didi

java - Box2D 碰撞检测失败

转载 作者:行者123 更新时间:2023-11-30 10:46:25 25 4
gpt4 key购买 nike

所以我遇到了一个我真的不明白的问题。在试图找出问题所在后,我决定记录一个video。并在这里提问。

在视频中,请注意左下角的 True/False boolean 值。这是我的变量 canJump 的值。一开始,只需左右移动“Player”,值就会在 true 和 false 之间变化。当玩家上下斜坡时也会发生这种情况。

map /碰撞层是用Tiled 创建的.

我的 TiledObject 类:

public class TiledObjectUtil {

public static float PPM = 32;

public static void parseTiledObjectLayer(World world, MapObjects objects) {
for(MapObject object : objects) {
Shape shape;

if(object instanceof PolylineMapObject) {
shape = createPolyLine((PolylineMapObject) object);

} else {
continue;
}

Body body;
BodyDef bdef = new BodyDef();
bdef.type = BodyDef.BodyType.StaticBody;
body = world.createBody(bdef);
body.createFixture(shape, 1.0f);

shape.dispose();
}
}

private static ChainShape createPolyLine(PolylineMapObject polyline) {
float[] vertices = polyline.getPolyline().getTransformedVertices();
Vector2[] worldVertices = new Vector2[vertices.length / 2];

for(int i = 0; i<worldVertices.length; i++) {
worldVertices[i] = new Vector2(vertices[i * 2] / PPM, vertices[i*2+1] / PPM);
}
ChainShape cs = new ChainShape();
cs.createChain(worldVertices);
return cs;
}}

还有我的 Player 类:

public class Player {

private BodyDef def = new BodyDef();
public Body playerBody;


private float speed = 10;

public Player() {

}

public void update() {

if (InputUtil.moveLeft) {
playerBody.setLinearVelocity(-speed, playerBody.getLinearVelocity().y);
}
if (InputUtil.moveRight) {
playerBody.setLinearVelocity(speed, playerBody.getLinearVelocity().y);
}

if (!InputUtil.moveLeft && !InputUtil.moveRight) {

playerBody.setLinearVelocity(0, playerBody.getLinearVelocity().y);
}
}

public void jump() {
if (ContactUtil.canJump) {
playerBody.applyLinearImpulse(0, 80, 0, 0, true);

}

}


public Body createPlayer(World world) {
def.type = BodyDef.BodyType.DynamicBody;
def.position.set(20, 20);
def.fixedRotation = true;

playerBody = world.createBody(def);

PolygonShape shape = new PolygonShape();
shape.setAsBox(2f / 2, 2f / 2);

FixtureDef playerFixture = new FixtureDef();
playerFixture.density = 1f;
playerFixture.shape = shape;
playerFixture.restitution = 0f;
playerFixture.friction = 1f;
playerBody.createFixture(playerFixture);

shape.setAsBox(2f / 2, 1f / 2, new Vector2(0, 0 - 1), 0);
playerFixture.shape = shape;
playerFixture.isSensor = true;
playerBody.createFixture(playerFixture).setUserData("player");

shape.dispose();

return playerBody;
} }

最后,我的 ContactListener:

public class ContactUtil implements ContactListener {

public static boolean canJump;


public ContactUtil() {

}

@Override
public void beginContact(Contact contact) {

Fixture fixtureA = contact.getFixtureA();
Fixture fixtureB = contact.getFixtureB();

System.out.println(fixtureA.getUserData() + ", " + fixtureB.getUserData());

if (fixtureA.getUserData() == "player" && fixtureB.getUserData() == null) {
canJump = true;
}
if (fixtureA.getUserData() == null && fixtureB.getUserData() == "player") {
canJump = true;
}

}

@Override
public void endContact(Contact contact) {

Fixture fixtureA = contact.getFixtureA();
Fixture fixtureB = contact.getFixtureB();

System.out.println(fixtureA.getUserData() + ", " + fixtureB.getUserData());

if (fixtureA.getUserData() == "player" && fixtureB.getUserData() == null) {
canJump = false;
}
if (fixtureA.getUserData() == null && fixtureB.getUserData() == "player") {
canJump = false;
}

}

此外,我的播放器每次停在斜坡上时都会跳动一点。我知道这是由于这一行:playerBody.setLinearVelocity(0, playerBody.getLinearVelocity().y);

如果有人知道处理运动的更好方法,将不胜感激。

最佳答案

我似乎记得过去有过这个确切的问题!我认为问题在于您在 beginContactendContact 中的逻辑。

我会用我粗糙的 MS Paint 技能来解释这个问题。想象一下以下情况:您的玩家在空中,向下方的平台坠落。如您所料,canJump 为 false: enter image description here

玩家掉落并着陆,调用平台一的 beginContact。这会将 canJump 设置为 true。同样,这是我们所期望的: enter image description here

现在玩家向右移动,直到接触到二号平台。调用平台二的 beginContact。再一次,canJump 被设置为 true。 enter image description here

这就是问题所在。最后一步,玩家仍与一号平台保持联系。但现在它们不是,所以调用了平台一的 endContactcanJump 现在设置为 false。这解释了您遇到的意外行为。 enter image description here

解决方案非常简单。您需要维护播放器底部接触的联系人列表。在这篇 iforce2d 文章中有一个指南可以做到这一点:http://www.iforce2d.net/b2dtut/jumpability

祝你好运!

关于java - Box2D 碰撞检测失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36553499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com