Quaternion Questions

Started by
8 comments, last by alvaro 11 years, 5 months ago
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!
Advertisement

  1. You can have a look at the wikipedia page: http://en.wikipedia....patial_rotation
  2. 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.
  3. 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!
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.[/quote]

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.[/quote]

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.[/quote]

Yes, there is overhead, but you're saving in other places by eliminating some vector-matrix multiplication.
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.

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.
Hi greenvertex, thanks for the links. 3 hours of tutorials and I didn't hit the 1st one.

[quote name='Overdunn' timestamp='1351105233' post='4993507']
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.
[/quote]

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?
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.

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
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).

This topic is closed to new replies.

Advertisement