Advertisement

Locking 'roll' in Quarternions

Started by January 06, 2002 03:51 PM
22 comments, last by LTJ 22 years, 6 months ago
Hi! I want to lock the ''roll'' part in my Quarternion to get a spectator mode like that in quake where you just have yaw and pitch and the roll part is ''locked''. Any ideas how to do that? I know how to do it with Euler angles, so should I just convert my quarternion to Euler angles, add the delta values and convert it back? Or is there a better way?
If you are still interested in the solution to this, email me (jon@bolt-action.com) and I will reply with a preprint of my Game Developer article from March 2002.

It talks about how to limit roll in quaternion form. But it''s not a beginner''s article. (If you''ve seen my first couple of columns in the magazine, it''s at about that same level).

-Jonathan.

Advertisement
what is this question about anyway? why do you need to "lock" the roll? does it automaticly happen or what? if so, it just proves once again that quaternions are stupid..
--bart
quote:
Original post by bpj1138
what is this question about anyway? why do you need to "lock" the roll? does it automaticly happen or what? if so, it just proves once again that quaternions are stupid..



The roll occurs for topological reasons. Rotations can be visualized as movement on a sphere. Euler angles move you on the sphere by directly controlling your latitude and longitude, while quaternions move you a given distance in a given direction.

Now, you have to remember that a sphere is not topologically equivalent to a plane. On a sphere, the sum of the angles of a triangle is greater than 180°. How much ''extra'' (or missing) angle there is is a function of the curvature of the surface the triangle is drawn on.

You can see that easily by making a cone out of paper by cutting out a wedge and taping the edges back together. On a cone, the curvature is concentrated at the tip. A triangle which does not enclose the tip will have a 180° sum of angles : it is on an euclidean surface. A triangle which encloses the tip will have and ''extra'' angle... equal to the angle of the wedge you have removed.

On a sphere, the curvature (+360°) is regularly spread over the whole surface of the sphere, which means that the ''extra angle'' of a triangle is dependent on how much surface (i.e. curvature) it encloses.

Now, why do we care, will you ask ? Simple. If you start pointing in a direction, then slide along one such triangle (without rotating). You''ll find out in the end that you have turned by an angle which is exactly equal to the curvature enclosed by the triangle.

This is what causes the ''roll'' when you use a quaternion. Some 3D application interfaces illustrate this problem quite well (e.g. Teddy). If you move your mouse in circles, the object will undergo a ''roll'' which increases with the size of you circles.

A combination of rotations around the X and Y axes will usually cause a rotation around the Z axis. This is normal behaviour, if not expected by aficionados of Euler angles. Now, for a way to prevent it, assuming you are rotating around X and Y, you can manually reset the Z coordinate of you quaternion to 0 and then renormalize it. Since the (X,Y,Z) coordinates of the quaternion can be read as the axis around which you rotated, killing Z will kill the rotation around Z, that is, the roll. Do not forget to renormalize.

Finally, to answer to bpj1138''s comment, quaternions are not stupid, they exactly model the SO(3) group (rotations), and are thus used in _real_ applications. Due to the counterintuitive behaviour, and the added complexity they involve, they are not usually used in games, which employ Euler angles instead, with the notable exception of flight simulators (hey, aerospace was the first engineering domain where quaternion algebra was applied) as they prevent the deadly Gimbal Lock problem.

If you don''t know it, Gimbal Lock occurs when you have, say, roll and pitch = 90°. In such a case, you end up with two of your rotation axes being parallel : acting on roll or yaw (while keeping pitch = 90°) will have exactly the same effect. Which is wrong. This effect is the biggest flaw (along with the inability of doing proper interpolation) of Euler angles.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
hrm, i use a combination of euler angles and basis vectors, the orientation is kept as basis vectors, but you rotate the orientation using normal X/Y/Z rotations. the best way is to have to have two matricies or three, one for direction, one for roll, and one for pitch, multiply them out to get the final result. the math ends up a lot more readable..
--bart
To further follow up...

... it''s no longer true that only flight simulators use Euler angles. Most 6DOF games written by engine programmers with a clue use quaternions.

Anyone who writes a character animation system, who is not a dumbass, uses quaternions.

bpj1138, you have to realize that you''re speaking from inexperience when you say that quaternions are stupid. Like Fruny says, the group of quaternions is the same shape as the space SO(3). That is pretty deep. Quaternions give you a much better picture of how rotation in 3D space behaves -- and I mean physical rotation of real objects, regardless of underlying math -- than any other rotation representation. To gain a better understanding of this, I would recommend trying to accomplish more ambitious things. Try writing a good skeletal animation system. This will cause you to think about rotation deeply enough that hopefully you can come to a better understanding of what''s going on.

-Jonathan.

Advertisement
hrm, i really don''t see how letting the quaternion interpolate things according to some path on a sphere makes the simulation more accurate. after all this is what the post was about.. even in a simple situation, it creates its own rotation mechanics, while the problem calls for no z rotation. how is that any more accurate, and how do you tell it what the different torques are?

also, off the record, i find it that people who make things more complicated than they really are and then ridicule other people are really just feeding their egos, nothing more. it also has the reverse effect where that in the end they make themselves look like an ass.. nobody ever says anything, except me..
--bart
quote:

hrm, i really don't see how letting the quaternion interpolate things according to some path on a sphere makes the simulation more accurate.



Euler angles add constraints. The order in which you combine your x,y,z rotations matter, since each subsequent rotation is done around an axis transformed by earlier rotations. Quaternions represent rotations around an arbitrary axis.

quote:

after all this is what the post was about.. even in a simple situation, it creates its own rotation mechanics, while the problem calls for no z rotation.



Yep, it was a question about quaternions. It is not that it "creates its own rotation mechanics", it _is_ the way real world rotations behave. As I said, the way to cancel the Z rotation is to zero out the Z component of the quaternion, then renormalize. But even then, you have to realize that the behaviour of quaternion-based rotations is QUITE different from that of Euler angles rotations. Just try and you'll see.

quote:

how is that any more accurate, and how do you tell it what the different torques are?



You add up the torques vectorially, as you learned in your physics class (assuming you had one, it's not on your resume). The resulting vector is the axis of the rotation your torque caused. The magnitude of the vector can be related to the acceleration. It is then trivial to translate the rotation into a quaternion. IIRC : If your angle is a, and (X,Y,Z) the normalized axis vector, the quaternion is ( cos(a/2), sin(a/2)*X, sin(a/2)*Y, sin(a/2)*Z ).

quote:

also, off the record, i find it that people who make things more complicated than they really are and then ridicule other people are really just feeding their egos, nothing more. it also has the reverse effect where that in the end they make themselves look like an ass.. nobody ever says anything, except me..



Some things only look complex until one has learned how and why they work. Of course one has to be willing to learn, and go beyond cookbook solutions. That is also a question of ego.

Edited by - Fruny on January 15, 2002 8:01:50 PM
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Have you ever seen this demonstration? : Hold a book in front of you. Rotate it 90 degrees around the X axis, then 90 degrees around Y. Remember what pose it ends up in. Then move it back to the orientation you started it in, but now move it first 90 degrees around Y, then 90 around X. Hmmm, it''s not in the same pose as it was before. What''s going on?

I''m not trying to make things harder than they really are, bpj1138. I spend a good chunk of my time these days trying to simplify game programming concepts and explain them to people... I write a monthly magazine column doing that, and it''s a lot of work, but I think it is worth it in the end.

What I am saying is that Euler angles only seem good to you because you''ve only worked on easy rotation problems so far. That isn''t an insult, that''s just me calling it like I see it. Rotation is a weird thing that is difficult to think about. That''s regardless of what math you use to represent them.

The book example above is only one of several weird things about rotation. Quaternions help us to understand this weirdness and deal with it. They help because quaternions have a topology that mimics what happens in physical space. Again, this is deep, and it is useful.

You think Euler angles are simpler because you can visualize what it means to rotate around the X axis, and the Y, and etc. But because rotations are weird, this kind of methodology doesn''t get you very far, in terms of solving the problems you need to solve to make a good game.

Forget the skeletal animation system I was talking about in my earlier post. Try programming an over-the-shoulder chase camera. You know, the Tomb Raider-style thing that follows your character around. If you try doing this with Euler angles, you will have a lot of problems, and in the end, after a huge amount of work, the camera system still won''t be very good.

Yet the same camera system done with quaternions is simple.

But you don''t have to take my word for this. Try it. I know that me sitting here arguing with you is not going to convince you of anything. You have to investigate, yourself.

-Jonathan.
as a matter of fact i did take college level physics classes, and guess what, they never talk about torques that are acting along different rotational axis. yup, it''s pretty easy to see that torques acting on the same axis just add, but the other case ain''t so simple, is it? chew on that for a while.

second, i use BASIS VECTORS, did i not say that? i think i did, and guess what, if you have the axis of rotation vector and an angle it''s simple as shit to rotate the three basis vectors, there is no mystery to it. yes, when dealing with euler angles, it would be a different story.

camera movement.. hahah.. please.. get your direction vector by simply subracting the focus point from the position, then cross that vector with +Y to get the right vector, then cross the right vector by direction to get the up vector..

listen kiddies, i know more physics in my little pinky that you''ll even know in your life time, if you don''t believe me, go to my page and read the shielding theory..
--bart

This topic is closed to new replies.

Advertisement