Search Unity

Trivia Q: the "w" in a Quaternion?

Discussion in 'General Discussion' started by Morgan, Jun 15, 2006.

  1. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Quaternions work fine whether I grasp them or not, but I'm just curious: what is the "w"?

    Quaternion
    Quaternion (float x, float y, float z, float w)


    (I looked up quaternion at Wikipedia and not only did I fail to learn anything as a result, I actually forgot several useful facts I used to know.)
     
  2. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    Quaternions are a form of imaginary number where it is assumed that -1 has three square roots (i, j, k) and not one (i) as per the standard complex numbers.

    See:

    http://en.wikipedia.org/wiki/Quaternion

    (Oops you already did.)

    x, y, z, w are basically coefficients:

    Since ijk = -1 and rules such as ij = k (etc.) convert all products of i, j, and k back into one of them, any quaternion can be expressed as:

    q = x + yi + zj + wk

    So the answer is that y, z, and w are all abstract. (i.e. w is just as weird as y and z). x is the "real" part.

    Now if you go back to your high school math you may recall that an ordinary imaginary number happens to act a lot like a vector, so you can think of x + yi as being an orientation about a single axis, plus a magnitude.

    If the magnitude of the vector is 1, then you can think of it as simply representing a direction. E.g. (0,1) is straight up the y axis.

    Quaternions do for orientation about three axes what complex numbers to for orientation about one axis. (And, again, your quaternions will be magnitude 1, or "normalized").
     
  3. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Great--I still don't know what w is, and now I don't know what y and z are either. Anyone care to take x away from me? :)

    Actually, your summary is all the answer I was hoping for--thanks!

    Imaginary rotations are probably perfect, since I have an imaginary game.
     
    john10mwangi likes this.
  4. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Rofl... very nice...

    -Jeremy
     
  5. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    I've read a bit math info found through google and wikipedia about quaternions. (At least I hope I understood it correctly. Please whip me hard and long if I prove to be wrong :p)

    The parts have this meaning:
    x: is the cosine of the amount of rotation.
    (y ,z ,w): is the axis of rotaion.

    Note that the quaternion must be normalized, ie. the magnitude of the quaternion must be 1. ( x^2+y^2+z^2+w^2=1 )

    So in order to answer the original question, the w of a Quaternion corresponds with the z part of the axis of rotation. (as does z to y and y to x). Confusing? Yes.
     
    john10mwangi likes this.
  6. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Interesting--that made even more sense!
     
  7. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Actually, three components (usually xyz) are axis * sin(angle/2) and the fourth (usually w) is cos(angle/2). Which ones are three and which is the fourth depends on the notation.

    In general it's best not to try to understand individual quaternion components. Just treat is as magic that represents rotation :)
     
  8. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Okay... I'm in for a spanking for being wring then, right? :p
     
  9. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    Cool would be if i wouldn't have to care about quaterions at all, no matter if i understand them or not.
     
  10. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    That's exactly the point.

    I've done some weird matrices, rotations, etc in my time. I never understood them.

    David did the gooball handling - if there ever was a rotation-heavy game, that's it. He doesn't get it either, AFAIK.

    Quaternions are just rotations. Think of them as that.
     
  11. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    Hi Nicholas,

    i know what they are but i hardly never had to deal with them directly...

    Ahm by the way you smokestuff looks nice. Brute force textures with different alphas your throwing around? I once saw very nice volumetric clouds in a demo. Had no time to think about how it was done so i just looked at it and enjoyed it...

    At least time to fire a question now: How are volumetric clouds done?


    Regards,

    taumel
     
  12. David-Helgason

    David-Helgason

    Moderator

    Joined:
    Mar 29, 2005
    Posts:
    1,104
    What I learned from GooBall (and am still trying to forget): When you're working with rotations, you need exactly three (3) functions:

    Quaternion.AngleAxis - Creates a rotation which rotates X degrees around an axis.
    Quaternion.FromToRotation - Creates a rotation which rotates from some direction to another. Usually you use this to rotate a transform so that one of its axes, for example the y-axis - follows a target direction in world space.
    Quaternion.LookRotation - Creates a rotation that looks along forward with the the head upwards along some direction.

    The result of all these functions is a Quaternion (read: a rotation). Assign it to transform.rotation and see instant action. Or use Quaternion.Slerp to glide towards them (Slerp is a high-precision version of Lerp for Quaternions).

    It's all in the docs: http://otee.dk/Documentation/ScriptReference/Quaternion.html#AxisAngle


    ... I agree there should be a cookbook, with examples of these far-most important operations. Some day :)


    d.
     
    DevAngelz, pahbsfro and Nkmol1 like this.
  13. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    The USE of Quaternions is well-documented--plus Transform.RotateAround is a favorite of mine :) And all the "slerps" have been real time-savers too. I'm used to writing that stuff by hand.

    Now, understanding the THEORY is irrelevant, but I got curious :) And thanks to everyone, I'm understanding them just enough to satisfy my curiosity.

    I wonder how much progress I could have made on my game instead of pondering that w?

    I will now open the floor to further irrelevant theory discussion--volumetric clouds?

    If that's like the yellow sun example someone posted, then I guess it's just a cluster of a lot of alpha textures.