[英]libGDX Box2D: How to destroy body after collision?
謝謝你的時間。
我正在通過 libGDX 使用 Box2D 創建 Pong 克隆。 當由於球體與兩個目標傳感器體之一接觸而嘗試刪除球體時(下圖),我遇到了一個導致 Null 指針異常的粘滯點。
我想將接觸球體添加到列表中,以便以后可以遍歷列表以刪除球體(以及將來的多個球體)。
堆棧跟蹤:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.physics.box2d.World.destroyBody(World.java:311)
at com.ckq3r.Ponger.screens.GameScreen.update(GameScreen.java:484)
at com.ckq3r.Ponger.screens.GameScreen.render(GameScreen.java:114)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.ckq3r.Ponger.PongerGame.render(PongerGame.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)
我的 GameScreen class 中的第 484 行是world.destroyBody(circleBody);
它位於public void render(float delta){stuff}
方法的if(scoredGoal1 == true){stuff}
條件語句中,如下所示。
public class GameScreen implements Screen, InputProcessor{
/*----methods and variables omitted for readability------*/
private boolean scoredGoal1 = false, scoredGoal2 = false;
ArrayList<Body> ballDeletionList = new ArrayList<Body>();
/*==============Screen implementation methods============*/
@Override
public void show(){
/*ball*/
BodyDef circleDef = new BodyDef();
Body circleBody = world.createBody(circleDef);
circleBody.setUserData(1);
CircleShape circleShape = new CircleShape();
FixtureDef circleFixture = new FixtureDef();
circleFixture.shape = circleShape;
circleBody.createFixture(circleFixture);
circleShape.dispose();
}
@Override
public void render(float delta) {
world.step(Gdx.app.getGraphics().getDeltaTime(), 8, 3);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
debugRenderer.render(world, camera.combined);
/*-------ball deletion experiment-------*/
if(scoredGoal1 == true){
//iterate through ballDeletionList somehow?
world.destroyBody(circleBody);
circleBody.setUserData(null);
circleBody = null;
scoredGoal1 = false;
//clear ballDeletionList
}else if(scoredGoal2 == true){
//iterate through ballDeletionList somehow?
world.destroyBody(circleBody);
circleBody.setUserData(null);
circleBody = null;
scoredGoal2 = false;
//clear ballDeletionList
}
/*-----end ball deletion experiment------*/
}
/*===========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");
/*ball deletion experiment*/
ballDeletionList.add(circleBody);
Gdx.app.log("Ball", "circleBody added to deletion list");
scoredGoal1 = true;
/*ball deletion experiment*/
}
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");
/*ball deletion experiment*/
ballDeletionList.add(circleBody);
Gdx.app.log("Ball", "circleBody added to deletion list");
scoredGoal2 = true;
/*ball deletion experiment*/
}
}
});
關於球體和目標傳感器體之間的接觸,我的邏輯如下:
ArrayList<Body> ballDeletionList = new ArrayList<Body>();
並且scoredGoal1 = true;
boolean 標志將設置為真。render();
檢查scoredGoal1 = true
或scoredGoal2 = true
然后在world.step()
方法之后刪除適用的 Body/Body。我在整個 web 中搜索了其他代碼示例和教程,只是為了找到模棱兩可的答案,因為代碼是特殊的,或者我目前只了解 Java。
如果可以發布 Java/libGDX 代碼示例解決方案,那就太好了。
您無法刪除聯系人偵聽器中的實體,因為它位於世界步驟中,並且世界已鎖定。 在您的render方法中,我所做的與您嘗試做的完全相同:
if(ballDeletionList.size>0) ballDeletionList.clear();
另外,使用libgdx時,建議使用Array http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Array.html而不是ArrayList,這樣可以減少垃圾並進行更多優化。
請問,你有任何關於如何摧毀屍體的簡單例子嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.