I have a struct for a 3x3 rotational matrix of integers that I need to store the data for rotation of objects in a 3D environment to be easily serialized into XML and interpreted by another program
TL;DR, Unsure of how to implement a fix for a quaternion of 270 degrees resulting in 90 instead
public struct IntegerMatrix3x3
{
public int m00;
public int m10;
public int m20;
public int m01;
public int m11;
public int m21;
public int m02;
public int m12;
public int m22;
/*
m00 m01 m02
m10 m11 m12
m20 m21 m22
*/
}
The issue I have come across is a with a function I have made to convert the matrix to a vector of Euler angles I can use to set the game objects transform in the unity environment
public Vector3 ToEulers()
{
// NTS: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
Quaternion q = new Quaternion();
q.w = Mathf.Sqrt(Mathf.Max(0, 1 + m00 + m11 + m22)) / 2;
q.x = Mathf.Sqrt(Mathf.Max(0, 1 + m00 - m11 - m22)) / 2;
q.y = Mathf.Sqrt(Mathf.Max(0, 1 - m00 + m11 - m22)) / 2;
q.z = Mathf.Sqrt(Mathf.Max(0, 1 - m00 - m11 + m22)) / 2;
q.x *= Mathf.Sign(q.x * (m21 - m12));
q.y *= Mathf.Sign(q.y * (m02 - m20));
q.z *= Mathf.Sign(q.z * (m10 - m01));
// Find euler angles from quaternion derived from matrix
Debug.Log(q.eulerAngles);
return q.eulerAngles;
}
When I encode the angles (0, 270, 0) into the matrix, then convert the matrix back to a angles using said function, it outputs (0, 90, 0). I did a bit of reading and discovered that that is a downfall of quaternions/matrices, but I am not sure how to implement a solution/workaround for it.
Thanks, Nifley