[SOLVED] Reverse CreateFromYawPitchRoll() (or how to get the vector that would produce the matrix given only the matrix)

Given a matrix, I want to get a 2d vector that I can later use like this:

Matrix.CreateFromYawPitchRoll(_currRotation.X, _currRotation.Y, 0f))

And will get the same original matrix I had (ignore scale and position for now).
Anyone knows how do I get that 2d vector?


You want the inverse rotation given 2 axis right?

I think he wants to calculate the currRotation parameters for a given matrix, which then reproduce the matrix when used as parameters in the Matrix.CreateFromYawPitchRoll method.

Given “originalMatrix” from unknown source, I want this:

Vector2 rotation = <some_magic>(originalMatrix);
Matrix copyMatrix = Matrix.CreateFromYawPitchRoll(rotation.X, rotation.Y, 0f))
copyMatrix == originalMatrix; // true!

Solved it:

    /// <summary>
    /// Extract yaw, pitch and roll from existing matrix.
    /// </summary>
    /// <param name="matrix">Matrix to extract from.</param>
    /// <param name="yaw">Out yaw value.</param>
    /// <param name="pitch">Out pitch value.</param>
    /// <param name="roll">Out roll value.</param>
    public void ExtractYawPitchRoll(Matrix matrix, out float yaw, out float pitch, out float roll)
        yaw = (float)System.Math.Atan2(matrix.M13, matrix.M33);
        pitch = (float)System.Math.Asin(-matrix.M23);
        roll = (float)System.Math.Atan2(matrix.M21, matrix.M22);

Based on the code from here: https://github.com/cureos/aforge/blob/master/Sources/Math/Matrix4x4.cs#L239

1 Like