lwjgl(OpenGL)错误的相机旋转

时间:2013-09-28 22:24:10

标签: java opengl lwjgl trigonometry

My Camera类有以下方法:

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和鼠标旋转。

I apologize for the crummy paint job here.

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

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

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

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);
    }

0 个答案:

没有答案