Search Unity

Unity's Euler Angle sequence

Discussion in 'Scripting' started by GoesTo11, Oct 16, 2019.

  1. GoesTo11

    GoesTo11

    Joined:
    Jul 22, 2014
    Posts:
    604
    I've been trying to figure out what which Euler angle sequence Unity is using to represent angles. After playing with it I have come up with XYZ" or rotation around X followed by rotation around the global Y axis followed by rotation around the new local Z axis. Can anyone confirm this for me? I find this a little odd since in biomechanics we usually use the local axis for all rotations (e.g. XY'Z")

    Does anyone know of a library that has functions to convert quaternions into different Euler angle sequences?

    Thanks
     
  2. GoesTo11

    GoesTo11

    Joined:
    Jul 22, 2014
    Posts:
    604
    It appears to be ZXY in world space or YX'Z" which makes sense. I still need to find a way to convert to other sequences (e.g.YZ'Y")
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    The documentation tells you the order the rotations are applied when you set the eulerAngles property.

    However, the primary representation of rotations in Unity isn't Euler angles at all, but Quaternions. Quaternions are harder to wrap your head around but they have several mathematical advantages (particularly for interpolation) so they're used pretty much everywhere "behind the scenes". When you set a Transform's eulerAngles property, Unity implicitly converts your Euler angles into a Quaternion.

    Don't try to create or modify a Quaternion by directly setting its x/y/z/w values. Those do not mean what you would think they mean.

    Instead, you can create rotations using the built-in helper functions like AngleAxis, FromToRotation, or LookRotation.

    It's also important to know that you can combine two rotations (represented by Quaternions) by multiplying them together. However, multiplication of Quaternions is not commutative! Suppose you have an object currently at some rotation R, and you want to apply a small spin around the Y axis, dY.

    dY * R gives you a rotation where your object has been spun around the global Y axis
    R * dY gives you a rotation where your object has been spun around its local Y axis

    So if you want to do something like Euler angles but apply the rotations in a different order, you could write something like
    Code (CSharp):
    1. Quaternion myRotation =
    2.     Quaternion.AngleAxis(xDegrees, Vector3.right) *
    3.     Quaternion.AngleAxis(yDegrees, Vector3.up) *
    4.     Quaternion.AngleAxis(zDegrees, Vector3.forward);
    And you can change the order those are applied just by changing the order that they're multiplied together
     
  4. GoesTo11

    GoesTo11

    Joined:
    Jul 22, 2014
    Posts:
    604
    Thanks for the reply. I've been working hard at understanding quaternions and have that mostly down. I just need a way to go from quaternions to specific euler angle sequences. I was hoping there was an easy way to convert them without going deep into the math again. I guess if worse comes to worse I could convert the quaternions to rotation matrices and then dig out my old Matlab code to convert into Euler angles.
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Oh, you need to convert from a Quaternion to Euler angles, but in a different order than Unity provides by default?

    I don't have the faintest idea how to do that, sorry. Presumably there's some math formula that Unity is already using to calculate Euler angles and it can probably be modified in some trivial way to put the axes in a different order, but I've got no idea what that formula is.