Quaternion Basics

Started by
13 comments, last by alvaro 10 years ago

Hi, all:

I've written a post on quaternion basics.

http://allenchou.net/2014/04/game-math-quaternion-basics/

It covers the basic operations of quaternions and slerp.

I hope some people find this post useful smile.png

Ming-Lun "Allen" Chou

Physics / Graphics / Procedural Animation
http://allenchou.net

Advertisement

They are basically the usual stuff you can found everywhere without any proof/explanation. I have a few observations:

  1. The dot product is not part of quaternion algebra. Since quaternions are 4D vectors you can clearly define it in the usual way, but it is an additional structure.
  2. You have defined the conjugate as an "optimization" for the inverse in the unit quaternion case, but it is very important in quaternion algebra, They are for example used to define the norm of a quaternion |q| = sqrt( q * conjugate(q)) and to define multiplicative inverses q^-1 = conjugate(q)/(q*conjugate(q)). Indeed we have that q*conjugate(q) is the real number |q|^2 and thus q*(conjugate(q)/(q*conjugate(q)) = 1.

EDIT: Removed the first point after Alvaro's comment.

I have always wondered wether there is a relation between quaternions and rotating phasors that can be used to make a more intuitive way of seeing how quaternions work.

A complex number: z = x + jy can basically be seen as a vector with a rotation and length:

z = r cos ? + j (r sin ?) = r e^(j?)

Where:

r = sqrt(x^2 + y^2) - The length

and:

? = arctan(y/x) - The angle

You now have:

r e^(j?)

Now you can just multiply with another vector to get rotation:

r1 e^(j?1) * r2 e^(j?2) = r1 r2 e^(j(?1 + ?2))

Above, you will also change the length of the vector when you multiply, so we will just "remove" the r2 to multiply with unit length.
r1 e^(j(?1 + ?2))

Now you see that rotation really is the matter of finding the original length and angle, then just add the amount of radians you want to rotate. Since this is the full rotation in one go, you need to add a time variable in there too.

In my DSP book, they define a complex exponential signal like this:
z(t) = A e^(j(w0 t + ?)) - It is basically the same formula as above

Where A is the amplitude, w0 is the frequency in radians, t is time and theta is the initial phase.

In my head, I see the amplitude as the length of the vector, i.e. the length of the line you draw from the centre to the point you want to rotate, and the phase as the initial angle of that point. The frequency part doesn't really fit, but to me it just amounts to find the amount of rotation you want and find a time step you need to animate it properly in place.

By the way, the book defines the phasor itself to be:

X = Ae^j? - Length and initial angle

and redefines the last formula to be:

z(t) = Xe^j(w0 t)

In other words you have the Amplitude and initial rotation multiplied with each small steps you want per time unit. (The formula really shows radian frequency multiplied with time.)

I must admit that I am trying to understand the whole picture here myself, but I have such a strong feeling that there must be a much simpler way of visualising this relationship with quartenions somehow. This example shows what happens in one dimension. If you understand what is going on in one dimension, it must be easier to understand what is happening in all three dimensions.

I am sorry if I am way off here though... It is just an idea I have had for a long time and want to find out more about. smile.png

A complex signal is simply a function of the time which takes complex values. For each time step you have a complex number defined by that formula. Since the amplitude is a constant, that signal represents a complex number of fixed magnitude and different angle. The angle is equal to theta at time zero and then increase linearly. The frequency (actually the angular velocity) represents the rate of change of the angle. Since the initial angle (the phase) is constant, it can be extracted from the exponential, separating the constant part (your phasor) and the time varying part (an exponential with a fixed frequency). It has nothing to do with quaternions.

They are basically the usual stuff you can found everywhere without any proof/explanation. I have a few observations:

  • The dot product is not part of quaternion algebra. Since quaternions are 4D vectors you can clearly define it in the usual way, but it is an additional structure.
  • You have defined the conjugate as an "optimization" for the inverse in the unit quaternion case, but it is very important in quaternion algebra, They are for example used to define the norm of a quaternion |q| = sqrt( q * conjugate(q)) and to define multiplicative inverses q^-1 = conjugate(q)/(q*conjugate(q)). Indeed we have that q*conjugate(q) is the real number |q|^2 and thus q*(conjugate(q)/(q*conjugate(q)) = 1.

Whether the dot product is an additional structure or not is debatable. As you point out, q * conjugate(q) is an important quantity (the square of the norm). This is a positive-definite quadratic form in the R-vector space of quaternions, and it naturally defines an inner product by writing

norm_squared(q + w) = norm_squared(q) + norm_squared(w) + 2 * inner_product(q, w)

The inner product defined that way is the same as the dot product of q and w as elements of R^4. So I wouldn't say it's an additional structure.

A complex signal is simply a function of the time which takes complex values. For each time step you have a complex number defined by that formula. Since the amplitude is a constant, that signal represents a complex number of fixed magnitude and different angle. The angle is equal to theta at time zero and then increase linearly. The frequency (actually the angular velocity) represents the rate of change of the angle. Since the initial angle (the phase) is constant, it can be extracted from the exponential, separating the constant part (your phasor) and the time varying part (an exponential with a fixed frequency). It has nothing to do with quaternions.

First: I am not trying to argue with you in any way, I am trying to understand.

Isn't that what a rotation in graphics is too: A fixed magnitude and a initial angle (the phase at time zero), then a rotation? I know that the angular velocity (sorry for being imprecise) makes the phasor rotate round and round forever in the kind of signal I presented, but isn't that just the representation, as you are saying? It is easy to rewrite the function I wrote to behave in the way that it stops at a given rotation in stead of rotating forever. Instead of giving a time, you would say that you want the rotation to be done in a certain amount of time OR in small steps until you reach the angle of rotation you want? In both cases complex notation is used too. I kind of see some resemblance here, but I am absolutely aware that I may be completely wrong. I probably am.

Edit: Probably what you are saying is that my thought is closer to be using Euler angles than quternions?

I am not implying those are the same, by the way, just that I thought maybe it can be used somehow to help visualising and give an intuitive understanding of quaternions.

Whether the dot product is an additional structure or not is debatable. As you point out, q * conjugate(q) is an important quantity (the square of the norm). This is a positive-definite quadratic form in the R-vector space of quaternions, and it naturally defines an inner product by writing


norm_squared(q + w) = norm_squared(q) + norm_squared(w) + 2 * inner_product(q, w)

The inner product defined that way is the same as the dot product of q and w as elements of R^4. So I wouldn't say it's an additional structure.

You are probably right, the quaternion algebra is a normed space and you can thus define an inner product in that way. And it is probably useful and worth defining anyway.

@aregee: I'm arguing we are speaking about different things. Signals are quantities varying in time, while quaternions represents a particular "state" (a transformation from one position to the other). In general a quaternion do not depends on any quantity (it is also true for complex numbers). It doesn't represent a process, there are no steps toward your final orientation.

You may have signals taking value in any set and you can then clearly have quaternion signals. You may also define the exponential function*. The exponential map is defined in this case from the 3D vector space (of imaginary quaternions for example) to the unit quaternions. You have that

exp(v) = (cos(theta/2), sin(theta/2)*v/theta)

where theta=|v|. You can thus generalize most of what you have seen to the quaternion case. But the theory is actually mostly the same and it doesn't give any new insight.

@aregee: I'm arguing we are speaking about different things. Signals are quantities varying in time, while quaternions represents a particular "state" (a transformation from one position to the other). In general a quaternion do not depends on any quantity (it is also true for complex numbers). It doesn't represent a process, there are no steps toward your final orientation.

You may have signals taking value in any set and you can then clearly have quaternion signals. You may also define the exponential function*. The exponential map is defined in this case from the 3D vector space (of imaginary quaternions for example) to the unit quaternions. You have that

exp(v) = (cos(theta/2), sin(theta/2)*v/theta)

where theta=|v|. You can thus generalize most of what you have seen to the quaternion case. But the theory is actually mostly the same and it doesn't give any new insight.

I am trying to find an intuitive way to connect formulas with what is really happening with quaternion rotation, like not just accepting "it is how it works" kind of explanation. I want to really understand it on a deeper level, but still intuitively. I understand now what you are saying, and I agree with you. I think I just need to spend a full day trying to wrap my head around quaternions.

This video is cheesy, but the best explanation I have found till now.

Though, as mentioned above, the material you discuss can be found elsewhere, I find your article a nice summation of aspects of quaternions applicable to game programming.

A comment on your description of SLERP: although you define the range of t, you do not mention the intent of a SLERP with regard to t. In the context of interpolating rotations, IMHO, you should add something like "With t varying linearly, SLERP provides..."

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement