• Create Account

Need scary sound effects or creepy audio loops for your next horror-themed game? Check out Highscore Vol.3 - The Horror Edition in our marketplace. 50 sounds and 10 loops for only \$9.99!

# Quaternion Questions

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1Overdunn  Members   -  Reputation: 124

Like
0Likes
Like

Posted 24 October 2012 - 01:00 PM

I have a camera implemented in OpenGL using Euler angles. The final code at the end of a frame looks like this:
glRotatef(-pitch(), 1.0f, 0, 0);
glRotatef(-yaw(), 0.0f, 1.0f, 0);
glRotatef(-roll(), 0.0f, 0.0f, 1.0f);
glTranslatef(-pos().x, -pos().y, -pos().z);


I have encountered 0 problems with this implementation so far. However, I am starting to implement classes for objects in my 3D world and quickly realized the order of operations would definitely matter, i.e. (X * Y * Z) != (Z * Y * X) for rotations.

It seems the standard solution to this issue and others (Gimbal lock, interpolation, etc.) is to employ quaternions to represent the same information in a different form. I have read several tutorials on quaternions from around the net, and I have a few questions:

1. How do you visualize a quaternion? It's simple to imagine a vector and rotation around a vector. But how do you visualize a + bj + ck + dl? What does each component represent (visually)? For example, I'm having a hard time imagining what adjusting the scalar component or any of the complex parts does to my rotation.

2. I have found several equations for converting a quaternion to a 4x4 matrix. I was therefore considering using a single quaternion to represent my camera instead of the code above. However, I'm not sure I understand how. For example, how are pitch, yaw, roll, and a vector all contained within 4 values? I'm definitely missing something here.

3. Does an implementation using quaternions come with an implicit performance hit? It seems translating provided angles into a quaternion, then into a matrix, and finally multiplying that matrix with the MODELVIEW is expensive.

Thanks!

Edited by Overdunn, 24 October 2012 - 01:02 PM.

### #2Faelenor  Members   -  Reputation: 381

Like
0Likes
Like

Posted 24 October 2012 - 01:25 PM

• What do you mean exactly? YPR are 3 values, so a quaternion has actually one more component. And for the matrix, it's just that a rotation matrix has a lot of redundant information.
• Usually, you do all your computing using quaternions, as operations are easier and faster that using matrix. You are right that you have to convert it, but it's only one time per frame, so it's not too bad.
Edit: Oh, for #2, you mean to represent the whole camera with only one quaternion? That's impossible, you also need a position vector!

Edited by Faelenor, 24 October 2012 - 01:27 PM.

### #3greenvertex  Members   -  Reputation: 510

Like
0Likes
Like

Posted 24 October 2012 - 01:31 PM

This is probably one of the better resources I've found on the subject.

1. How do you visualize a quaternion? It's simple to imagine a vector and rotation around a vector. But how do you visualize a + bj + ck + dl? What does each component represent (visually)? For example, I'm having a hard time imagining what adjusting the scalar component or any of the complex parts does to my rotation.

In short? You don't. Our 3D experience doesn't lend itself readily to considering hyperspheres and such. Work on understanding the operations you need instead.

2. I have found several equations for converting a quaternion to a 4x4 matrix. I was therefore considering using a single quaternion to represent my camera instead of the code above. However, I'm not sure I understand how. For example, how are pitch, yaw, roll, and a vector all contained within 4 values? I'm definitely missing something here.

This seems like a good set of implementations for common quaternion uses.

3. Does an implementation using quaternions come with an implicit performance hit? It seems translating provided angles into a quaternion, then into a matrix, and finally multiplying that matrix with the MODELVIEW is expensive.

Yes, there is overhead, but you're saving in other places by eliminating some vector-matrix multiplication.

### #4Overdunn  Members   -  Reputation: 124

Like
0Likes
Like

Posted 24 October 2012 - 01:33 PM

HI Faelenor, thanks for your answer. My original question #2 said "pitch, yaw, roll, and a vector", so I was thinking 6 values actually. But you're right, and the example I saw actually used 3 quaternions not 1, they were just combined without ever being individually instantiated. I'm starting to get a better grasp now, thanks for the link. Maybe the "hyper-sphere" concept will help.

### #5Brother Bob  Moderators   -  Reputation: 6408

Like
2Likes
Like

Posted 24 October 2012 - 01:35 PM

I have encountered 0 problems with this implementation so far. However, I am starting to implement classes for objects in my 3D world and quickly realized the order of operations would definitely matter, i.e. (X * Y * Z) != (Z * Y * X) for rotations.

It seems the standard solution to this issue and others (Gimbal lock, interpolation, etc.) is to employ quaternions to represent the same information in a different form. I have read several tutorials on quaternions from around the net, and I have a few questions:

Before you dive into quaternions for these reasons, you should know what quaternions are just as sensitive to gimbal lock and order of operations as matrices.

Gimbal lock is an effect of using Euler angles, and using Euler angles as a basis for your quaternions is no better that using Euler angles as a basis for your matrices. The correct solution is to prevent gimbal lock is not to switch to quaternions, but to stop using Euler angles. You can run into the gimbal lock problem with quaternions also if youäre still composing your rotations from Euler angles.

Order of operations is an effect of how rotations work. If you're having problems with order of operations with matrices, you're going to have it with quaternions as well.

It sounds very much like you're just misinformed about what quaternions can do for you. They are no different than matrices when it comes to representing a rotation. Both are just tools to represent a rotation. Problems come from how you use your tools, and both tools are order-dependent and prone to gimbal lock when used with Euler angles. You don't change to quaternions for the reasons you mention, because you're going to keep running in to the same problems.

### #6Overdunn  Members   -  Reputation: 124

Like
0Likes
Like

Posted 24 October 2012 - 01:35 PM

Hi greenvertex, thanks for the links. 3 hours of tutorials and I didn't hit the 1st one.

### #7Overdunn  Members   -  Reputation: 124

Like
0Likes
Like

Posted 24 October 2012 - 01:37 PM

I have encountered 0 problems with this implementation so far. However, I am starting to implement classes for objects in my 3D world and quickly realized the order of operations would definitely matter, i.e. (X * Y * Z) != (Z * Y * X) for rotations.

It seems the standard solution to this issue and others (Gimbal lock, interpolation, etc.) is to employ quaternions to represent the same information in a different form. I have read several tutorials on quaternions from around the net, and I have a few questions:

Before you dive into quaternions for these reasons, you should know what quaternions are just as sensitive to gimbal lock and order of operations as matrices.

Gimbal lock is an effect of using Euler angles, and using Euler angles as a basis for your quaternions is no better that using Euler angles as a basis for your matrices. The correct solution is to prevent gimbal lock is not to switch to quaternions, but to stop using Euler angles. You can run into the gimbal lock problem with quaternions also if youäre still composing your rotations from Euler angles.

Order of operations is an effect of how rotations work. If you're having problems with order of operations with matrices, you're going to have it with quaternions as well.

It sounds very much like you're just misinformed about what quaternions can do for you. They are no different than matrices when it comes to representing a rotation. Both are just tools to represent a rotation. Problems come from how you use your tools, and both tools are order-dependent and prone to gimbal lock when used with Euler angles. You don't change to quaternions for the reasons you mention, because you're going to keep running in to the same problems.

I see. (BTW, the second line of this wikipedia article must be wrong then.) So how do you recommend representing rotations without using Euler angles? With vectors?

Edited by Overdunn, 24 October 2012 - 01:41 PM.

### #8Brother Bob  Moderators   -  Reputation: 6408

Like
0Likes
Like

Posted 24 October 2012 - 01:48 PM

You should use what representation makes sense. If Euler angles is what makes sense for you then that is what you should use. But as I said, you should just be aware that switching to quaternions in that case won't make any difference if you're afraid of gimbal lock. An axis-angle pair may be an option though if you actually have a choice.

### #9Overdunn  Members   -  Reputation: 124

Like
0Likes
Like

Posted 24 October 2012 - 01:51 PM

You should use what representation makes sense. If Euler angles is what makes sense for you then that is what you should use. But as I said, you should just be aware that switching to quaternions in that case won't make any difference if you're afraid of gimbal lock. An axis-angle pair may be an option though if you actually have a choice.

Thanks for the help! Another good tutorial: http://www.flipcode....atrfaq.html#Q60

Edited by Overdunn, 24 October 2012 - 01:53 PM.

### #10Álvaro  Crossbones+   -  Reputation: 7946

Like
1Likes
Like

Posted 24 October 2012 - 02:44 PM

Some intuition of what rotation a quaternion represents is that the imaginary part of the quaternion (b*i+c*j+d*k) indicates the axis of rotation is along (b,c,d), and the real part (a) tells you how much to rotate by (a=cos(angle/2)). The size of (b,c,d) is such that a^2+b^2+c^2+d^2=1.

If you think about it this way, you should see that (a+bi+cj+dk) represents the same rotation as (-a-bi-cj-dk).

Edited by alvaro, 25 October 2012 - 03:21 AM.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS