每个人都有什么,
我试图通过libGDX在Box2D中产生一个Pong克隆。 Null Pointer Exception源自ContactListener的beginContact()方法,我在其中尝试实现得分逻辑。
Box2D世界使用两个独立的EdgeShapes作为传感器,用于在与Ball碰撞时递增得分变量(查看附加图像)。 Ball和两个EdgeShapes之间的碰撞逻辑有效,但Ball和Box2D世界中任何其他东西之间的碰撞都会使程序崩溃。
堆栈跟踪:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.ckq3r.Pong.screens.GameScreen$2.beginContact(GameScreen.java:491)
at com.badlogic.gdx.physics.box2d.World.beginContact(World.java:876)
at com.badlogic.gdx.physics.box2d.World.jniStep(Native Method)
at com.badlogic.gdx.physics.box2d.World.step(World.java:602)
at com.ckq3r.Pong.screens.GameScreen.render(GameScreen.java:99)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.ckq3r.Pong.PongGame.render(PongGame.java:236)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:204)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:112)
有问题的代码:
/**Box2D contact listener**/
private void createContactListener() {
world.setContactListener(new ContactListener() {
@Override
public void beginContact(Contact contact) {
Fixture fixtureA = contact.getFixtureA();
Fixture fixtureB = contact.getFixtureB();
Gdx.app.log("beginContact", "between " + fixtureA.toString() + " and " + fixtureB.toString());
if(fixtureA.getBody().getUserData().equals(1) && fixtureB.getBody().getUserData().equals(2) || fixtureA.getBody().getUserData().equals(2) && fixtureB.getBody().getUserData().equals(1)){
Gdx.app.log("HIT", "goal1 contact");
score1++;
score1String = score1 + "";
}
if(fixtureA.getBody().getUserData().equals(1) && fixtureB.getBody().getUserData().equals(3) || fixtureA.getBody().getUserData().equals(3) && fixtureB.getBody().getUserData().equals(1)){
Gdx.app.log("HIT", "goal2 contact");
score2++;
score2String = score2 + "";
}
}
@Override
public void endContact(Contact contact) {
Fixture fixtureA = contact.getFixtureA();
Fixture fixtureB = contact.getFixtureB();
Gdx.app.log("endContact", "between " + fixtureA.toString() + " and " + fixtureB.toString());
}
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
}
});
}
注意:
答案 0 :(得分:1)
在Box2D世界中为每个正文设置userdata,以便您可以在ContactListener中使用条件逻辑来评估是否应该处理碰撞。
重点放在下面的paddleBody.setUserData(4);
行。您可以选择任何数字来设置单个对象的用户数据,只要该数字与您所在世界中其他对象的用户数据唯一。
例如:
/**paddle1**/
BodyDef paddleDef = new BodyDef();
paddleDef.type = BodyType.DynamicBody;
paddleDef.position.set(width * 0.2f, height / 2);
paddleDef.fixedRotation = true;
Body paddleBody = world.createBody(paddleDef);
paddleBody.setUserData(4);
PolygonShape paddleShape = new PolygonShape();
paddleShape.setAsBox(3.0f, 15.0f); //half-width and half-height
FixtureDef paddleFD = new FixtureDef();
paddleFD.shape = paddleShape;
paddleFD.density = 10.0f;
paddleFD.friction = 0.0f;
paddleFD.restitution = 0.1f;
paddleBody.createFixture(paddleFD);
paddleShape.dispose();
paddleBody.setLinearVelocity(0.0f, 0.0f);
/**end paddle1**/
/**paddle2**/
BodyDef paddleDef2 = new BodyDef();
paddleDef2.type = BodyType.DynamicBody;
paddleDef2.position.set(width * 0.8f, height / 2);
paddleDef2.fixedRotation = true;
Body paddleBody2 = world.createBody(paddleDef2);
paddleBody2.setUserData(5);
PolygonShape paddleShape2 = new PolygonShape();
paddleShape2.setAsBox(3.0f, 15.0f); //half-width and half-height
FixtureDef paddleFD2 = new FixtureDef();
paddleFD2.shape = paddleShape2;
paddleFD2.density = 10.0f;
paddleFD2.friction = 0.0f;
paddleFD2.restitution = 0.1f;
paddleBody2.createFixture(paddleFD2);
paddleShape2.dispose();
paddleBody2.setLinearVelocity(0.0f, 0.0f); // Move nowhere at a rate of x = 0.0f, y = 0.0f meters per second
/**end paddle2**/