Page 1 of 9 123 ... LastLast
Results 1 to 10 of 88
Like Tree7Likes

Thread: Short quaternions guide

  1. #1
    Wire Sofaking Fizyk's Avatar
    Join Date
    Jun 2008
    Location
    Łomianki, Poland
    Posts
    737
    Blog Entries
    1

    Default Short quaternions guide

    What are quaternions?

    To explain it well, we need to start at what are complex numbers.
    The regular numbers, that everyone is used to, are the real numbers. But they have a drawback - there are equations that don't have solutions in real numbers, like x^2=-1. There is no real number that squared gives -1. That's why people invented complex numbers. A number that when squared gives -1 is i, called an imaginary unit.

    A complex number is a number like a+bi, where a and b are real. They have many great properties, but this is not the matter of this tutorial. To give you a quick feel how calculations on complex numbers work, here are a few examples:

    Assume we have two complex numbers, z = a+bi and w = c+di (a, b, c, d are real). Then:
    • z+w = a+bi + c+di = (a+c) + (b+d)i
    • z*w = (a+bi)(c+di) = ac + adi + bic + bidi = ac + adi + bci - bd = (ac-bd)+(ad+bc)i
    • z/w = (a+bi)/(c+di) = [(a+bi)(c-di)]/[(c+di)(c-di)] = [(ac+bd)+(bc-ad)i]/[(cc+dd)+(cd-cd)i] = [(ac+bd)+(bc-ad)i]/(c^2+d^2)


    It might be not very readable at the first glance, but if you read it a few times, I'm sure you will see how it works. It's just like with real numbers, except we have i and we have to remember that i^2=-1.

    Ok, so now, quaternions. Quaternions extend the idea of complex numbers to 3 imaginary units. They are called i, j and k. All three work like i in this sense, that i^2 = j^2 = k^2 = -1. But! Beware, quaternions are tricky, because their multiplication isn't commutative! Here is how it works:

    i^2 = j^2 = k^2 = ijk = -1
    ij = -ji = k
    jk = -kj = i
    ki = -ik = j

    I will not look into addition and multiplication of quaternions, because it's not that important and it would take a lot of text to write it all. Besides, it wouldn't be readable at all

    How are they useful?

    The quaternions appeared to be very useful in representing rotations. Those of you, who use applyAngForce know the problem of gimbal lock near 90 degrees pitch. Quaternions are a great way to avoid that.

    Imaginary units i, j and k make a great basis in 3D space. For example, 3i + 5j - 2k is very similar to a [3,5,-2] vector. That's why people sometimes write quaternions as a+v, instead of usual a+bi+cj+dk.

    Now, we can do that the other way round. Get a vector from 3D space and treat it like a quaternion with real part (the "a" number) equal to 0.

    It turns out, that if we take a quaternion q = cos(x/2)+vsin(x/2) (where x is an angle and v is a unit vector) and vector w, then:
    q w (1/q)
    is vector w rotated by angle x about axis v. Confusing? Let's see an example.

    We want to rotate vector [3,5,-2] 90 degrees about X axis. Our angle x is 90 and axis v is [1,0,0] (X axis). The quaternion q is:
    q = cos(45) + [1,0,0]*sin(45) = 0.707 + 0.707*i
    It turns out, that:
    1/q = 0.707 - 0.707*i
    Vector w, as a quaternion, is 3i + 5j - 2k. Now we have to calculate:
    (0.707+0.707*i)(3i+5j-2k)(0.707-0.707*i) = (after many calculations) = 3i + 2j + 5k
    So, the rotated vector is [3,2,5].

    Fortunately, in E2 the chip does all the calculations for you

    One more thing: let's see what happens, if I rotate a vector using 1/q.

    RotatedV = (1/q) V (1/(1/q)) = (1/q) V q
    Now rotate it with q again:
    RotatedV2 = q RotatedV (1/q) = q (1/q) V q (1/q) = 1*V*1 = V

    The point is: rotation by (1/q) is a rotation in the opposite direction than rotation by q, and by the same angle.

    Representing orientation with quaternions

    Here we come to the really useful things.

    An orientation, or an angle as it's called in E2 can be considered a rotation from orientation (0,0,0) about some axis by some angle. As that, it can be represented by a quaternion. For example, quaternion 1 (no rotation) means (0,0,0), so 0 pitch, 0 yaw, 0 roll. Quaternion 1k is rotation by 180 degrees about Z axis, so it's 0 pitch, 180 yaw, 0 roll.

    Now, imagine we have two angles given, like current orientation and target orientation, and we want to know how to rotate between them. Practical example: you have a turret and you want it to chase a target. We have Turret:angle() and a vector, DirectionToTarget. So, we have something like this:
    Code:
    CurrentQuat = quat(Turret:angle()) #or just quat(Turret)
    TargetQuat = quat(DirectionToTarget:toAngle())
    How to calculate a quaternion that will rotate between those two orientations? First, remember that CurrentQuat is rotation from (0,0,0) to current orientation, and TargetQuat is rotation from (0,0,0) to target orientation. So, to rotate from CurrentQuat to TargetQuat, we need to rotate from CurrentQuat to (0,0,0) and from (0,0,0) to TargetQuat!

    Rotation from CurrentQuat to (0,0,0) is 1/CurrentQuat, or inv(CurrentQuat).
    Rotation from (0,0,0) to TargetQuat is... TargetQuat.
    So - rotation from CurrentQuat to TargetQuat is TargetQuat*inv(CurrentQuat), or just TargetQuat/CurrentQuat!

    Code:
    CurrentQuat = quat(Turret:angle()) #or just quat(Turret)
    TargetQuat = quat(DirectionToTarget:toAngle())
    Q = TargetQuat/CurrentQuat
    Now we want to find out about what axis and by what angle we have to rotate. There are 3 convenient functions for that:
    1. rotationAxis(Q) - will return a unit vector, representing the axis.
    2. rotationAngle(Q) - will return a number, that is the angle by which we have to rotate.
    3. rotationVector(Q) - will return a vector, that direction is the axis, and its magnitude is the angle.

    Wait... rotationVector() is perfect for applyTorque! We need to rotate about exactly this axis and we want to rotate stronger when we are further away from target, that is, when the angle is greater! The only drawback is that applyTorque needs an intrinsic vector and rotationVector returns an extrinsic one, but this can easily be fixed:
    Code:
    Torque = Turret:toLocal(rotationVector(Q)+Turret:pos())
    Finally, we will use angVelVector as the delta term and we have:
    Code:
    CurrentQuat = quat(Turret:angle()) #or just quat(Turret)
    TargetQuat = quat(DirectionToTarget:toAngle())
    Q = TargetQuat/CurrentQuat
    Torque = Turret:toLocal(rotationVector(Q)+Turret:pos())
    
    Turret:applyTorque((Torque*N - Turret:angVelVector()*M)*Turret:inertia())
    N and M are some constants that have to be adjusted for best performance.

    Summary
    This of course isn't all you can do with quaternions, but it will probably be the most popular use If you have any questions, post them and I will try to clear things up. I hope this tutorial will be useful for you.

    You can read more on this topic here: http://en.wikipedia.org/wiki/Quatern...atial_rotation


    EDIT:
    What to learn if you can't understand this
    Here is a short list of topics to research, that can help you in understanding quaternions:
    • Complex numbers - quaternions are a generalization of complex numbers, so it's good to start with them
    • Vector spaces, linear transformations - that's what rotations are all about
    • Groups - some basics of group theory (what are groups, examples of groups) can help you understand how quaternions work
    Last edited by Fizyk; 05-29-2010 at 05:29 AM. Reason: Replaced highlight boxes with code boxes, as highlight boxes were broken.
    Hickley and miojox like this.

    My programs: BIOS - Alcyone - Calculator - Notepad - Movie Player
    My tutorials: applyTorque - Quaternions - PID controllers
    Some other things I made: FT Chip - RK4 Solar System

  2. #2
    Wire Sofaking ahref's Avatar
    Join Date
    Jul 2008
    Posts
    522

    Default Re: Short quaternions guide

    i got lost at "A complex number is a number like a+bi"
    A-Z Materials - Released!!!!!!!

  3. #3
    Master of Mars Magos Mechanicus's Avatar
    Join Date
    May 2008
    Posts
    852

    Default Re: Short quaternions guide

    Interesting. From before I'd really only heard of quaternions as forerunners of vectors that caused the standard R^3 basis to be named ijk and so on, but I hadn't really got a handle on how you use the things themselves.
    Based on this, I find the axis angle/direction vector approach a bit more natural to think in terms of, but maybe I'll give it a whirl. I've been going back to orientation stuff a bit now after seeing Liquos' thruster challenge, so some fresh ideas might be good.
    I can wire anything directly into anything! I'm the Professor!
    -Professor Hubert Farnsworth

  4. #4
    Lifetime Supporter Nikita's Avatar
    Join Date
    May 2009
    Posts
    788

    Default Re: Short quaternions guide

    Ohhh... now it makes sense.

    But I already learned how to avoid gimbal lock with applyForce.

    But I'm not sharing so that everyone starts using quaternions and get smarter

  5. #5
    Wire Sofaking Fizyk's Avatar
    Join Date
    Jun 2008
    Location
    Łomianki, Poland
    Posts
    737
    Blog Entries
    1

    Default Re: Short quaternions guide

    Quote Originally Posted by Magos Mechanicus
    From before I'd really only heard of quaternions as forerunners of vectors that caused the standard R^3 basis to be named ijk and so on
    Nice, I didn't know about that. I always heard of quaternions as an extension to complex numbers, only recently I found out they were being connected to the 3D space and rotations.

    Quote Originally Posted by Nikita
    Ohhh... now it makes sense.

    But I already learned how to avoid gimbal lock with applyForce.

    But I'm not sharing so that everyone starts using quaternions and get smarter
    Heh

    My programs: BIOS - Alcyone - Calculator - Notepad - Movie Player
    My tutorials: applyTorque - Quaternions - PID controllers
    Some other things I made: FT Chip - RK4 Solar System

  6. #6
    Lifetime Supporter rich_27's Avatar
    Join Date
    Aug 2007
    Location
    findByUser("rich_27"):pos()
    Posts
    224

    Default Re: Short quaternions guide

    Thankyou very much. That was very helpful. I have been waiting for something like this for quite a while now =]

  7. #7
    Wire Amateur Legendary Ace's Avatar
    Join Date
    Jul 2008
    Location
    Washington
    Posts
    54

    Default Re: Short quaternions guide

    Yes! Finally a tutorial.

    Great job, gave me a better understanding.

    Also, is it possible to efficiently avoid gimble lock with applyAngForce, or no matter what, the angular system fails?

  8. #8
    Wirererer SoundKiller777's Avatar
    Join Date
    Dec 2008
    Location
    United Kingdom
    Posts
    191

    Question Re: Short quaternions guide

    So as much as im inspired and interested as to how this works, I have a personal vendetta against math and its inability to grasp the concept that making things more complicated than they need to be is in fact more elitist than useful.

    Your code creates a super turret something that will hit you even when sprint no-clipping, however so does this code:

    Code:
    Ang = ang(Turret:elevation(Target:pos()+vec(0,0,50)),Turret:bearing(Target:pos()),Turret:angles():roll())
    Turret:applyAngForce( ( (Ang) * -300 - (Turret:angVel()*2))*Turret:mass())
    Is it that you get a thrill from seeing peoples bewildered faces when they attempt to read your code, or is it a real piece of math that does something more than my simple calculations? Although I am slightly tired and therefor not of a solid mind right now please don't mistake my intentions as been to diminish your obvious high skill with math, instead I'm simply trying to asses whether spending the next month or so learning this will be of any use other than been able to sit on a nice comfy pedestal.

    Oh and admin before you ban me for creating a post containing possible minge material I'd have to say that my code is missing a few declarations that no noob could figure out how to do and Lengend's code is more complete than mine which is a full copy paste tutorial as to how to make a instant kill death machine.
    Last edited by SoundKiller777; 09-07-2009 at 11:58 PM.

  9. #9
    Wire Sofaking Fizyk's Avatar
    Join Date
    Jun 2008
    Location
    Łomianki, Poland
    Posts
    737
    Blog Entries
    1

    Default Re: Short quaternions guide

    Thanks for your feedback

    Quote Originally Posted by Legendary Ace
    Also, is it possible to efficiently avoid gimble lock with applyAngForce, or no matter what, the angular system fails?
    Nikita says he found out a way to do it, but I don't know of any.

    Quote Originally Posted by Soundkiller777
    Is it that you get a thrill from seeing peoples bewildered faces when they attempt to read your code, or is it a real piece of math that does something more than my simple calculations?
    I don't know how your code behaves near 90 degrees pitch. From what I see, it can have some problems, as then yaw becomes the same as roll and you are starting to apply two different forces in the same angle (I think it is treated a bit differently in the lua code though). This is the gimbal lock. Quaternions' greatest advantage is that they don't suffer from it - they let you calculate how to go from any orientation to any other, and do it directly.

    Of course probably for most wire contraptions applyAngForce and Euler angles are enough and this code for a turret was just an example, but it is fun to do it in some other way, especially such that is immune to problems with the angle system

    My programs: BIOS - Alcyone - Calculator - Notepad - Movie Player
    My tutorials: applyTorque - Quaternions - PID controllers
    Some other things I made: FT Chip - RK4 Solar System

  10. #10
    No U Divran's Avatar
    Join Date
    Jul 2008
    Location
    Sweden
    Posts
    6,324

    Default Re: Short quaternions guide

    It's shit like this that makes you want to pay attention in math class.
    Ghork likes this.
    Get dropbox and get 250 mb extra space: Dropbox | League of Legends

Page 1 of 9 123 ... LastLast

LinkBacks (?)

  1. 07-13-2010, 07:38 AM

Similar Threads

  1. [REQ] E2 Quaternions Tutorial
    By rich_27 in forum Expression 2 Discussion & Help
    Replies: 3
    Last Post: 08-25-2009, 09:15 AM
  2. 2.5 Years in Short
    By KillerBlonde in forum Off-Topic
    Replies: 9
    Last Post: 07-12-2008, 02:19 AM
  3. Quaternions?
    By Syranide in forum Wiremod General Chat
    Replies: 4
    Last Post: 02-09-2008, 06:09 AM
  4. PLZ Help its an e-z and short Question
    By eligray in forum Installation and Malfunctions Support
    Replies: 1
    Last Post: 08-21-2007, 10:00 AM
  5. Short Range Radio
    By Razara in forum Ideas & Suggestions
    Replies: 6
    Last Post: 04-07-2007, 12:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
unnecessary
unnecessary
unnecessary
unnecessary
linguistic-parrots
linguistic-parrots
linguistic-parrots
linguistic-parrots