Typos in quaternion tutorials?

Recommended Posts

Nairou    430
Does anyone have some authoritative sources for learning the math behind quaternion rotation and conversion to matrices? I've been reading the following two tutorials: http://www.gamedev.net/reference/articles/article1095.asp http://www.cprogramming.com/tutorial/3d/quaternions.html and while they mostly agree, there are small changes between the two of them, like using different quaternion components in multiplications (for example, look at their quaternion-to-matrix math). Since they are both describing the same thing, I can only assume one of them is a typo. I really wish I knew more about the math being done to follow it and know for sure which one was correct. Barring that, are there tutorials or references which are known to be accurate?

Share on other sites
jyk    2094
The quaternion-to-matrix example in the second article does indeed appear to have some errors. The corresponding example in the first article appears to be correct.

If you just want to be sure you have the 'correct answer', the quat-to-mat conversion can be found many places online, so you should have plenty of references to double-check against.

If you want to derive it yourself, a fairly straightfoward way is to work out by hand the quaternion-vector rotation formula v' = qvq*. The result, when expressed as a matrix-vector multiplication, will give you the matrix form for the quaternion rotation.

A couple of other things you might run into that can be a bit confusing are the difference between row-basis and column-basis convention for matrices (one is the transpose of the other), and the difference between standard and 'reverse' quaternion multiplication (which affects the form for quaternion multiplication, as well as the quaternion-vector rotation formula and quaternion concatenation order).

If you have further questions about any of these issues, just ask.

Share on other sites
Nairou    430
Quote:
 Original post by jykThe quaternion-to-matrix example in the second article does indeed appear to have some errors. The corresponding example in the first article appears to be correct.If you just want to be sure you have the 'correct answer', the quat-to-mat conversion can be found many places online, so you should have plenty of references to double-check against.

Good to know, thanks for confirming that. Maybe it's just me, but "good" descriptive quaternion websites seem to be hard to find.

Quote:
 Original post by jykIf you want to derive it yourself, a fairly straightfoward way is to work out by hand the quaternion-vector rotation formula v' = qvq*. The result, when expressed as a matrix-vector multiplication, will give you the matrix form for the quaternion rotation.

Could you explain the components of your "v' = qvq*" formula? I can probably do the math, but I don't know what you're starting with there (I'm learning but a lot of this math is still new to me).

Quote:
 Original post by jykA couple of other things you might run into that can be a bit confusing are the difference between row-basis and column-basis convention for matrices (one is the transpose of the other), and the difference between standard and 'reverse' quaternion multiplication (which affects the form for quaternion multiplication, as well as the quaternion-vector rotation formula and quaternion concatenation order).

I've read many places that matrix row/column order doesn't matter much as long as I remain consistent, I assume this is still the case here?

As for the second, do you just mean that, like matrices, quaternion multiplication is not reversible? Or is there more to it than that? I guess I've wondered how one determines the "correct" order to perform the multiplication, rather than taking a tutorial's word for it (same is true I guess of matrix multiplications as well...), but are there cases when the reverse is actually useful? Or is it just something to watch out for?

Share on other sites
jyk    2094
Quote:
Original post by Nairou
Quote:
 Original post by jykIf you want to derive it yourself, a fairly straightfoward way is to work out by hand the quaternion-vector rotation formula v' = qvq*. The result, when expressed as a matrix-vector multiplication, will give you the matrix form for the quaternion rotation.

Could you explain the components of your "v' = qvq*" formula? I can probably do the math, but I don't know what you're starting with there (I'm learning but a lot of this math is still new to me).
v is a quaternion whose xyz components are set to the elements of the vector you wish to rotate. The w component can safely be ignored, and is usually set to 0.

v' is the vector (still in quaternion form) after the rotation. The rotated vector can be extracted directly from the xyz components of v'.

* is the conjugate operator. The conjugate of a quaternion (x,y,z,w) is (-x,-y,-z,w).
Quote:
 I've read many places that matrix row/column order doesn't matter much as long as I remain consistent, I assume this is still the case here?
Yup. For the most part it doesn't matter which convention you use; the pitfalls are simply in maintaining consistency and knowing which convention is being used in a given reference (quite often it's not specified one way or the other).
Quote:
 As for the second, do you just mean that, like matrices, quaternion multiplication is not reversible? Or is there more to it than that? I guess I've wondered how one determines the "correct" order to perform the multiplication, rather than taking a tutorial's word for it (same is true I guess of matrix multiplications as well...), but are there cases when the reverse is actually useful? Or is it just something to watch out for?
Like matrix multiplication, quaternion multiplication is not commutative; therefore, order matters.

Using the standard mathematical definition of quaternion multiplication, the quaternion-vector rotation formula is v' = qvq*, as I mentioned earlier. It follows from this that the quaternion product q1q2 applies the rotation represented by q2 first, followed by the rotation represented by q1. This corresponds to the use of matrices with column vectors, where the matrix product R1R2 has a similar effect.

Those who are used to row-vector notation expect the matrix product R1R2 to have the opposite effect: R1 is applied first, then R2. For these folks, standard quaternion concatenation order, being 'backward' from this, can seem unintuitive.

A workaround for this is to flip the sign of the cross product in the quaternion multiplication, yielding the 'reverse' form. This changes the quat-vec rotation formula to v' = q*vq. As a result, the quaternion product q1q2 applies q1 first, then q2, just as with row-basis matrices.

This 'reverse' form is used in, among other places, the book '3D Math Primer', which is a fairly popular reference on game math. It's also used in the DirectX math library, so that the behavior of DX quaternions and matrices (which use row-vector notation) will match.

Hm, I guess that took a little bit of explaining :-| In short, just be sure you know which convention is being used by the references you read, and make sure to be consistent in your own code.

Share on other sites
Nairou    430
Awesome explanation! Thank you very much, I'll read over this a few times to make sure I got it, but it clarifies a lot already. Like DX, my matrix library uses row-order, so that was really good to know. :)