# Quaternion n00b needs help

This topic is 4907 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Alllllrighty. So I think I've worn out the guys in the Game Programming forum with my thick-skulledness, I figured I'd spread some love down this way. It's about quaternions. I've been reading papers on them for 2 days, and they're still black magic to me. But I have gained this: I can mathematically reproduce a quaternion from a rotation about an arbitrary x,y,z axis; I can mathematically reproduce a rotation about an arbitrary x,y,z axis from a quaternion. I can also convert quaternions to and from rotation matrices. But what I'm beginning to see is that quaternions seem to represent rotations very accurately, but they do NOT represent orientation. It's relative to how the object was facing to begin with. In other words, say you have two objects, A and B. A faces positive X and B faces positive Y. Then say you have quaternion Q. If you turn Q into a rotation matrix, applying it to A will give you a different orientation than applying it to B. So really, it's just a fail-proof way to represent a series of rotations, right? But you can't use it to represent orientation ITSELF. You need to know the vector that defines how the object was facing initially, then use the quaternion to rotate it, and THEN your object will be oriented correctly ( meaning pointing the right direction AND titlted properly ). If I am wrong about that, please shoot me now. Seriously. If I'm not, is it possible to get a vector out of that rotation matrix ( built from the quaternion ) that represents the direction this object is now facing? I'm trying to figure out how I might move an object forward after rotating it via a quaternion. Thanks to anyone who bogged through my post. =) Mike

##### Share on other sites
Quote:
 Original post by Michael KronBut what I'm beginning to see is that quaternions seem to represent rotations very accurately, but they do NOT represent orientation. It's relative to how the object was facing to begin with.

Bingo! I hug you!!! Sorry, I meant to post exactly what you have realized, but never hit "Reply". Anyways, you are exactly right.

Now, if you consider the object to initially have his X axis facing down the global X axis, you can find the direction of his new X axis using the following equation:

xlocal = rot(q, xglobal)

where rot(q, v) is a function which rotates the vector v according to the quaternion rotation q, and xglobal is the global X vector, which is (1, 0, 0, 0). rot(q, v) can be implemented as:

rot(q, v) = q * v * q-1

Other axes can be determined similarly. You can actually use this to build up a rotation matrix (though it probably introduces more redundant computation to do it this way).

##### Share on other sites
Ok, more confusion.

Upon rethinking the quaternion meaning, couldn't a quaternion actually represent orientation if you set your meshes up properly?

A quaternion represents a rotation theta around an arbitrary axis x,y,z, right? Well if you treat x,y,z as your object's facing direction, and then theta as how you are rolled around that axis, couldn't you represent possible orientation?

In order to translate from your object's initial position to the orientation from the quaternion, you would first have to rotate your object so that it is facing down the arbitrary axis. Could I use a quaternion for this? How would I build a quaternion that represents a rotation from vector A(initial facing direction) to B(quaternion axis direction)? Anyway, once you can get THAT, then you just rotate by your original quaternion and boom, you're perfectly oriented.

Right?

Mike

##### Share on other sites
Oh NOES!

I'm wrong. When I rotate my mesh to face the quaternion arbitrary axis, I have no idea where my view-up vector will be pointing. Hence, even though I'm correctly positioned on the axis of rotation, there's no guarantee that rotating the number of radians specified in the quaternion will actually give me the right orientation. Actually, it seems pretty downright unlikely.

Is there any way to get around this? Is there some way to tie a view-up vector to the angle rotated around the arbitrary axis of a quaternion?

##### Share on other sites
Just a couple of comments. I won't get into the difference between orientation and rotation, but don't think that a quaternion can represent one but not the other; it can be used for either, just like a 3x3 matrix.

Also, it seems that you're equating the axis of rotation with the object's direction vector, which isn't necessarily the case. The visual that helped me understand axis-angle rotations is as follows. Imagine a ball floating in space, and a thin rod. The rod can be stuck through the ball in any direction, but must pass through the ball center. Once the rod is in place, it's clear that the ball can only rotate about the rod; the rod has become an axis of rotation. Given all possible directions for the rod, and a choice of angle in the range [0,pi], you can represent all possible orientations.

Now imagine an orthonormal basis attached to the ball. The basis vectors rotate about the axis of rotation, but none of them is necessarily aligned with this axis. It's not particularly intuitive, but the axis of rotation is not really a 'direction vector' of any sort (although a direction vector can be derived from it).

Well, I hope that didn't just confuse the issue further. I have to run now, but I'll be glad to clarify any of the above if needed.

##### Share on other sites
Quote:
 I'm wrong. When I rotate my mesh to face the quaternion arbitrary axis, I have no idea where my view-up vector will be pointing. Hence, even though I'm correctly positioned on the axis of rotation, there's no guarantee that rotating the number of radians specified in the quaternion will actually give me the right orientation. Actually, it seems pretty downright unlikely.

I think what you just described is a problem many, MANY, people have with their quaternion cameras. It's not really a problem, it's just how quaternion based cameras work and isn't what they thought they signed up for.

Quaternions can rotate around any arbitrary axis. In the case of cameras, you generally want the camera to rotate around it's relative x,y,z as seen by the user (looking through the camera).

If the camera rotates 1 radian around Y, then 1 around X, then 1 radian in the opposite direction from the first rotation around Y, you end up with a hidden rotation around Z. After the X rotation the Y axis for the camera has changed, so the third rotation does not simply undo the first as some expect.

Anyway, you can figure out the up view vector. It would simply be the Y axis after the rotation. And I believe M21+M41, M22+M42, and M23+M43 make up that vector from the Quaternion. That is, of course, assuming your original up vector was the global Y axis. If not, just rotate the up vector along with the object as Sneftel suggested.

Here's a link to C#/C++ Quaternion Free Cam Source and Link. The provided source is a little buggy but the fixes wern't major and it might spark some ideas. Also, there is a link to a C++ version.

Someone will probably correct me here, but I think unless you plan to do SLERPing, a rotation matrix will work just as well.

Good Luck,

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 33
• 16
• 11
• 10
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015633
×