What are quaternions? When/how to use them?

Hi all, I’m a new Panda3D (italian) user, and this is my first post.
I studied some of of linear algebra, vectors, general physics etc. at the university, but I’ve never heard of quaternions.
So, what are and (above all) how and when to use quaternions?
Is there any tutorial/guide?

Thank you very much!
And sorry for the noob question. Is there any FAQ list somewhere?

EDIT: Sorry, my intention was to post in General discussion Forum, I don’t know what appened. Please move this topic in the correct place.

MOD EDIT: Done.

Quaternions are just a mathematical shorthand for doing rotation calculations. Conceptually, they are similar to axis-angle rotations, so you might get more search hits related to that. But unlike pure axis-angle rotations, you can do all kinds of nifty tricks with them, such as spherical linear interpolation. (You want angle C which is halfway between angle A and B? Slerp between A/B at time frame 0.5.) You can also combine them into spline-quads in order to smooth interpolate between multiple rotations.

You can easily do your math in pure Euler angles and never touch Quats, but you better be sure you’re dealing with canonical rotations. (ie: Make sure pitch is always -180,180, bank is -90,90, etc, but even then you can still get gimbal lock if you aren’t careful with how you combine them.) And you can’t easily interpolate them, not without a lot of special-case logic. (For example, rotating from 170 to -170 degrees.)

They’re convenient in that they take 4 numbers to store, versus 3x3 for a rotation matrix.

Panda does everything internally as matrices, so it’s really your call on how you want to manipulate them.

How and when to use them? Well, for example, I needed a vehicle that will conform to sloping terrain. I did the math in Quats because it’s very simple… One way to do this is to calculate a new forward axis for your vehicle so you can use lookAt(), based on its current right axis (ie: its heading) and it’s desired new up axis (which would be the terrain’s normal). With Quats, all you need to do is:

oldRight = myobject.getQuat().getRight()
newUp = groundNormal
newForward = newUp.cross (oldRight)
myobject.lookAt (newForward, newUp)

Update: Quaternions are usually not taught in standard math course because they’re a branch of complex numbers that has very little real use except outside of realtime graphics (and probably robotics). Both are fairly new fields of study, compared to linear algebra, etc.

long answer short. if you dont know what quaternions are, you do not want to use them, and you propably wont be able to use them.

as already said, quaternions are a way to store and calculate rotations in a way which is very hardware-friendly. but they are far from beeing easy to understand.

First, thank you very much for both your very useful replies.

But don’t radiant angles (instead) avoid special-case logic?

Well, you wan’t believe it, but it is exactly the task I’m working in at present (!!). Impeccable timing.

So (it seems just telepathy) I think I’m going to copy it :p.

I understand.

I think it’s a wise and precious suggestion in general, but maybe I don’t totally agree, because if I don’t know what quaternions are, how could I know if I want to use them? For example, see the FenrirWolf’s code snippet above.

Update: Anyhow, I asked about quaternions above all because they appear in first ODE-physics examples in the Panda3D manual (Worlds, Bodies and Masses, Simulating the Physics World).

Hardware-friendly means also more quicky? In this case it’s a interesting information.

No, radian angles are still euler angles.
Basically, the two main advantages of quaternions are:

  • No gimbal lock. Gimbal lock is when two or three of the rotational axes are perfectly aligned and thus you lose a degree of freedom.
  • A rotation from -10 to 10 degrees (euler) will take the shortest route.
  • More numerically stable as pointed out above.

The only disadvantage behind quaternions is that they are so hard to understand. This page might help you with more information about it though:
en.wikipedia.org/wiki/Quaternion … l_rotation

Do note that Panda can automatically convert euler angles into quaternions for you, and back, when you’re about to do something like a rotation interpolation. For example:
discourse.panda3d.org/viewtopic.php?t=6608

That’s just to copy the rotation from the body to the nodePath (and back). I preferred to copy the quaternion rather than the euler angles because of the reasons stated above.

Thank you very much pro-soft (also for references which I asked in the first post)! When I will start to really use quaternions, I will study them.

About the Panda3D manual, I encountered some incomplete sections. Do you roughly know when do them will be updated?

(notice I don’t actually know what sort of english I’m speaking)

Not until someone is bored enough to update them.

Probably better than you think :wink:

AFAIK, all 3d hardware works with 4x4 matrix stacks. So, natively, that is the fastest hardware representation. However, quats (and Eulers) can be converted into matrices in a fairly efficient manner, from what I understand, so it isn’t expensive to use those formats.

You can sort of arrange it like this, going from fastest-in-hardware to easiest-to-understand:

4x4 matrices -> Quaternions -> Euler angles

Let me elaborate more: The key to understanding quaternions is to realize that Euler rotations (which are 3 separate rotations around mutually perpendicular axis*) are equivalent to a single rotation around an arbitrary axis – axis-angle rotations. You’re dealing with the full orientation of an object in this sense, and this is what a quaternion represents. Just like a 3x3 rotation matrix, that orientation is relative.

*If those axis are not perpendicular, then you have problems. This is where gimbal lock comes from.

If you don’t actually want to understand quaternions, that’s fine, but you should have a pretty firm grasp on that concept. You can combine two quaternions in any shape or form and they will generate a valid rotation – Not so with Euler rotations.

Really, quats are the same thing as axis-angle representations, but you don’t get quite as many nifty shortcuts with those. The same goes for matrix vector rotations. In fact, outside of the squads, you can get by with just vector rotations. Some people say that quaternions are just a passing fad, fooey, I’m happy with linear algebra and vector math alone.

As proof, I’ll show how my example above could have been done with just vector math:

                mat = object.getNetTransform().getMat()
                oldRight = mat.getRow3(0).normalize()

                newUp = normal
                newForward = newUp.cross (oldRight)
                newRight = newForward.cross (newUp)

                mat.setRow(0, newRight)
                mat.setRow(1, newForward)
                mat.setRow(2, newUp)
                object.setNetTransform(mat)

BTW, not tested, I might have my rows wrong and some commands out of whack. As you can see, it’s pretty much the same thing, conceptually.

That wikipedia article is weak, IMHO. It starts out promising, but then it leaps right into the pure math of quaternions. But, I’m not a huge math guy.

It’s sort of similar to explaining how 4x4 matrices work. Again, you’re looking at something that you’ll never find in an algebra class, as it’s a specific funky mathematical shorthand for combining rotations and translations. And it has the same kind of non-intuitive mathematical weirdness behind it as quaternions. (Shearing 4D space, versus quat’s complex numbers.)

Sorry, my post is rambling now. I shouldn’t try to explain things when I am tired.