2016-10-30 60 views
0

我想從我的模型實例中獲取三維座標(作爲Vector3)。 下面的代碼我只能得到原始座標,但在渲染方法中,我的模型圍繞Y軸旋轉(並同時移動),我想每幀得到它的座標。如何獲取LibGDX中模型實例的三維座標?

我使用@Xoppa創建一個小類:

public static class GameObject extends ModelInstance { 
    public Vector3 center = new Vector3(); 
    public Vector3 dimensions = new Vector3(); 
    public float radius; 

    public BoundingBox bounds = new BoundingBox(); 

    public GameObject (Model model, String rootNode, boolean mergeTransform) { 
     super(model, rootNode, mergeTransform); 
     calculateBoundingBox(bounds); 
     bounds.getCenter(center); 
     bounds.getDimensions(dimensions); 
     radius = dimensions.len()/2f; 
    } 
} 

,這裏是我的代碼:

public void render(float delta) { 
    super.render(delta); 
    if (loading && manager.update()) { 
     doneLoading(); 
    } 

    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); 

    batch.begin(); 
    backgroundSprite.draw(batch); 
    batch.end(); 

    for(GameObject instance : instances){ 
     instance.transform.rotate(new Vector3(0, 1, 0), 0.5f); 
     float x = instance.bounds.getCenterX(); 
     float y = instance.bounds.getCenterY(); 
     float z = instance.bounds.getCenterZ(); 
     System.out.println(x+" "+y+" "+z); 
     if(isVisible(cam, instance)){ 
      modelBatch.begin(cam); 
      modelBatch.render(instance, environment); 
      modelBatch.end(); 
     } 
    } 
} 

有沒有功能「爲getPosition」或類似的東西,它是所有關於Matrix4我從來沒有過關於它的數學課程。我卡住了。

+0

在這裏你去:http://badlogicgames.com/forum/viewtopic.php?f=11&t=17878&p=75338#p75338 – Xoppa

+0

@Xoppa我跟着你做了什麼你的教程,但它似乎並沒有工作: – Vellyxenya

+0

這裏是我的渲染方法:'for(GameObject實例:實例){0} {0} {0} {0}實例.transform.rotate(new Vector3(0,1,0),0.5f); instance.updateTransform(); System.out.println(instance.position);如果(isVisible(cam,instance)){model_atch_begin(cam); modelBatch.render(instance,environment); modelBatch.end(); } }和GameObject類: – Vellyxenya

回答

1

而且遊戲對象類:

public static class GameObject extends ModelInstance { 
    public Vector3 center = new Vector3(); 
    public Vector3 dimensions = new Vector3(); 
    public float radius; 
    public final Vector3 position = new Vector3(); 
    public final Quaternion rotation = new Quaternion(); 
    public final Vector3 scale = new Vector3(); 

    public BoundingBox bounds = new BoundingBox(); 

    public GameObject (Model model, String rootNode, boolean mergeTransform) { 
     super(model, rootNode, mergeTransform); 
     calculateBoundingBox(bounds); 
     bounds.getCenter(center); 
     bounds.getDimensions(dimensions); 
     radius = dimensions.len()/2f; 
    } 

    public void updateTransform() { 
     this.transform.set(position, rotation, scale); 
    } 
}`  

它只返回(0,0,0),當我使用updateTransform方法,它甚至不渲染。

對不起,有多個答案。不知道如何格式在評論部分