Unexpected sign when building rotation matrix in System.Numerics

129 views Asked by At

I am using System.Numerics and I am trying to build a rotation matrix using methods such as CreateFromAxisAngle(Vector3, Single) and CreateRotationY(Single).

Let's say I want to build a rotation matrix that represents a rotation of 90° aroud the y axis. The output I would expect is:

 0 0 1 0
 0 1 0 0
-1 0 0 0
 0 0 0 1

This is, for example, the output generated by Matlab, and the result suggested by the formula on Wikipedia.

However, when I execute Matrix4x4.CreateRotationY((float)(Math.PI / 2)) or, alternatively, Matrix4x4.CreateFromAxisAngle(new Vector3(0, 1, 0), (float)(Math.PI / 2)) the output I get is:

 0 0 -1 0
 0 1  0 0
 1 0  0 0
 0 0  0 1

I can easily implement my own function to creare the rotation matrix, but I am trying to understand if System.Numerics uses a different standard or if I am missing something.

Thanks

I can add here the implementation of CreateRotationY(Single), where it is clear they use -sine in position 1,3 of the matrix, where it should be sine.

      float num1 = (float) Math.Cos((double) radians);
      float num2 = (float) Math.Sin((double) radians);
      Matrix4x4 rotationY;
      rotationY.M11 = num1;
      rotationY.M12 = 0.0f;
      rotationY.M13 = -num2;
      rotationY.M14 = 0.0f;
      rotationY.M21 = 0.0f;
      rotationY.M22 = 1f;
      rotationY.M23 = 0.0f;
      rotationY.M24 = 0.0f;
      rotationY.M31 = num2;
      rotationY.M32 = 0.0f;
      rotationY.M33 = num1;
      rotationY.M34 = 0.0f;
      rotationY.M41 = 0.0f;
      rotationY.M42 = 0.0f;
      rotationY.M43 = 0.0f;
      rotationY.M44 = 1f;
1

There are 1 answers

4
JonasH On

3D coordinate systems can be left or right handed, however, as far as I can tell, both System.Numerics and Mathlab uses right handedness. In contrast to DirectX that is left handed. Note that handedness is only relevant for some types of functions, like creating rotation matrices.

I would suspect that the rotation matrices are simply transposed. This is another convention with how matrices are ordered and multiplied. You should be able to test this by multiplying the matrices with some arbitrary vector. This should give the the same result in Matlab and system.Numerics if this is the issue.