首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何获得模型实例的三维坐标?[LibGDX]

如何获得模型实例的三维坐标?[LibGDX]
EN

Game Development用户
提问于 2016-10-30 14:24:32
回答 1查看 3.5K关注 0票数 0

我想从我的模型实例中获取3d坐标(作为Vector3)。使用下面的代码,我只能得到原始的坐标,但是在呈现方法中,我的模型是围绕Y轴旋转(同时移动),我希望得到它的每个帧的坐标。我使用@Xoppa创建的一个小类:

代码语言:javascript
运行
复制
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;
    }
}

这是我的密码:

代码语言:javascript
运行
复制
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的,我从来没有上过一门数学课。我卡住了。

EN

回答 1

Game Development用户

发布于 2016-10-30 14:36:54

试一试如下:

代码语言:javascript
运行
复制
Vector3 position;
position = modelInstance.transform.getTranslation(new Vector3());

这将获取位置并将其存储在position向量中。

有时,您可能需要使用视图和投影矩阵来转换转换,以获得正确的结果。

代码语言:javascript
运行
复制
/* Make sure that you use a new Matrix4, otherwise you will 
   change the models transform, which we don't want to do. */
Matrix4 modelTransform = new Matrix4();
modelTransform.set(modelInstance.transform);
/* Multiply the transform with the combined matrix of the camera. */
modelTransform.mul(camera.combined);
/* Extract the position as usual. */
Vector3 position;
position = modelTransform.getTranslation(new Vector3());

它获取位置并将其存储在position向量和转换中,并将其存储在modelTransform矩阵中。你可能应该重复使用这个向量和矩阵,每次你得到这个位置,而不是创建新的。

票数 3
EN
页面原文内容由Game Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://gamedev.stackexchange.com/questions/132310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档