为什么Libgdx Bullet中的ContactListener派生类中的数组大小始终为零?

时间:2017-03-08 09:43:47

标签: java arrays libgdx bulletphysics

我正在尝试使用Bullet包装器在Libgdx中创建一个SELECT `Chat`.`id`, `Chat`.`sender_id`, `Chat`.`receiver_id`, `Chat`.`message`, `Chat`.`datetime`, `Chat`.`converstation_id` FROM `gopher`.`chat` AS `Chat` WHERE ((`Chat`.`sender_id` = 10) OR (`Chat`.`receiver_id` = 10)) GROUP BY converstation_id ORDER BY `Chat`.`id` DESC LIMIT 0,1 派生类,用于碰撞检测,就像在这个tutorial中一样,但是在不同的类中。它将渲染和游戏世界的类分开。在类ContactListener的{​​{1}}方法中,我将一个模型实例Array传递给此派生类。但是当我运行它时会给出一个因为数组大小为零。这是派生类:

render()

这是Renderer类:

Render

我做错了什么?在package com.anutrix.brickbreaker3d.Helpers; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g3d.ModelInstance; import com.badlogic.gdx.physics.bullet.collision.ContactListener; import com.badlogic.gdx.utils.Array; public class CollisionListener extends ContactListener { private Array<ModelInstance> instances; public CollisionListener() { this.instances = new Array<ModelInstance>(); } public void setModelInstances(Array<ModelInstance> instances) { this.instances = instances; } @Override public boolean onContactAdded(int userValue0, int partId0, int index0, int userValue1, int partId1, int index1) { //instances.get(colObj1Wrap.getCollisionObject().getUserValue()).collided = false;error Gdx.app.log("instances.size", Integer.toString(instances.size));//zero Gdx.app.log("ddhbdfhd", "fhfgjfgj"); return true; } } 内,instances.size是正确的。但在每次调用之后,package com.anutrix.brickbreaker3d.gameWorld; import com.anutrix.brickbreaker3d.gameObjects.Ball; import com.anutrix.brickbreaker3d.gameObjects.Brick; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.PerspectiveCamera; import com.badlogic.gdx.graphics.g3d.Environment; import com.badlogic.gdx.graphics.g3d.ModelBatch; import com.badlogic.gdx.graphics.g3d.ModelInstance; import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight; import com.badlogic.gdx.graphics.g3d.utils.CameraInputController; import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder; import com.badlogic.gdx.physics.bullet.DebugDrawer; import com.badlogic.gdx.physics.bullet.collision.btCollisionDispatcher; import com.badlogic.gdx.physics.bullet.collision.btCollisionWorld; import com.badlogic.gdx.physics.bullet.collision.btDbvtBroadphase; import com.badlogic.gdx.physics.bullet.collision.btDefaultCollisionConfiguration; import com.badlogic.gdx.physics.bullet.linearmath.btIDebugDraw; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Disposable; public class GameRenderer implements Disposable { private GameWorld gameWorld; private PerspectiveCamera cam; public ModelBatch modelBatch; private CameraInputController camController; private Environment environment; public Array<ModelInstance> instances; ModelBuilder mb = new ModelBuilder(); btCollisionDispatcher dispatcher; public GameRenderer(GameWorld world) { this.modelBatch = new ModelBatch(); this.environment = new Environment(); this.instances = new Array<ModelInstance>(); gameWorld = world; cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); cam.position.set(10f, 10f, 0f); cam.lookAt(0, 0, 0); cam.near = 1f; cam.far = 300f; cam.update(); camController = new CameraInputController(cam); Gdx.input.setInputProcessor(camController); environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f)); environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f)); } public void render() { //Gdx.app.log("GameRenderer", "render"); Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); Gdx.gl.glClearColor(0f, 0.2f, 0.2f, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); for (Brick b : gameWorld.bricks) { b.getObject().setUserValue(instances.size); instances.add(b.getModelInstance()); } for (Ball b : gameWorld.balls) { b.getObject().setUserValue(instances.size); instances.add(b.getModelInstance()); } gameWorld.collisionListener.setModelInstances(instances); modelBatch.begin(cam); modelBatch.render(instances, environment); modelBatch.end(); instances.clear(); } @Override public void dispose() { modelBatch.dispose(); } } 等于0。  此外,我不确定通过引用传递(因为Java使用pass by value)。如果我只调用setModelInstances()一次,那么它是否更好(如果有效)?

1 个答案:

答案 0 :(得分:2)

CollisionListener#instances方法中调用GameRenderer#instances后,gameWorld.collisionListener.setModelInstances(instances);GameRenderer#render()都指向相同的引用。

然后,在方法结束时,您正在调用:

    instances.clear();

这将清除instances。因此,当您拨打render时,大小将变为0。

相反,在setModelInstances方法中,您可以创建一个新的Array实例,如下所示:

public void setModelInstances(Array<ModelInstance> instances) {
    this.instances = new Array<>(instances);
}

希望这有帮助!