public static void Invert(ref Matrix4 matrix, out Matrix4 result) { float determinant = 1 / matrix.Determinant(); result.X.X = ((matrix.Y.Y * matrix.Z.Z * matrix.W.W) + (matrix.Y.Z * matrix.Z.W * matrix.W.Y) + (matrix.Y.W * matrix.Z.Y * matrix.W.Z) - (matrix.Y.Y * matrix.Z.W * matrix.W.Z) - (matrix.Y.Z * matrix.Z.Y * matrix.W.W) - (matrix.Y.W * matrix.Z.Z * matrix.W.Y)) * determinant; result.X.Y = ((matrix.X.Y * matrix.Z.W * matrix.W.Z) + (matrix.X.Z * matrix.Z.Y * matrix.W.W) + (matrix.X.W * matrix.Z.Z * matrix.W.Y) - (matrix.X.Y * matrix.Z.Z * matrix.W.W) - (matrix.X.Z * matrix.Z.W * matrix.W.Y) - (matrix.X.W * matrix.Z.Y * matrix.W.Z)) * determinant; result.X.Z = ((matrix.X.Y * matrix.Y.Z * matrix.W.W) + (matrix.X.Z * matrix.Y.W * matrix.W.Y) + (matrix.X.W * matrix.Y.Y * matrix.W.Z) - (matrix.X.Y * matrix.Y.W * matrix.W.Z) - (matrix.X.Z * matrix.Y.Y * matrix.W.W) - (matrix.X.W * matrix.Y.Z * matrix.W.Y)) * determinant; result.X.W = ((matrix.X.Y * matrix.Y.W * matrix.Z.Z) + (matrix.X.Z * matrix.Y.Y * matrix.Z.W) + (matrix.X.W * matrix.Y.Z * matrix.Z.Y) - (matrix.X.Y * matrix.Y.Z * matrix.Z.W) - (matrix.X.Z * matrix.Y.W * matrix.Z.Y) - (matrix.X.W * matrix.Y.Y * matrix.Z.Z)) * determinant; result.Y.X = ((matrix.Y.X * matrix.Z.W * matrix.W.Z) + (matrix.Y.Z * matrix.Z.X * matrix.W.W) + (matrix.Y.W * matrix.Z.Z * matrix.W.X) - (matrix.Y.X * matrix.Z.Z * matrix.W.W) - (matrix.Y.Z * matrix.Z.W * matrix.W.X) - (matrix.Y.W * matrix.Z.X * matrix.W.Z)) * determinant; result.Y.Y = ((matrix.X.X * matrix.Z.Z * matrix.W.W) + (matrix.X.Z * matrix.Z.W * matrix.W.X) + (matrix.X.W * matrix.Z.X * matrix.W.Z) - (matrix.X.X * matrix.Z.W * matrix.W.Z) - (matrix.X.Z * matrix.Z.X * matrix.W.W) - (matrix.X.W * matrix.Z.Z * matrix.W.X)) * determinant; result.Y.Z = ((matrix.X.X * matrix.Y.W * matrix.W.Z) + (matrix.X.Z * matrix.Y.X * matrix.W.W) + (matrix.X.W * matrix.Y.Z * matrix.W.X) - (matrix.X.X * matrix.Y.Z * matrix.W.W) - (matrix.X.Z * matrix.Y.W * matrix.W.X) - (matrix.X.W * matrix.Y.X * matrix.W.Z)) * determinant; result.Y.W = ((matrix.X.X * matrix.Y.Z * matrix.Z.W) + (matrix.X.Z * matrix.Y.W * matrix.Z.X) + (matrix.X.W * matrix.Y.X * matrix.Z.Z) - (matrix.X.X * matrix.Y.W * matrix.Z.Z) - (matrix.X.Z * matrix.Y.X * matrix.Z.W) - (matrix.X.W * matrix.Y.Z * matrix.Z.X)) * determinant; result.Z.X = ((matrix.Y.X * matrix.Z.Y * matrix.W.W) + (matrix.Y.Y * matrix.Z.W * matrix.W.X) + (matrix.Y.W * matrix.Z.X * matrix.W.Y) - (matrix.Y.X * matrix.Z.W * matrix.W.Y) - (matrix.Y.Y * matrix.Z.X * matrix.W.W) - (matrix.Y.W * matrix.Z.Y * matrix.W.X)) * determinant; result.Z.Y = ((matrix.X.X * matrix.Z.W * matrix.W.Y) + (matrix.X.Y * matrix.Z.X * matrix.W.W) + (matrix.X.W * matrix.Z.Y * matrix.W.X) - (matrix.X.X * matrix.Z.Y * matrix.W.W) - (matrix.X.Y * matrix.Z.W * matrix.W.X) - (matrix.X.W * matrix.Z.X * matrix.W.Y)) * determinant; result.Z.Z = ((matrix.X.X * matrix.Y.Y * matrix.W.W) + (matrix.X.Y * matrix.Y.W * matrix.W.X) + (matrix.X.W * matrix.Y.X * matrix.W.Y) - (matrix.X.X * matrix.Y.W * matrix.W.Y) - (matrix.X.Y * matrix.Y.X * matrix.W.W) - (matrix.X.W * matrix.Y.Y * matrix.W.X)) * determinant; result.Z.W = ((matrix.X.X * matrix.Y.W * matrix.Z.Y) + (matrix.X.Y * matrix.Y.X * matrix.Z.W) + (matrix.X.W * matrix.Y.Y * matrix.Z.X) - (matrix.X.X * matrix.Y.Y * matrix.Z.W) - (matrix.X.Y * matrix.Y.W * matrix.Z.X) - (matrix.X.W * matrix.Y.X * matrix.Z.Y)) * determinant; result.W.X = ((matrix.Y.X * matrix.Z.Z * matrix.W.Y) + (matrix.Y.Y * matrix.Z.X * matrix.W.Z) + (matrix.Y.Z * matrix.Z.Y * matrix.W.X) - (matrix.Y.X * matrix.Z.Y * matrix.W.Z) - (matrix.Y.Y * matrix.Z.Z * matrix.W.X) - (matrix.Y.Z * matrix.Z.X * matrix.W.Y)) * determinant; result.W.Y = ((matrix.X.X * matrix.Z.Y * matrix.W.Z) + (matrix.X.Y * matrix.Z.Z * matrix.W.X) + (matrix.X.Z * matrix.Z.X * matrix.W.Y) - (matrix.X.X * matrix.Z.Z * matrix.W.Y) - (matrix.X.Y * matrix.Z.X * matrix.W.Z) - (matrix.X.Z * matrix.Z.Y * matrix.W.X)) * determinant; result.W.Z = ((matrix.X.X * matrix.Y.Z * matrix.W.Y) + (matrix.X.Y * matrix.Y.X * matrix.W.Z) + (matrix.X.Z * matrix.Y.Y * matrix.W.X) - (matrix.X.X * matrix.Y.Y * matrix.W.Z) - (matrix.X.Y * matrix.Y.Z * matrix.W.X) - (matrix.X.Z * matrix.Y.X * matrix.W.Y)) * determinant; result.W.W = ((matrix.X.X * matrix.Y.Y * matrix.Z.Z) + (matrix.X.Y * matrix.Y.Z * matrix.Z.X) + (matrix.X.Z * matrix.Y.X * matrix.Z.Y) - (matrix.X.X * matrix.Y.Z * matrix.Z.Y) - (matrix.X.Y * matrix.Y.X * matrix.Z.Z) - (matrix.X.Z * matrix.Y.Y * matrix.Z.X)) * determinant; Transpose(result, out result); }
public static void Invert(ref Matrix4 matrix, out Matrix4 result) { float determinant = 1 / matrix.Determinant(); result.X.X = ((matrix.Y.Y * matrix.Z.Z * matrix.W.W) + (matrix.Y.Z * matrix.Z.W * matrix.W.Y) + (matrix.Y.W * matrix.Z.Y * matrix.W.Z) - (matrix.Y.Y * matrix.Z.W * matrix.W.Z) - (matrix.Y.Z * matrix.Z.Y * matrix.W.W) - (matrix.Y.W * matrix.Z.Z * matrix.W.Y)) * determinant; result.X.Y = ((matrix.X.Y * matrix.Z.W * matrix.W.Z) + (matrix.X.Z * matrix.Z.Y * matrix.W.W) + (matrix.X.W * matrix.Z.Z * matrix.W.Y) - (matrix.X.Y * matrix.Z.Z * matrix.W.W) - (matrix.X.Z * matrix.Z.W * matrix.W.Y) - (matrix.X.W * matrix.Z.Y * matrix.W.Z)) * determinant; result.X.Z = ((matrix.X.Y * matrix.Y.Z * matrix.W.W) + (matrix.X.Z * matrix.Y.W * matrix.W.Y) + (matrix.X.W * matrix.Y.Y * matrix.W.Z) - (matrix.X.Y * matrix.Y.W * matrix.W.Z) - (matrix.X.Z * matrix.Y.Y * matrix.W.W) - (matrix.X.W * matrix.Y.Z * matrix.W.Y)) * determinant; result.X.W = ((matrix.X.Y * matrix.Y.W * matrix.Z.Z) + (matrix.X.Z * matrix.Y.Y * matrix.Z.W) + (matrix.X.W * matrix.Y.Z * matrix.Z.Y) - (matrix.X.Y * matrix.Y.Z * matrix.Z.W) - (matrix.X.Z * matrix.Y.W * matrix.Z.Y) - (matrix.X.W * matrix.Y.Y * matrix.Z.Z)) * determinant; result.Y.X = ((matrix.Y.X * matrix.Z.W * matrix.W.Z) + (matrix.Y.Z * matrix.Z.X * matrix.W.W) + (matrix.Y.W * matrix.Z.Z * matrix.W.X) - (matrix.Y.X * matrix.Z.Z * matrix.W.W) - (matrix.Y.Z * matrix.Z.W * matrix.W.X) - (matrix.Y.W * matrix.Z.X * matrix.W.Z)) * determinant; result.Y.Y = ((matrix.X.X * matrix.Z.Z * matrix.W.W) + (matrix.X.Z * matrix.Z.W * matrix.W.X) + (matrix.X.W * matrix.Z.X * matrix.W.Z) - (matrix.X.X * matrix.Z.W * matrix.W.Z) - (matrix.X.Z * matrix.Z.X * matrix.W.W) - (matrix.X.W * matrix.Z.Z * matrix.W.X)) * determinant; result.Y.Z = ((matrix.X.X * matrix.Y.W * matrix.W.Z) + (matrix.X.Z * matrix.Y.X * matrix.W.W) + (matrix.X.W * matrix.Y.Z * matrix.W.X) - (matrix.X.X * matrix.Y.Z * matrix.W.W) - (matrix.X.Z * matrix.Y.W * matrix.W.X) - (matrix.X.W * matrix.Y.X * matrix.W.Z)) * determinant; result.Y.W = ((matrix.X.X * matrix.Y.Z * matrix.Z.W) + (matrix.X.Z * matrix.Y.W * matrix.Z.X) + (matrix.X.W * matrix.Y.X * matrix.Z.Z) - (matrix.X.X * matrix.Y.W * matrix.Z.Z) - (matrix.X.Z * matrix.Y.X * matrix.Z.W) - (matrix.X.W * matrix.Y.Z * matrix.Z.X)) * determinant; result.Z.X = ((matrix.Y.X * matrix.Z.Y * matrix.W.W) + (matrix.Y.Y * matrix.Z.W * matrix.W.X) + (matrix.Y.W * matrix.Z.X * matrix.W.Y) - (matrix.Y.X * matrix.Z.W * matrix.W.Y) - (matrix.Y.Y * matrix.Z.X * matrix.W.W) - (matrix.Y.W * matrix.Z.Y * matrix.W.X)) * determinant; result.Z.Y = ((matrix.X.X * matrix.Z.W * matrix.W.Y) + (matrix.X.Y * matrix.Z.X * matrix.W.W) + (matrix.X.W * matrix.Z.Y * matrix.W.X) - (matrix.X.X * matrix.Z.Y * matrix.W.W) - (matrix.X.Y * matrix.Z.W * matrix.W.X) - (matrix.X.W * matrix.Z.X * matrix.W.Y)) * determinant; result.Z.Z = ((matrix.X.X * matrix.Y.Y * matrix.W.W) + (matrix.X.Y * matrix.Y.W * matrix.W.X) + (matrix.X.W * matrix.Y.X * matrix.W.Y) - (matrix.X.X * matrix.Y.W * matrix.W.Y) - (matrix.X.Y * matrix.Y.X * matrix.W.W) - (matrix.X.W * matrix.Y.Y * matrix.W.X)) * determinant; result.Z.W = ((matrix.X.X * matrix.Y.W * matrix.Z.Y) + (matrix.X.Y * matrix.Y.X * matrix.Z.W) + (matrix.X.W * matrix.Y.Y * matrix.Z.X) - (matrix.X.X * matrix.Y.Y * matrix.Z.W) - (matrix.X.Y * matrix.Y.W * matrix.Z.X) - (matrix.X.W * matrix.Y.X * matrix.Z.Y)) * determinant; result.W.X = ((matrix.Y.X * matrix.Z.Z * matrix.W.Y) + (matrix.Y.Y * matrix.Z.X * matrix.W.Z) + (matrix.Y.Z * matrix.Z.Y * matrix.W.X) - (matrix.Y.X * matrix.Z.Y * matrix.W.Z) - (matrix.Y.Y * matrix.Z.Z * matrix.W.X) - (matrix.Y.Z * matrix.Z.X * matrix.W.Y)) * determinant; result.W.Y = ((matrix.X.X * matrix.Z.Y * matrix.W.Z) + (matrix.X.Y * matrix.Z.Z * matrix.W.X) + (matrix.X.Z * matrix.Z.X * matrix.W.Y) - (matrix.X.X * matrix.Z.Z * matrix.W.Y) - (matrix.X.Y * matrix.Z.X * matrix.W.Z) - (matrix.X.Z * matrix.Z.Y * matrix.W.X)) * determinant; result.W.Z = ((matrix.X.X * matrix.Y.Z * matrix.W.Y) + (matrix.X.Y * matrix.Y.X * matrix.W.Z) + (matrix.X.Z * matrix.Y.Y * matrix.W.X) - (matrix.X.X * matrix.Y.Y * matrix.W.Z) - (matrix.X.Y * matrix.Y.Z * matrix.W.X) - (matrix.X.Z * matrix.Y.X * matrix.W.Y)) * determinant; result.W.W = ((matrix.X.X * matrix.Y.Y * matrix.Z.Z) + (matrix.X.Y * matrix.Y.Z * matrix.Z.X) + (matrix.X.Z * matrix.Y.X * matrix.Z.Y) - (matrix.X.X * matrix.Y.Z * matrix.Z.Y) - (matrix.X.Y * matrix.Y.X * matrix.Z.Z) - (matrix.X.Z * matrix.Y.Y * matrix.Z.X)) * determinant; Transpose(result, out result); }