Converting Matrix3X3 to Quaternion and back

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
PoweredBySauce
Posts: 2
Joined: Sat May 26, 2012 5:42 pm

Converting Matrix3X3 to Quaternion and back

Post by PoweredBySauce »

Hi

I'm getting unexpected results when converting a Matrix3X3 to a Quaternion and back. The code below shows that the converted matrix ends up completely different, and that the quaternions returned by Matrix.Decompose() and Matrix3X3.CreateQuaternion() have their vector components pointing in opposite directions.

I'm not directly using these classes myself, but this is causing problems when setting Entity.OrientationMatrix (where it is converted to a Quaternion and back). Setting WorldTransform instead works as expected, but I'm just wondering what I'm misunderstanding.

Cheers


Vector3 forward = new Vector3(0.7071068f, -0.7071068f, 0);
Vector3 up = new Vector3(0.2141865f, 0.2141865f, -0.9530206f);
forward.Normalize();
up.Normalize();

Matrix matrix = Matrix.CreateWorld(Vector3.Zero, forward, up);
Vector3 decomposedScale;
Quaternion decomposedRotation;
Vector3 decomposedTranslation;
matrix.Decompose(out decomposedScale, out decomposedRotation, out decomposedTranslation);
Matrix3X3 originalMatrix3X3 = Matrix3X3.CreateFromMatrix(matrix);
Quaternion quaternion;
Matrix3X3.CreateQuaternion(ref originalMatrix3X3, out quaternion);
Matrix3X3 convertedMatrix3X3;
Matrix3X3.CreateFromQuaternion(ref quaternion, out convertedMatrix3X3);

Debug.WriteLine("forward = " + forward);
Debug.WriteLine("up = " + up);
Debug.WriteLine("angle between forward and up = " + MathHelper.ToDegrees((float)Math.Acos(Vector3.Dot(forward, up))));
Debug.WriteLine("matrix = " + matrix);
Debug.WriteLine("decomposedScale = " + decomposedScale);
Debug.WriteLine("decomposedRotation = " + decomposedRotation);
Debug.WriteLine("decomposedTranslation = " + decomposedTranslation);
Debug.WriteLine("originalMatrix3X3 = " + originalMatrix3X3);
Debug.WriteLine("quaternion = " + quaternion);
Debug.WriteLine("convertedMatrix3X3 = " + convertedMatrix3X3);


forward = {X:0.7071068 Y:-0.7071068 Z:0}
up = {X:0.2141865 Y:0.2141865 Z:-0.9530207}
angle between forward and up = 90
matrix = { {M11:0.6738874 M12:0.6738874 M13:0.3029055 M14:0} {M21:0.2141865 M22:0.2141865 M23:-0.9530208 M24:0} {M31:-0.7071068 M32:0.7071068 M33:0 M34:0} {M41:0 M42:0 M43:0 M44:1} }
decomposedScale = {X:1 Y:1 Z:1}
decomposedRotation = {X:-0.6040904 Y:-0.3675252 Z:0.1672768 W:0.687036}
decomposedTranslation = {X:0 Y:0 Z:0}
originalMatrix3X3 = {0.6738874, 0.6738874, 0.3029055} {0.2141865, 0.2141865, -0.9530208} {-0.7071068, 0.7071068, 0}
quaternion = {X:0.6040905 Y:0.3675252 Z:-0.1672768 W:0.687036}
convertedMatrix3X3 = {0.6738873, 0.2141866, -0.7071068} {0.6738874, 0.2141864, 0.7071069} {0.3029055, -0.9530208, -1.490116E-07}
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Converting Matrix3X3 to Quaternion and back

Post by Norbo »

The Matrix3X3.CreateQuaternion method was grabbing some transposed values; the development version should fix the problem. Thanks for the report!
PoweredBySauce
Posts: 2
Joined: Sat May 26, 2012 5:42 pm

Re: Converting Matrix3X3 to Quaternion and back

Post by PoweredBySauce »

Cool, thanks for fixing that so quickly! Keep up the good work - I've only been using BEPU a short while but am really impressed with it.
Post Reply