1

我的相机类有以下方法:

public void strafeLeft(float speed){
        cameraPosition = Vector3f.add(cameraPosition, 
                new Vector3f(-cameraRight.x * speed, 
                        -cameraRight.y * speed, 
                        -cameraRight.z * speed), 
                        null);
        lookAt(cameraPosition, Vector3f.add(cameraPosition, cameraDirection, null), up);
    }

    public void strafeRight(float speed){
        cameraPosition = Vector3f.add(cameraPosition, 
                new Vector3f(cameraRight.x * speed, 
                        cameraRight.y * speed, 
                        cameraRight.z * speed), 
                        null);
        lookAt(cameraPosition, Vector3f.add(cameraPosition, cameraDirection, null), up);
    }

    public void moveForwards(float speed){
        cameraPosition = Vector3f.add(cameraPosition, 
                new Vector3f(cameraDirection.x * speed, 
                        cameraDirection.y * speed, 
                        cameraDirection.z * speed), 
                        null);
        lookAt(cameraPosition, Vector3f.add(cameraPosition, cameraDirection, null), up);
    }

    public void moveBackwards(float speed){
        cameraPosition = Vector3f.add(cameraPosition, 
                new Vector3f(-cameraDirection.x * speed, 
                        -cameraDirection.y * speed, 
                        -cameraDirection.z * speed), 
                        null);
        lookAt(cameraPosition, Vector3f.add(cameraPosition, cameraDirection, null), up);
    }

    public void rotateCamera(int deltaX, int deltaY){
        cameraVerticalAngle -= deltaY * cameraSensitivity;
        cameraHorizontalAngle += deltaX * cameraSensitivity;

        cameraDirection.x = (float)(Math.cos(cameraVerticalAngle) * Math.sin(cameraHorizontalAngle));
        cameraDirection.y = (float)(Math.sin(cameraVerticalAngle));
        cameraDirection.z = (float)(Math.cos(cameraVerticalAngle) * Math.cos(cameraHorizontalAngle));

        cameraRight.x = (float)(Math.sin(cameraHorizontalAngle - 3.14f/2.0f));
        cameraRight.y = (float)(Math.sin(cameraVerticalAngle));
        cameraRight.z = (float)(Math.cos(cameraHorizontalAngle - 3.14f/2.0f));

        up = Vector3f.cross(cameraRight, cameraDirection, null);

        lookAt(cameraPosition, Vector3f.add(cameraPosition, cameraDirection, null), up);
    }

使用测试客户端,我得到了非常奇怪的行为,这更容易用图像来解释。一个立方体被渲染到屏幕上,并且可以使用 WASD 和鼠标来移动和旋转相机。

我为这里糟糕的油漆工作道歉。

问题是当相机向右转动时,它看起来好像相机位置移动了,或者模型位置移动了!

我不知道为什么会这样。我正在为模型矩阵使用单位矩阵,所以我猜这是我的视图矩阵的问题,但我不知道为什么......rotateCamera 有问题吗?

编辑:这是看看。因为 cameraPosition、cameraDirection 和 up 都是类变量,所以我已经取出了 lookAt 中的参数。

public void lookAt() {
        Vector3f f = normalize(cameraDirection); 
        Vector3f s = normalize(cameraRight); 
        Vector3f u = normalize(up); 

        Matrix4f result = new Matrix4f();

        result.m00 = s.x;
        result.m10 = s.y;
        result.m20 = s.z;
        result.m01 = u.x;
        result.m11 = u.y;
        result.m21 = u.z;
        result.m02 = -f.x;
        result.m12 = -f.y;
        result.m22 = -f.z;


        viewMatrix = Matrix4f.translate(new Vector3f(-cameraPosition.x, -cameraPosition.y, -cameraPosition.z),  result, null);
    }
4

0 回答 0