Associated Topics || Dr. Math Home || Search Dr. Math

### Rotations in Three Dimensions

```
Date: 11/11/1999 at 21:11:14
From: Drew
Subject: Quaternions and 3D rotations

Hello:

I am trying to learn about quaternions and 3D rotations. I am trying
to build my own 3D engine and ran into the classic problem concerning
Euler angles. I understand the basics for quaternions, but don't
understand how to use them to rotate something in 3D. I have read many
books about it but they are all too complex. What if I want to rotate
of the sources I have found are simple enough. Additionally, could you
tell me how to rotate something about any general line in 3D? I know
they are both connected, I just don't know how to do it.

Thanks a lot,
Drew
```

```
Date: 11/12/1999 at 08:27:56
From: Doctor Mitteldorf
Subject: Re: Quaternions and 3D rotations

Dear Drew,

I don't know enough about quaternions to tell you how to relate them
to rotation matrices. I do know about rotation matrices, and I don't
think they have to be very hard.

To rotate an angle a about the z axis, apply the matrix:

[cos(a)  -sin(a)  0]
[sin(a)   cos(a)  0]
[  0       0      1]

From this, I am sure you can construct similar matrices for rotating

To do two rotations in a row, multiply the second matrix on the left
by the first matrix on the right. This should answer your question
matrices together, with the last of the three operations on the left.

To rotate about a general line in 3D, here's your strategy: First,
construct a rotation about z so that your line has no more y in it -
it is purely x and z. Call this matrix A. Then rotate about the new
y-axis until the new z axis is right along your line. Call this matrix
B.

Multiply BA = C, and calculate the inverse C^-1 = A^-1 * B^-1.

Now you have a matrix C that moves your z-axis to the line about which
you wish to rotate. C^-1 * C clearly does nothing at all. But suppose
that you sandwich another rotation in between: C^-1 * D * C, where D
is a rotation about the z-axis. Now you have a matrix that first moves
the z-axis to the line you've specified, then rotates about that line,
then puts the z-axis back where it would have been.

This is the matrix you want: C^-1 * D * C is the rotation about the
given line.

- Doctor Mitteldorf, The Math Forum
http://mathforum.org/dr.math/
```

```
Date: 12/2/2002
From: Jill
Subject: Re: Quaternions and 3D rotations

The preceding answer avoids this specific use of the quaternion.
I believe some reasons for that may be: it is not used in the
classic manner, and as a result is not encountered by math
enthusiasts very often. This specific use is not as bad as it seems,
it's just uncommon, unfamiliar, and applied a little differently.

With Euler angles the "Gimbal lock" problem is that certain
orientations are difficult or impossible to attain. And around these
orientations any kind of precision is just as difficult. In some
homebrew software you may have noticed the "top-over flip" effect,
which is that as you pass over the vertical axis, the object or view
suddenly flips by Pi degrees. The quaternion has 4 parts, basically
three imaginary numbers and a scalar. Using the quaternion to
overcome the "Gimbal lock" problem, the three imaginary numbers are
used with the imaginary part simply removed [x,y,z]. As non-imaginary
scalars they represent an arbitrary axis orientation in three
dimensions, and the scalar [w] is used to represent a rotation around
this axis. Note this also means there is more than one quaternion to
represent any particular orientation. This overcomes the "Gimbal lock"
problem.

There is more:

With these unique properties of the quaternion, it also makes the
application and adding of rotational vectors much more intuitive for
humans, and easier to code.

For example, when using Euler angles, if you move the joystick
forward and backward you would expect a rotation around the x-axis, and
if you move it left and right you would expect rotation around the
z-axis, and similarly if you twist the handle of the joystick you would
expect rotation around the y-axis. But if you move forward, then to the
side, then twist, then undo each in the original order, you will see
that [depending on which axis is calculated first] the last two angles
calculated don't rotate around the expected axis, but around the
projected axis after prior calculation(s). With the quaternion, the
projection from the joystick can be more directly applied to the
rotation of the object or view, and thus much more intuitive.

Another example is in physics. When applying your rotational velocity
and/or rotational acceleration vectors, they can be added and applied
much more directly and intuitively. It makes the calculations much
more simple and thus much less complicated code, etc. When you grasp
the concept, you may find, as I did, that you will want to use this
form for all your 3D engine representations. However, I do have some
transformation matrix algorithms available for Euler-quaternion
conversion.
```
Associated Topics:
College Linear Algebra
High School Linear Algebra

Search the Dr. Math Library:

 Find items containing (put spaces between keywords):   Click only once for faster results: [ Choose "whole words" when searching for a word like age.] all keywords, in any order at least one, that exact phrase parts of words whole words

Submit your own question to Dr. Math
Math Forum Home || Math Library || Quick Reference || Math Forum Search