java - 在 libGDX 中将四元数应用于相机

标签 java android opengl-es libgdx quaternions

我正在尝试使用 libGDX 中的四元数来旋转相机。我创建了一个四元数并正在对其进行操作,但我不知道如何将其应用到相机,我尝试过的所有操作都根本没有移动相机。

这是我设置旋转四元数的方法:

    public void rotateX(float amount) {
        tempQuat.set(tempVector.set(1.0f, 0.0f, 0.0f), amount * MathHelper.PIOVER180);
        rotation = rotation.mul(tempQuat);
    }

    public void rotateY(float amount) {
        tempQuat.set(tempVector.set(0.0f, 1.0f, 0.0f), amount * MathHelper.PIOVER180);
        rotation = tempQuat.mul(rotation);
    }

这是我尝试更新相机的方法(与原始 libGDX 版本相同的更新方法,但我将有关旋转矩阵的部分添加到顶部):

    public void update(boolean updateFrustum) {
        float[] matrix = new float[16];
        rotation.toMatrix(matrix);
        Matrix4 m = new Matrix4();
        m.set(matrix);

        camera.view.mul(m);
        //camera.direction.mul(m).nor();
        //camera.up.mul(m).nor();

        float aspect = camera.viewportWidth / camera.viewportHeight;
        camera.projection.setToProjection(Math.abs(camera.near), Math.abs(camera.far), camera.fieldOfView, aspect);
        camera.view.setToLookAt(camera.position, tempVector.set(camera.position).add(camera.direction), camera.up);
        camera.combined.set(camera.projection);
        Matrix4.mul(camera.combined.val, camera.view.val);

        if (updateFrustum) {
            camera.invProjectionView.set(camera.combined);
            Matrix4.inv(camera.invProjectionView.val);
            camera.frustum.update(camera.invProjectionView);
        }
    }

最佳答案

我必须评论关于您的代码的两件事:

  1. 在调用 setToLookAt 之前不能修改 View 矩阵, 这是没有用的,因为该方法重新计算孔矩阵。
  2. update() 的前几行非常浪费。避免 创建不必要的对象(特别是如果您计划更新您的 每个周期都有相机)。要将旋转应用于矩阵,您可以 调用 [matrix].rotate(quaternion)。

最后,有很多方法可以旋转相机,所以我无法真正解决你的问题,但如果你想看到旋转的东西,只需在camera.view.setToLookat之后调用camera.view.rotate(rotation)即可。也许你应该旋转其他东西(例如方向 vector 、向上 vector 等),但你可以从这里开始:

//protected float[] matrix = new float[16];//unused!

public void update(boolean updateFrustum) {

        float aspect = camera.viewportWidth / camera.viewportHeight;
        camera.projection.setToProjection(Math.abs(camera.near), Math.abs(camera.far), camera.fieldOfView, aspect);
        camera.view.setToLookAt(camera.position, tempVector.set(camera.position).add(camera.direction), camera.up);

        camera.view.rotate(q); // THIS IS THE ONLY REAL CHANGE TO YOUR CODE!

        camera.combined.set(camera.projection);
        Matrix4.mul(camera.combined.val, camera.view.val);

        if (updateFrustum) {
            camera.invProjectionView.set(camera.combined);
            Matrix4.inv(camera.invProjectionView.val);
            camera.frustum.update(camera.invProjectionView);
        }
    }

祝你编码愉快!

关于java - 在 libGDX 中将四元数应用于相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13896013/

相关文章:

java - 如何查找本地IP地址

java - 将唯一的随机数放入数组中

android - SQLite Android 数据库光标窗口分配 2048 kb 失败

opengl-es - LibGDX 恢复功能

iphone - 学习 OpenGL ES 1.x

c++ - 如何在 IOS 应用程序 (OpenGL) 中放置 href 链接

java - 如何在 Grizzly http 服务器中为 Jersey 设置模板基本路径?

java - 是否有像 java.awt.Polygon 这样的代号类?

android - 新谷歌地图 v2 的奇怪异常

android - 应用程序在后台时如何处理推送 firebase 通知单击事件?