quaternions as orientation

Started by
60 comments, last by GameDev.net 18 years, 3 months ago
Quote:Original post by someusername
edit:
I'm tired of posting this, but you'll probably need it...

To convert the quaternion to a DCM matrix (orientation) do the following:
Define vectors: localX, localY, localZ to store the object's local axes
localZ = Normalize( quaternion rotation axis {x,y,z} );
localX = cross( localZ, 'unit world up vector' );
localY = cross( localZ, localX );

The orientation matrix will be the product: R*O,
where R is:
[ cos(w) -sin(w) 0 0 ]
[ sin(w) cos(w) 0 0 ]
[ 0 0 1 0 ]
[ 0 0 0 1 ]


and O is:
[ localX.x localY.x localZ.x 0 ]
[ localX.y localY.y localZ.y 0 ]
[ localX.z localY.z localZ.z 0 ]
[ 0 0 0 1 ]

This is the DCM matrix for your object.
If you are using a left-handed system, negate the results of the cross products, and if you work with row-vectors instead of columns, use the transpose of that product.

Btw, have you come across this in physics simulation? I've lately discovered that people often choose this approach, to describe the instantaneous axis of an object's rotation and the angle it has rotated by, with respect to its previous state, when solving for the specific time step.


Since you're tired of posting it anyhow, please don't post it again. I hate to sound mean, but the code you've posted is just plain wrong, and I don't want to see it spread. The code isn't even for quaternions. You're presenting code for an axis-angle representation. Although a quaternion does encode axis-angle information, you won't see calls to sin() and cos() in a quaternion->matrix function since the sine and cosine values are already "encoded" in the 4-tuple.

Another big problem with your code is that it makes an assumption about a world orientation of world-up. This is an unnecessary restriction that leads to all kinds of singularities. Proper handling of orientation with quaternions doesn't require using a world-up vector here and avoids potential singularity issues arising around the up-axis. Converting any quaternion to a matrix in no way requires an external up-vector.

Please just spend a few hours reading up on quaternions. It's well worth the investment. It doesn't take much effort to learn the basics. Once you understand the relationships between vectors and quaternions, life is so much easier and results are so much more robust.
Advertisement
Quote:Original post by Sneftel
(where complex space has 2, 4, or 8 dimensions)


Actually, by the Cayley-Dickson construction, you can keep going on and on ad infinitum. But after the sedonions (16-dimensions), things start getting weird (e.g., zero divisors et. al.).
my understanding, and i may be way off, is that a quaternion is a hypercomplex number which is analogus to a complex number. quaternions reside in complex space where as complex numbers reside in the complex plane. so it would be fair to refer to a quaternion as a whole as a complex number, or hypercomplex number to be correct. but to say a quaternion contains complex numbers, well that wouldn't be right.

but by refering to them as just four scalers their relationship with complex numbers - and therffffeore how they relate to rotations becomes less clear. maybe that's just me.
As of my knowledge a quaternion
Q(r,a,b,c) := r*1 + a*i + b*j + c*k
is a complex number of dimension 4, as Sneftel and others have stated already above, where i, j, and k are imaginary numbers, and 1 a real number. Together [1,i,j,k] define the basis of Quaternion space. The values r, a, b, and c are real valued coefficients, giving the coordinates of Q in the quaternion space.

It is similar to Euclidean space: There the basis is given
in 3D: (1 0 0)^T, (0 1 0)^T, (0 0 1)^T
and a vector is build up of adding the weighted basis:
v(x,y,z) := x * (1 0 0)^T + y * (0 1 0)^T + z * (0 0 1)^T
See the similarity with the Q(r,a,b,c) above.

The writing as (x y z)^T is just a convenient method derived from vector calculus. In fact, this kind of writing is arbitrary as long as not also speciying the basis. It is just a common convention.

Quote:Original post by someusername
Quaternions are used in rotations, because they can uniquely describe an axis (3-vector) and an amount of rotation around that axis. The only possible way I see, to determine an orientation from a quaternion, is *to make a convention* that the rotation axis should always represent the "look-at" axis of your object (this one is usually the local Z axis), and the remaining scalar part should represent the 'roll angle' of your object around that axis. This is enough information to uniquely describe an orientation in 3d space.

A quaternion (to be precise: a _unit_ quaternion) _as a whole_ describes a rotation or an orientation. Here, as well as is the case for rotation matrices, orientation means nothing else than the rotation measured from an origin (what itself could be interpreted as the "identity" rotation but as orientation).

If you restrict the axis implicitely given by a quaternion to the look-at vector is the same as to restrict rotation matrices to represent rotations around the z-axis only. Quaternions definitely allow rotation around arbitrary axis, and hence the axis implicitely given by a quaternion could be oriented anyway.
Quote:Original post by Richy2k
Quote:Original post by Sneftel
First of all, they aren't three imaginary numbers and a real number. They're four real numbers which are used as a single four-dimensional complex number, just as two real numbers can be used as a single two-dimensional


Isnt it 3 complex numbers and 1 real number. W(X, Y, Z): W being the Real number?


This person is correct, Sneftel. A quaternion is a number which can be said to be made of 3 imaginary and 1 real part. The term vector was first used to describe the imaginary part of the quaternion by Hamilton. In addition, quaternions make a 4 dimensional real vector space, which perhaps could be your maning? It would seem though, you may have some concepts backwards. For example, treating a complex number as 2 reals is only possible if you consider them couples in a R^2; they are not inherently composed of 2 reals. A complex number has one real and one "imaginary" part.
Quote:Original post by nilkn
Quote:Original post by Sneftel
(where complex space has 2, 4, or 8 dimensions)


Actually, by the Cayley-Dickson construction, you can keep going on and on ad infinitum. But after the sedonions (16-dimensions), things start getting weird (e.g., zero divisors et. al.).


Sorry but as far as my Algbra book goes, the complex numbers (1+i) are a field and quaternion are only a division ring.
And to my knowlage you can't get a division ring with more elements then the quaternion.

Here a link for the math correct defention ( I hope :D ) http://mathworld.wolfram.com/Quaternion.html
“Always programm as if the person who will be maintaining your program is a violent psychopath that knows where you live”
Quote:Original post by dragongame
Quote:Original post by nilkn
Quote:Original post by Sneftel
(where complex space has 2, 4, or 8 dimensions)


Actually, by the Cayley-Dickson construction, you can keep going on and on ad infinitum. But after the sedonions (16-dimensions), things start getting weird (e.g., zero divisors et. al.).


Sorry but as far as my Algbra book goes, the complex numbers (1+i) are a field and quaternion are only a division ring.
And to my knowlage you can't get a division ring with more elements then the quaternion.

Here a link for the math correct defention ( I hope :D ) http://mathworld.wolfram.com/Quaternion.html


Actually there is no complex space here, the space is Real: hence a motivation for the terming real algebra. And the problem has not to do with "complex spaces" of numbers that are factors of 8 but simply because (for some fairly involved reasons) only in the quaternion, H, complex C, and Real R, spaces is it possible to construct an associative division algebra without zero as a divisor. The octonios O and up drop associativity and are not definable over a field or even ring. The reasons boil down to no more than it is not possible due to the properties of the manner in which construction occurs. Not so special, simply that because of the way *we define* stuff it just can't be done: you can't fit a sphere into a spherical indentation.

--

Yes dragongame, you're right. Rating++ ;). Actually, both complex and quaternions are defined over a field: the real numbers. The quaternions are after all a vector space over R but while the complex numbers as you state are themselves fields the quaternions only form a skew field (i.e no commutivity definable) or division ring. Nonetheless, sensible division is possible (for the very reason that they are a skew field, need to commute was dropped) and so quaternions are as well a division algebra. And yeah, above quaternions a division algebra is not possible since there exists zero divisors in higher dimensional spaces as as nilkn mentioned. Also as mentioned, the units quaternions represent or encode a rotation of 180 degrees.

cold_storm if all the responses seem as gobbledegook to you then read this: http://members.gamedev.net/potentialwell/nums_quarts.pdf, it is a very general and approachable explanation of quaternions. Sorry for a lack of clicky, AP's are not allowed such luxury. :(
I just reread my post and noticed I made a few nontrivial mistakes.
Quote:Original post by Anonymous Poster
The reasons boil down to no more than it is not possible due to the properties of the manner in which construction occurs. Not so special, simply that because of the way *we define* stuff it just can't be done: you can't fit a sphere into a spherical indentation.


I meant a cube cannot fit into a spherical indentation. And I should also emphasize that it is the manner of construction of the numbers and the properties that are wished (for reasons of sensibility) is what affects what algebras, fields, rings and Numbers etc. are, what they entail, what is possible and how they act.

Quote:And yeah, above quaternions a division algebra is not possible since there exists zero divisors in higher dimensional spaces as as nilkn mentioned.


A mistake here, I meant above quaternions a division ring is not possible, did not mean to say algebra. Since octonions form an (alternative) division algebra (lackof/weak associativity). But above octonions no division algebras are possible (zero divsors exist). Simply non associative algebras with unity.
@Troll

I think you're misunderstanding my post. Let me remind you (and explain) of some things I've posted.

I wrote in a previous reply
What you suggest, (using quaternions to orient objects) seems more like a convention to me, rathen than an intuitive/straight forward approach.
This means that I believe, in this case, you use a quaternion just because it's compact, not because it's how they were meant to be used in the first place, or because you can do operations on them and come to a 'solution quaternion' to use. I hope you see what I mean. E.g. you can represent an integer with a vector by agreeing to assign each digit to a component. You can do it, it may suit your needs better in some instances, but it doesn't mean that you should replace all integer arithmetics with vector calculus! It's just a temporary convention.

The only possible way I see, to determine an orientation from a quaternion, is *to make a convention* that the rotation axis should always represent the "look-at" axis of your object (this one is usually the local Z axis), and the remaining scalar part should represent the 'roll angle' of your object around that axis. This is enough information to uniquely describe an orientation in 3d space.
This means that you should forget the classic meaning of the quaternion's components and assign each one your own arbitrary interpretation. You only use a quaternion as I described, because it provides four floats in one wrapper, and this is just what you need.

I'm tired of posting this, but you'll probably need it...
I'm obviously not tired of posting anything, that's why I spend so much time in the forum. It was a figure of speech. Sorry if it sounded bad though. I believe it takes me quite long to answer, not only because it's not my native language, but also because I'm trying to clarify as deeply as possible, instead of just posting a comment in vagueness! And my time doesn't come for free either!

However, now that I've been looking into it more deeply, I made a mistake about the matrix I suggested... It's not R*O as I originally posted, it's O*R-1 == O*RT

On the other hand, you posted:

I hate to sound mean, but the code you've posted is just plain wrong, and I don't want to see it spread.
In what way is it wrong when all the above are clear? I wasn't talking about the approach where you integrate the orientation quaternion to find the new orientation. This is a physics engine issue.
What I was trying to describe, is how you can map a unique orientation to a single quaternion, when you've made the convention that you keep its look-at axis in its x,y,z components and an extra roll angle in w. If you're used to quaternion components having a different meaning, I'm sorry, but I wasn't planing on using it as a quaternion in the first place.
The matrix I described, maps the model space local Z axis {0,0,1} to whatever {x,y,z} axis vector you've plugged into the quaternion's x,y,z components and then rotates around it by w rad.
Isn't this a way to express orientation with a quaternion?
Isn't this enough to uniquely define an orientation in space?
For -PI<w<PI, isn't this a unique mapping of a single 4-vector, to a unique orientation of an object in R3?
How can it be that it's 'just plain wrong' then? Or 'it shouldn't spread'?

Although a quaternion does encode axis-angle information, you won't see calls to sin() and cos() in a quaternion->matrix function since the sine and cosine values are already "encoded" in the 4-tuple.

Of course you won't. Because in that case you have a unit quaternion that you intend to ultimately turn into a rotation matrix, therefore you've constructed it properly in the first place. (ie. sum of squared components == 1)
In the case I was describing, you don't find the orientation from the quaternion. You just keep the axis in the x/y/z and the angle in w.
The original question was how you can represent orientation with a quaternion.
Am I just wrong, because I'm not saying the same thing you had in mind?

Another big problem with your code is that it makes an assumption about a world orientation of world-up. This is an unnecessary restriction that leads to all kinds of singularities.

I agree. It can create singularities. But it's still one of the most common approaches in creating view matrices/'look at' controllers etc... Also, if you force-normalize the local axes vectors before plugging them into the matrix, it seems to fix most problems. I suppose some checking can be added, to eliminate the problem completely.
However, as posted, it is error prone, I agree.

Please just spend a few hours reading up on quaternions.
At my 25, I'm probably a few months away from signing in for my second degree in mathematics. (state university)
I'm not saying I'm a genius; on the contrary I'm getting rusty on many fields and I had to leave out a big number of interesting subjests -or just "scratch the surface"- due to their wide range.
I just don't think I would have to wait for your prompt to do so in the first place...

What you had in mind was obviously the approach used in physics simulations, where -instead of using the angular velocity vector to integrate the orientation matrix-, you use a quaternion. This is why I asked the OP if he had seen that in a physics simulation, so I could turn the discussion there. I've only learned of the existence of this method while discussing with another poster in this forum one week ago, but I see how it can be realized. Instead of integrating the 3 local axes (vectors in the orientation matrix) you integrate the quaternion axis and apply that instantaneous rotation for the time step directly via the quaternion.

The point is, that the original question was "How can one describe orientation with a quaternion?". I believe what I desribed in my post, was one way. You chose to argue with me, because what I described wasn't the one you had in mind. Instead of just argueing with me, don't you think you should post something -at least- pointing to the "correct" way to this? Just for the poster who had the question in the first place...
If I'm not mistaken, this is the idea.
The OP asked about quaternions for orientations, not fitting an axis-angle into a quaternion structure. Axis-angle might be more convenient for integrating velocities, but that isn't what the question was about.

This topic is closed to new replies.

Advertisement