矩阵 4 旋转不会在 x 和 y 周围工作,但会在 z 周围工作

Matrix4 rotation won't work around x and y but works around z

本文关键字:工作 周围 旋转 矩阵      更新时间:2023-10-16

我正在尝试制作一个立方体以正确绕所有轴旋转。我可以移动立方体,视图是透视的,但是当我尝试旋转它时,旋转仅在z轴上工作。

即使我要考虑到X和Y轴旋转也没有效果。我的旋转方法:

        void Matrix4::rotate(float degrees, Vector3 axis)
    {
        float c = cos(Common::degreesToRadians(degrees));
        float s = sin(Common::degreesToRadians(degrees));
        values[0] = (axis.x * axis.x) * (1.0f - c) + c;
        values[1] = (axis.y * axis.x) * (1.0f - c) + (axis.z * s);
        values[2] = (axis.z * axis.x) * (1.0f - c) - (axis.y * s);
        values[4] = (axis.x * axis.y) * (1.0f - c) - (axis.z * s);
        values[5] = (axis.y * axis.y) * (1.0f - c) + c;
        values[6] = (axis.z * axis.y) * (1.0f - c) + (axis.x * s);
        values[8] = (axis.x * axis.z) * (1.0f - c) + (axis.y * s);
        values[9] = (axis.y * axis.z) * (1.0f - c) - (axis.x * s);
        values[10] = (axis.z * axis.z) * (1.0f - c) + c;
    }

我的CreateTransFormationMatrix函数:

        Matrix4 Matrix4::createTransformationMatrix(Vector3 position, Vector3 rotation, Vector3 scale)
    {
        Matrix4 result;
        result.translate(position);
        result.rotate(rotation.x, Vector3(1, 0, 0));
        result.rotate(rotation.y, Vector3(0, 1, 0));
        result.rotate(rotation.z, Vector3(0, 0, 1));
        result.scale(scale);
        return result;
    }

我感谢任何帮助,谢谢。

没关系,问题是所有3个函数:缩放,旋转,翻译错误的位置,然后弄乱了。我根据lwjgl matrix4f类修复了它们,现在一切正常。