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

Started by Allen Chou, Apr 09 2014 02:05 AM

14 replies to this topic

Posted 09 April 2014 - 02:05 AM

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

Ming-Lun "Allen" Chou

Physics / Graphics / Procedural Animation

http://allenchou.net

Sponsor:

Posted 09 April 2014 - 02:32 AM

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.

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

**Edited by apatriarca, 09 April 2014 - 06:12 AM.**

Posted 09 April 2014 - 04:46 AM

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.

**Edited by aregee, 09 April 2014 - 05:11 AM.**

Posted 09 April 2014 - 05:02 AM

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.

Posted 09 April 2014 - 05:22 AM

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.

Posted 09 April 2014 - 05:50 AM

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.

**Edited by aregee, 09 April 2014 - 05:58 AM.**

Posted 09 April 2014 - 06:10 AM

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.

Posted 09 April 2014 - 06:41 AM

@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.

Posted 09 April 2014 - 07:45 AM

@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.

Posted 09 April 2014 - 07:58 AM

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.

Posted 09 April 2014 - 11:48 AM

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

rangeoft, you do not mention theintentof a SLERP with regard tot. In the context of interpolating rotations, IMHO, you should add something like "With t varyinglinearly, SLERP provides..."

Good call. I've added a short paragraph commenting on this property.

Ming-Lun "Allen" Chou

Physics / Graphics / Procedural Animation

http://allenchou.net

Posted 09 April 2014 - 07:02 PM

About SLERP, this article is usually worth looking: http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/

By the way, the thing about SLERP is not that it interpolates through the shortest path, but the fact that it ensures constant angular velocity.

In fact using the formula of your explanation alone you are NOT ensuring that you take the shortest path. For SLERP to ensure the minimal path the interpolation must be done between quaternions in the same hyperhemisphere. In order to do that, you need to test whether the dot product of both quaternions is negative and then, if so, negate one of them. And you can do that whether you use SLERP or NLERP.

“We should forget about small efficiencies, say about 97% of the time; premature optimization is the root of all evil” - Donald E. Knuth, Structured Programming with go to Statements

"First you learn the value of abstraction, then you learn the cost of abstraction, then you're ready to engineer" - Ken Beck, Twitter

Posted 09 April 2014 - 09:30 PM

About SLERP, this article is usually worth looking: http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/

By the way, the thing about SLERP is not that it interpolates through the shortest path, but the fact that it ensures constant angular velocity.

In fact using the formula of your explanation alone you are NOT ensuring that you take the shortest path. For SLERP to ensure the minimal path the interpolation must be done between quaternions in the same hyperhemisphere. In order to do that, you need to test whether the dot product of both quaternions is negative and then, if so, negate one of them. And you can do that whether you use SLERP or NLERP.

Thanks for the info. I edited the article a little bit to add that info in.

Ming-Lun "Allen" Chou

Physics / Graphics / Procedural Animation

http://allenchou.net

Posted 19 April 2014 - 09:26 AM

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.

how would you derive this quaternion inner product using only quaternion arithmetic though?

Posted 19 April 2014 - 12:43 PM

how would you derive this quaternion inner product using only quaternion arithmetic though?

The post you are responding to has the definition. But I'll make it explicit, to be more clear:

norm_squared(a) := a * conjugate(a)

inner_product(q,w) := (norm_squared(q + w) - norm_squared(q) - norm_squared(w)) / 2