如何在 LibGDX 中正确旋转和移动 3D 透视相机 [英] How to correctly rotate and move a 3D perspective camera in LibGDX

查看:50
本文介绍了如何在 LibGDX 中正确旋转和移动 3D 透视相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几周以来,我一直在断断续续地尝试正确处理 LibGDX 中的对象和相机旋转.

I have been trying on and off now for a few weeks to correctly handle object and camera rotation in LibGDX.

我在我的对象的自定义类中有以下移动和 yrotate 方法,'this' 是一个 ModelInstance:

I have the below move and yrotate methods in a custom class for my objects, 'this' being a ModelInstance:

public void move(float i) {
    // TODO Auto-generated method stub
    this.instance.transform.translate(0, 0, i);
    this.instance.calculateTransforms();

}

public void yrotate(float i) {
    // TODO Auto-generated method stub
    this.instance.transform.rotate(Vector3.Y, -i);
    this.instance.calculateTransforms();

}

这一切似乎都很好,因为我可以旋转对象并将它们正确地沿旋转方向移动(尽管我必须承认我对为什么 Y 向量必须为负数感到有些困惑).

This all seems to work fine as I can rotate objects and move them in the rotated direction correctly (though I must admit I'm a bit stumped as to why the Y Vector has to be negative).

我现在正在尝试为相机复制这个.我可以看到相机也有很多可用的方法,但它们与用于物体的方法并不完全匹配.目前我正在为相机执行以下操作:

I am now trying to replicate this for the camera. I can see that the camera also has a lot of methods available to it but they do not exactly match those used for objects. At the moment I am doing the following for the camera:

void move(float i) {
    // cam.translate(0, 0, i);
    cam.position.add(0, 0, i);
    cam.update();

}

void yrotate(float i) {
    cam.rotate(Vector3.Y, -i);
    // cam.direction.rotate(Vector3.Y, -i);
    cam.update();
}

上面似乎旋转和移动了相机.然而,当移动相机时,位置 x、y 和 z 没有考虑已经应用的旋转.

The above seems to rotate and move the camera. However, when moving the camera the position x, y and z is not taken into consideration the rotation that has been applied.

我认为对于对象来说,'calculateTransforms' 位在这里发挥了神奇作用,可以确保对象沿其面向的方向移动,但我正在努力为相机找到这样的东西.

I am thinking that for the objects it's the 'calculateTransforms' bit that does the magic here in making sure the object moves in the direction it's facing, but I'm struggling to find anything like this for the camera.

任何帮助将不胜感激!

推荐答案

如果你想将相机移动到它正在观察的方向,那么你可以这样做:

If you want to move the camera into the direction it is looking into, then you can do that something like this:

private final Vector3 tmpV = new Vector3();
void move(float amount) {
    cam.position.add(tmpV.set(cam.direction).scl(amount));
    cam.update();
}

如果您出于某种原因不喜欢添加临时向量,那么您可以内联缩放向量:

If you for some reason don't like to add a temporary vector then you can scale the vector inline:

void move(float a) {
    cam.position.add(cam.direction.x * a, cam.direction.y * a, cam.direction.z * a);
    cam.update();
}

顺便说一句,如果您没有修改节点,则不应调用 calculateTransforms.另请参阅文档:

Btw, you shouldn't call calculateTransforms if you didn't modify the nodes. See also the documentation:

如果节点的任何局部属性(平移、旋转、缩放)被修改,此方法可用于重新计算所有变换.

This method can be used to recalculate all transforms if any of the Node's local properties (translation, rotation, scale) was modified.

这篇关于如何在 LibGDX 中正确旋转和移动 3D 透视相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆