Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Quaternion caculation

Discussion in 'Scripting' started by live_xi, Nov 2, 2015.

  1. live_xi

    live_xi

    Joined:
    Oct 21, 2015
    Posts:
    12
    hi folks
    you guys might help me to understand this piece of code in "MouseLook.cs"
    at the begin of the function ClampRotationAroundXAxis i find the following segment concerning Quaternion

    q.x /= q.w;
    q.y /= q.w;
    q.z /= q.w;
    q.w = 1.0f;

    what's the purpose of those lines code?
    thanks in advance
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    how good is your math? quaternions can get complicated. Best to start with "what is a quaternion?":
    https://en.wikipedia.org/wiki/Quaternion

    then you can start the mind melt of "ok, so what is each component?"
    http://forum.unity3d.com/threads/trivia-q-the-w-in-a-quaternion.2039/#post-13733
    http://www.mathsisfun.com/numbers/complex-numbers.html

    *mind melted tries to escape the rabbit hole.... runs from thread*



    edit: I lied, didn't run, tried to find an answer to the question "what is achieved by dividing the imaginary component by the real component in complex numbers?" which is what that code is trying to do, but that comes up with loads of maths explanations and not much programming examples... so I tried a guess "perhaps its normalizing the quaternion"... which yielded this: http://www.cprogramming.com/tutorial/3d/quaternions.html nice explanation of the components from a programmers point of view even if I'm not entirely sure that your sample is doing a normalization, setting w to 1 at the end suggests that but my math learning never got to complex numbers so... *runs*
     
    Last edited: Nov 2, 2015
    LaneFox likes this.
  3. Glurth

    Glurth

    Joined:
    Dec 29, 2014
    Posts:
    109
    I'm not positive, but It looks like this is generating the Axis-of-Rotation for the Quaternion "q". The result would be stored in the q.x,q.y, and q.z variables.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    You're going to need a math class to understand Quaternions. Most people on this forum, even among the expert Unity users, don't understand quaternions. We know how to use the Quaternion class, and we know why Quaternions are used, but that's about it. Unity's Quaternion class is phenomenal for people who don't understand Quaternions.

    If you are actually interested in a deeper understanding of Quaternions, more power to you! However, I suspect that you just want to know how it's accomplishing the particular thing that it does in that function so you can modify and/or re-appropriate it. If that guess is correct, you might have better luck asking us how directly to accomplish what you want to accomplish :)
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Yeah, I've attempted to describe it many times to people. And it just doesn't happen.

    You need to take a course on complex number systems before you can understand it. Once you've done that though... it's not too hard... but that's completely relative to the idea that you've dealt with complex numbers before.


    As for OP's use case... can I see the surrounding code?


    [edit]
    is it the one from the Standard Assets?

    Code (csharp):
    1.  
    2.         Quaternion ClampRotationAroundXAxis(Quaternion q)
    3.         {
    4.             q.x /= q.w;
    5.             q.y /= q.w;
    6.             q.z /= q.w;
    7.             q.w = 1.0f;
    8.  
    9.             float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
    10.  
    11.             angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
    12.  
    13.             q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);
    14.  
    15.             return q;
    16.         }
    17.  
    The method explains exactly what they're doing...
     
    Last edited: Nov 2, 2015
    Glurth likes this.
  7. live_xi

    live_xi

    Joined:
    Oct 21, 2015
    Posts:
    12
    hi guys again,
    i post this thread just wanting to confirm/correct my answer. but seems to get a discuss around it.
    so correct me if i'm wrong. i am just talking about the purpose of those 4 lines.
    1. it seems just scaling (like normalization) with factor q.w
    2. this scaling just makes calculation of angleX convenient
    btw I studied math but never never heard of quaternion. i think it really an issue of graphics
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Well yeah, kind of, though normalizing a quaternion isn't done quite exactly like that.

    But I mean, to explain exactly what's going on is going to require knowing how quaternions work. Since most people here don't know, that's what the conversation was about.

    And those who do know, probably don't want to take the time to define the intricacies of quaternions, especially not knowing what level of math you're capable of. It could take some time.

    So really, how you just described it is pretty much exactly the best answer you'd get.

    No, quaternions are part of math. They are used in graphics a lot yes, but they're math. They were invented in Dublin by Sir William Rowan Hamilton back in the 1840's. Upon realizing them he carved the identity formula of them into the side of the bridge, which is still there to this day.

    i^2 = j^2 = k^2 = ijk = -1

    As for learning them... if you google around you'll find 2 types of lessons about them.

    1) a very simplified lesson usually lasting 10 minutes or so in video form, defining their use case for rotation in 3-space usually in the context of videogames. This requires the quat to always be normalized (which unity does for you), and really just follows the same rules as matrices for rotation in a more compact space. Getting into the magic of the numbers isn't really covered, and you're expected to just accept the quat for what it is.

    2) a full lesson like would be had at university in a complex number theory course, or maybe an advanced physics/engineering course that deals with applied mathematical structures. The lessons could be 45 minutes or more in length. And even then you'll have to practice practice practice to really understand them as they're framed in the context of the larger course... just like most math, you need the previous lessons building up to the current to understand it thoroughly.

    Here's some full lessons:


    Or:
     
    LeftyRighty and Ian094 like this.
  9. Glurth

    Glurth

    Joined:
    Dec 29, 2014
    Posts:
    109
    >>1. it seems just scaling (like normalization) with factor q.w

    While "scaling" is indeed the operation that it's doing on the x,y,z components, it is NOT normalizing the Quaternion. In fact, all Quaternions in Unity, are actually UNIT Quaternions (a "normalized" Quaternion: normalized is in quotes because "a quaternion of length 1.0" does not really make sense). A "full" Quaternion, is required to define any rotations over 360deg. Unity's Quaternion Class actually defines an "Orientation".

    Think I'm going to watch those vids too, thanks LordOfDuct!