Search Unity

Vector math vs Quaternions/Angles

Discussion in 'Scripting' started by XJonOneX, Jan 28, 2018.

  1. XJonOneX

    XJonOneX

    Joined:
    Dec 19, 2017
    Posts:
    113
    I'm used to vectors for programing 3d games. Been using them for decades. (on a 20 year old engine...) Now I'm using Unity and there's all these new fangled Quaternion and Euler angle thingies. (I know, they're not new, just go with me ;))

    So anyway, I'm all about optimization from the get-go. It would appear that I can achieve several of the same effects, such as rotating objects to specific vectors/angles over time, either through vectors and their functions, or quaternions and their functions, or heck, even by multiplying quaternions by vectors. (Oh you, Unity)

    So does it all boil down to preference? Is there a situation where I'd use one over the other? Is it faster one way or the other?

    I'd like to hear some thoughts, or read them, rather. :)
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    As far as I know, Unity’s rotations are internally stored as quaternions. If your intending to apply a rotation to a game object, it eventually has to get turned into that. Setting the Euler angles on a transform’s rotation just automatically converts it to a quaternion.

    You can use Euler angles to create quaternions, or work with Euler angles directly (as float values). If all you need is to rotate something around an axis, Euler is simple and straight forward since you can just track and do your math on a single float value. However if you’re doing complex rotations on two or three Euler angles, Euler has a significant failing. Gimbal lock. Euler can be much faster as it’s just adding each angle together, but the resulting rotation won’t necessarily be what you expect.

    You can also use vectors if you want and construct a quaternion from that too.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    They're all just mathematical structures.

    You can perform your math in whatever manner you want using translational vectors, directional vectors, euler angles (stored as a vector...), rotational quaternions, heck there's even a 4x4 Matrix if you really really want to get old school:
    https://docs.unity3d.com/ScriptReference/Matrix4x4.html

    In the end, underneath, Unity stores them a specific way. But you can convert to and from them (you should of course understand any limitations that may arise as a result... like euler angles having many many flaws). Of course that doesn't mean that the function to convert may actually exist... but if it doesn't, you can always write one. Because again... they're all just mathematical structures that are defined by rules independent of Unity.

    https://en.wikipedia.org/wiki/Euclidean_vector
    https://en.wikipedia.org/wiki/Quaternion
    https://en.wikipedia.org/wiki/Matrix_(mathematics)