如何在LibGDX中获取模型实例的3d坐标?

时间:2016-10-30 18:27:05

标签: java 3d libgdx coordinates

我想从我的模型实例中获取3d坐标(作为Vector3)。 使用下面的代码我只能获得原始坐标,但在渲染方法中,我的模型围绕Y轴旋转(并同时移动),我希望每帧得到它的坐标。

我正在使用@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的,我从未有过关于它的数学课程。我被卡住了。

1 个答案:

答案 0 :(得分:2)

GameObject类:

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方法时,它甚至不会渲染。

很抱歉有多个答案。我不知道如何在评论部分格式化