Search code examples
c++matrixrotationangleglm-math

Get the angle from a rotated matrix using glm?


I want to know how to get the angle from a rotated matrix, how do I calculate that? I am using glm c++.

For example how do I get the angle from this matrix using c++?

[-0.01458][-2.26652][0][0]
[-1.27492][-0.02596][0][0]
[    0   ][    0   ][1][0]
[    x   ][    y   ][z][1]

Solution

  • This looks like an identity matrix that was rotated around Z axis. If that's always the case, you can get the angle back by applying glm::atan function on the first two elements of the first column:

    float get_angle_in_rad(const glm::mat4 &matrix) {
        return glm::atan(matrix[0][1], matrix[0][0]);
    }
    

    See Rotation matrix for explanation.

    Note that if the matrix represents more complicated tranformation than just rotation around Z axis, the value returned by this function will be bogus. Depending on your usecase you may want to keep the euler rotation angles separately, in addition to the transformation matrix.