Search Unity

Does Quaternion ToAngleAxis round?

Discussion in 'Editor & General Support' started by Reeley, Jun 17, 2020.

  1. Reeley

    Reeley

    Joined:
    Feb 23, 2017
    Posts:
    45
    Im having a wierd error with getting the rotation around the Y axis.
    Code (CSharp):
    1. void Start()
    2. {
    3.     transform.localRotation = Quaternion.AngleAxis(359.9675f, Vector3.down);
    4.     transform.localRotation.ToAngleAxis(out float angle, out _);
    5.     Debug.Log($"{360f - transform.localRotation.eulerAngles.y:0.000000} | {angle:0.000000} | {360f - transform.localRotation.eulerAngles.y == angle}");
    6. }
    Output: 359,967500 | 360,000000 | False

    Im expecting the output to be the same, but it isnt. I dont know why the rounding occurs here.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    You're discarding the axis part of the ToAngleAxis result. It might not be Vector3.down. It's probably something else. Euler representations are not unique. Try printing the discarded axis too.
     
  3. Reeley

    Reeley

    Joined:
    Feb 23, 2017
    Posts:
    45
    Why wouldn´t it be Vector3.down/up when im only rotating around the Y-Axis. Can you explain this to me or is this some magic Quaternion stuff^^
    Printing the vector results in (NaN, -Infinity, NaN).

    Is there no Quaternion method which takes in a Vector3 and returns the rotation around this vector?
     
  4. cassidycurtis

    cassidycurtis

    Joined:
    Apr 8, 2019
    Posts:
    6
    This (returning NaN and Infinity values) seems to be something that Quaternion.ToAngleAxis does whenever the rotation angle is just small enough to cause a floating point precision problem, but not so small that it gets rounded to zero. I've run into this problem recently as well. (It would be nice to hear from Unity devs whether this behavior is intentional-but-undocumented, or just a bug.)

    But in general, it seems wise not to expect a lot of precision when converting back and forth between Quaternions, Angle/Axis rotations, and Euler angles. There are just too many places in the math where the littlest bits can get lost.