Why do game programmers use quaternions to rotate a vector?

Started by
33 comments, last by dawidjoubert 18 years ago
Do not understand me wrong I have nothing against quaternions, I really like them but I see many programmers specially beginners here talking about quaternions without knowing basic mathematics, one of the reasons I have started this threat is to show this people that there are easier ways if they just want to rotate a vector and that Mathematical knowledge is important for a programmer.
Advertisement
Indeed. Quaternions were conceived for a totally different purpose. Fundamental proofs of stereometry and geometry were written with quaternions first, long before the advent of vectors and matrices.
I doubt whether a textbook on quaternions would have more than 5% dedicated to their isomorphism with SO3. There are so many things about them, that rotations is the last you could mention.

My opinion is that most beginners use quaternions in order to avoid gimbal lock. And indeed, when I was younger myself, I probably wouldn't give a damn about what causes it, or having to rotate about three mutually perpendicular axes to get rid of it. I would only care about how to get the damn spaceship to rotate :) , and
if I had seen it written somewhere that "quaternions avoid gimbal lock" I would probably be flaming now, with arguments like: "dude come on! They avoid gimbal lock, but I don't know what causes it in the first place!"

IMHO, this is not bad; it's only natural. It's just a matter of prorities.
I don't use quaternions because I believe it is important to have a good Understanding of the math you use (unlike many beginners, as Kambiz pointed out)
consequently, seeing as how I cannot understand Why they work, and Hamilton(the inventor) as far as I can tell was insane, I don't use them.

I get by just fine, I use a largely vector and matrix approach.
Although even matrices tend to be rare for me, If I want to rotate a vector I just add a direction offset to it then Normalize.

It was mentioned that interpolating between two orientations at fixed speed was a 'killer app' for quaternion rotations. However I generally don't ever do that kind of thing; my engine is entirely based on physics not animation keyframes. So when I want to rotate between two orientations I use a PID controller that dynamically handles the motion...



Quote:Original post by someusername
My opinion is that most beginners use quaternions in order to avoid gimbal lock.


I think I should point out, that quaternions don't avoid gimbal lock....
gimbal lock is a property of the sequence of rotations you use, not how those rotations are represented.

Quaternions definitely get a lot of abuse, from both sides. A lot of people dismiss them without understanding what they have to offer (there have been some surprisingly ill-informed articles published online to this effect), and a lot of people embrace them with equally little understanding (just look at any of the various 'simple quaternion camera' tutorials that use them in completely pointless ways).

As usual, the truth is somewhere in the middle (obviously the posters in this thread know this, but it's always good to reiterate it for those new to the subject). The fact is, there is hardly anything (if anything) you can do with a quaternion that you can't do with a matrix. Matrices and quaternions do essentially the same things, but not always with equal ease or efficiency. So in the end it's just a matter of choosing the best (or perhaps just preferred) tool for the job.
Quote:Original post by haphazardlynamed
Quote:Original post by someusername
My opinion is that most beginners use quaternions in order to avoid gimbal lock.


I think I should point out, that quaternions don't avoid gimbal lock....
gimbal lock is a property of the sequence of rotations you use, not how those rotations are represented.


Correct me if I'm wrong, but gimbal lock comes from the fact that you perform three rotations around axes that depend on the previous rotations.
It is almost impossible that all three of them are perpendicular to each other. (that depends on the actual angles too)
This way, you may end up rotating twice around "one" axis, and this makes the model seem to rotate erratically.
This is what causes the weird behaviour we call "gimbal lock".
Quaternions avoid this problem, by performing a single rotation on the right plane...

Quote:
Original post by jyk
As usual, the truth is somewhere in the middle (obviously the posters in this thread know this, but it's always good to reiterate it for those new to the subject). The fact is, there is hardly anything (if anything) you can do with a quaternion that you can't do with a matrix. Matrices and quaternions do essentially the same things, but not always with equal ease or efficiency. So in the end it's just a matter of choosing the best (or perhaps just preferred) tool for the job.

I couldn't agree more. There may be problems with matrix rotation that do not affect quaternions.
Similarly, there are problems with quaternions, that can be handled much easier with matrices. E.g. creating a quaternion that rotates one set of base vectors to another.
Their true power comes when you can use them interchangeably to get what you want with the "least effort".
I actually could not find the nicely condinsed equation #4, which you provided. I have google searched for "rotate point/vector around arbitrary vector/axis" with minimal results (other searches as well). Most results are quanternions and the others have around 12 steps of costly math to rotate the point. I never had the patience to condense the formulas found down to #4 and just went with, oh well lets use freaking quanternions.


QUESTION: what the heck does #3 mean? Exp( vector_axis cross??? wha??? ). Does not seem to make a lick of sense to me.

ANOTHER QUESTION: in #2, what is the vector J? The original vector you whish to rotate around axis p? If so, why are you not using the variable x, like equation #4 for consistancy and less confusion?

YET ANOTHER QUESTION: I assume your applying all these trig/exp functions to every x,y,z component of the original vectors to obtain your new vector, correct?

LAST QUESTION: what is passive rotation, ive never heard that before.

one use of quaternions is their ability for spherical interpolation, which really improves quality of animations. I listened to a seminar about quaternions 2 months ago, unfortunately the person speaking didn t seem to understand them very well.
http://www.8ung.at/basiror/theironcross.html
#4 doesn't look all that great to me. qvq* seems to be a very simple equation to me. Even when it's fully expanded out to [cos(a/2),n sin(a/2)]v[cos(a/2),n -sin(a/2)], it seems no more complex than #4. It's also much more comprehensible to me. If I see the quaternion equation somewhere I know exactly what's going on. I can't tell immediately that #4 is a rotation operation.
Quote:Original post by someusername
Correct me if I'm wrong, but gimbal lock comes from the fact that you perform three rotations around axes that depend on the previous rotations.
It is almost impossible that all three of them are perpendicular to each other. (that depends on the actual angles too)
This way, you may end up rotating twice around "one" axis, and this makes the model seem to rotate erratically.
This is what causes the weird behaviour we call "gimbal lock".
Quaternions avoid this problem, by performing a single rotation on the right plane...


Yes, that is essentialy how gimbal lock works.

However, lets put it this way:
a quaternion can perform a rotation on an arbitrary axis.
a matrix can perform a rotation on an abitrary axis.

Gimbal lock is a problem with the 'Euler Angles' representation; of sequential Axis Aligned rotations that might recombine to move an axis onto one you already used, thus doing the 'rotating twice around one axis' effect you described. Euler Angles are merely a logical representation; a notation system. Whether you perform the rotation using a matrix or quaternion is an idependant issue.

a 'single rotation on the right plane' could be peformed by a quaternion, or by a matrix. the real question is, given an Euler Angles representation, how do you want to recombine those 3 succesive x,y,z rotations into a single 'right plane'? the process of recombination may or may not eliminate the gimbal lock, but this is also a separate problem from either the quaternion or the matrix used to peform the final rotation.


final killer example:
Gimbal Lock is a term that comes from Robotics, where the manuverability of a robot arm for example, would become limited if its joints were to align to lose an axis of freedom. Consider then, if your physical robot arm is capable of gimbal locking, then does it matter wheter the controller program hooked up to that arm represents the rotations as quaternions or as matrixes? Gimbal Lock is a logic control problem, not a math problem.


anyhow... I don't have anything against quaternions
I just get tired of people assuming that gimbal lock is exclusively a matrix issue and that switching to quaternions will magically fix everything
a search of the archives should reveal a few begginers' topics of "I switched to quaternions but still have gimbal lock! Help!" that will attest to the problems caused by this misconception...

[Edited by - haphazardlynamed on March 29, 2006 12:43:12 PM]
One reason why you would prefer quaternions to an axis-angle representation for vector rotation is if you need to accomodate a stack of several rotation operations. Concatenating quaternions is really easy; concatenating two axis-angle transformations is considerably harder.

Also, I don't think anyone mentioned the advantage quarternions have over a matrix representation in terms of maintaining the orthonormality of the transformation (that is, the transformation is less likely to become distorted or scaled due to rounding errors, since many fewer floating point operations are necessary for quaternion composition).

One final thought: because I don't feel like I understand quaternions very well, I set myself a while ago to deriving an alternative matrix formulation that was simpler than quaternions, didn't require composition of component matrix transforms (like Euler Angles), and didn't have any more special cases than Quats do (e.g., the degenerate case of a 180 degree pole-to-pole slerp). I succeeded pretty well, but then I thought about everything I'd have to do to actually use it; all the thunking code that would need to be written to map from quaternion orientation representations into my own system, the extra porting code needed for quat based animation systems, all the good, portable math libraries that would no longer be very useful to me. I eventually decided it was the lesser of two evils to pursue quaternions, despite not understanding them entirely. There is a pretty significant advantage to following the herd, after all :p
-david

This topic is closed to new replies.

Advertisement