2017-03-08 3 views
1

tutorial에서와 같이 별도의 클래스에서 충돌 검색을 위해 Bullet 래퍼를 사용하여 Libgdx에서 ContactListener 파생 클래스를 만들려고합니다. 렌더링 및 게임 세계를위한 클래스를 분리합니다. 클래스 Renderrender() 메서드에서이 인스턴스 클래스의 모델 인스턴스를 전달합니다. 하지만 실행할 때 배열 크기가 0이기 때문에이를 제공합니다. 여기왜 Libgdx Bullet의 ContactListener 파생 클래스에서 배열 크기가 항상 0입니까?

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; 
    } 
} 

렌더러 클래스 :

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(); 
    } 

} 

내가 잘못하고있는 중이 야 무슨 일이 여기에서 파생 된 클래스는? setModelInstances() 안에있는 instances.size는 정확합니다. 그러나 각 호출 후에 instances.size은 0과 같습니다. 또한 Java에서 값을 통한 전달을 사용하므로 참조로 전달할 수 있는지 확실하지 않았습니다. 그래서 만약 내가 setInstances()을 한번만 부르면 더 효과적일까요?

답변

2

CollisionListener#instancesGameRenderer#instances은 모두 GameRenderer#render() 방법으로 gameWorld.collisionListener.setModelInstances(instances);으로 전화 한 후 동일한 참조 번호를 가리 킵니다.

그런 방법의 끝에서, 당신은 호출된다 : 이것은 instances을 취소 할 것

instances.clear(); 

. 따라서 render으로 전화하면 크기가 0이됩니다.


대신, setModelInstances 방법 안에,이 같은 새 Array 인스턴스를 만들 수 있습니다

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

희망이 도움이!

+0

그럼 어떻게'instances.clear();'를 대체 할 수 있습니까? 이를 제거하면 각 루프가 끝난 후 인스턴스 수가 증가합니다. – Anutrix

+1

@Anutrix 당신은 배열을'setModelInstances'에 복사 할 수 있습니다 (참조를 할당하는 대신) – UnholySheep

+0

업데이트 된 대답보기 .. – anacron