2

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;
  • Is it possible that this is just a difference in terms of clockwise/anti-clockwise? (I remember being surprised at school that a lot of trigonometry seems to be based on "angles anti-clockwise from the x axis".) – Jon Skeet Jan 18 '23 at 13:19
  • Normally, angles are defined as positive when going counter-clockwise, in accordance with the right-hand rule. This is consistent with `Math.Sin` expected value since `Math.Sin(Math.PI / 2)` is indeed 1. – Gianluca Bardaro Jan 18 '23 at 13:29
  • Yup, that's what I'd expect as well - I just wonder whether System.Numerics has taken a different approach. – Jon Skeet Jan 18 '23 at 14:22

1 Answers1

0

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.

JonasH
  • 28,608
  • 2
  • 10
  • 23
  • The matrix doesn't seem transposed, only element (1,3) is swapped with element (3,1). Everything else is correct. When doing matrix multiplication the result using System.Numerics is the same as Matlab. The discrepancy seems to be only here in the creation of the rotation matrix. – Gianluca Bardaro Jan 18 '23 at 13:33
  • @GianlucaBardaro I'm not sure what you mean, your example matrix sure looks like transpositions of each other to me. In the System.Numerics case your example matrix should have an x-axis of (0,0,-1), is this inconsistent with matlab? – JonasH Jan 18 '23 at 13:54
  • Sorry, you are right, it is the transpose, but why? Then if I try to multiply this transpose matrix with another 4x4 matrix representing a rototranslation, I don't get the expected result. – Gianluca Bardaro Jan 18 '23 at 14:11
  • @GianlucaBardaro You might want to take a look at [this answer](https://stackoverflow.com/a/41940286/12342238), It looks like System.Numerics does some things differently. I try to just ignore the internal details, and just ensure that the results are correct. Even if another convention is followed, it should at least be consistent. – JonasH Jan 18 '23 at 14:53