【问题标题】:LWJGL - OpenGL - Camera rotationLWJGL - OpenGL - 相机旋转
【发布时间】:2023-03-12 15:36:01
【问题描述】:

我尝试创建第一人称相机。我拿了我 XNA 项目的一段代码,并试图将它转换成 java (LWJGL)。

Matrix4f mx = Matrix4f.rotate(pitch, new Vector3f(1, 0, 0),
            new Matrix4f(), null);

   Matrix4f my = Matrix4f.rotate(yaw, new Vector3f(0, 1, 0),
            new Matrix4f(), null);

    Matrix4f rotationMatrix = Matrix4f.mul(mx, my, null);

    Vector4f tmp = Matrix4f.transform(rotationMatrix, new Vector4f(0, 0,
            -1, 0), null);

    target = new Vector3f(position.x + tmp.x, position.y + tmp.y,
            position.z + tmp.z);

    GLU.gluLookAt(this.position.x, this.position.y, this.position.z,
            this.target.x, this.target.y, this.target.z, 0, 1.0f, 0);

如果我移动鼠标,俯仰和偏航将增加/减少。在开始运行我的游戏时,它运行良好,但经过一些动作后,我无法向下或向上看。如果我的位置

完美运行的 XNA 代码:

         Matrix rotationMatrix = Matrix.CreateRotationX(pitch) * Matrix.CreateRotationY(yaw);

        camera.Target = Vector3.Transform(Vector3.Forward, rotationMatrix) + camera.Position;
        this.viewMatrix = Matrix.CreateLookAt(this.position, this.target, Vector3.Up);

编辑

所以我发现 LWJGL 和 XNA 在旋转矩阵方面有些不同。

Java:

Matrix4f mx = Matrix4f.rotate(1.2345f, new Vector3f(1,0,0),new Matrix4f(),null);

结果:

{1.0 0.0 0.0 0.0} {0.0 0.3299931 -0.9439833 0.0}{0.0 0.9439833 0.3299931 0.0}{0.0 0.0 0.0 1.0}

XNA

Matrix.CreateRotationX(1.2345f)

结果:

{1,0,0,0} {0,0.3299931,0.9439833,0} {0,-0.9439833,0.3299931,0} {0,0,0,1}

区别是 -0.9439833 和 +0.9439833 ...有人能解释一下为什么会有不同的结果吗?不是一样的功能吗?

谢谢!

【问题讨论】:

    标签: opengl matrix camera rotation lwjgl


    【解决方案1】:

    哦,我明白了。好吧,这就是与众不同。最后我解决了我的问题。问题是 LWJGL 中的“转换”方法。那是我最后的工作代码。也许我可以优化它?

        public static Matrix4f createRotationX(float pitch) {
        Matrix4f mX = new Matrix4f();
        mX.m00 = 1;
        mX.m10 = 0;
        mX.m20 = 0;
        mX.m30 = 0;
        mX.m01 = 0;
        mX.m11 = (float) Math.cos(pitch);
        mX.m21 = (float) Math.sin(pitch);
        mX.m02 = 0;
        mX.m03 = 0;
        mX.m12 = (float) -Math.sin(pitch);
        mX.m22 = (float) Math.cos(pitch);
        mX.m23 = 0;
        mX.m03 = 0;
        mX.m13 = 0;
        mX.m23 = 0;
        mX.m33 = 1;
        return mX;
    }
    
    public static Matrix4f createRotationY(float yaw) {
        Matrix4f mY = new Matrix4f();
        mY.m00 = (float) Math.cos(yaw);
        mY.m10 = 0;
        mY.m20 = (float) -Math.sin(yaw);
        mY.m30 = 0;
        mY.m01 = 0;
        mY.m11 = 1;
        mY.m21 = 0;
        mY.m31 = 0;
        mY.m02 = (float) Math.sin(yaw);
        mY.m12 = 0;
        mY.m22 = (float) Math.cos(yaw);
        mY.m23 = 0;
        mY.m03 = 0;
        mY.m13 = 0;
        mY.m23 = 0;
        mY.m33 = 1;
        return mY;
    }
    
    public static Vector3f getNewTarget(Vector3f position,float pitch, float yaw){
    
        Matrix4f mx = MathUtil.createRotationX(pitch);
        Matrix4f my = MathUtil.createRotationY(yaw);      
    
        Matrix4f rotationMatrix = Matrix4f.mul(mx, my, null);
    
        Matrix4f def = new Matrix4f();
        def.m20 = -1;
        def.m00 = 0;
        def.m11 = 0;
        def.m22 = 0;
        def.m33 = 0;
    
        Matrix4f v4 = Matrix4f.mul(def,rotationMatrix,null);
    
        return new Vector3f(v4.m00+position.x, v4.m10+position.y, v4.m20+position.z);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多