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

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

问题描述

我已经尝试打开和关闭几个星期,以正确处理LibGDX中的对象和相机旋转.

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

在我的对象的自定义类中,我具有以下move和yrotate方法,这"是一个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 Vector必须为负值有些困惑).

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:

如果修改了Node的任何局部属性(平移,旋转,缩放),则可以使用此方法重新计算所有变换.

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天全站免登陆