# Rotating objects in x y and z

1. Nov 28, 2008

### DaveC426913

My buddy, a Flash developer, wants to learn 3D rotation of objects from first principles, i.e. using trig. I know there are shrink-wrapped engines out there that will do this for you, but he wants to learn it. From me. Cuz I'm the math whiz.

I used to do this simply when I'd plot simple 3D graphs in "Basic classic" but now I'm stepping it up a notch.

I'll start off with a primitive object and rotation on one axis.

So... my primitive object is a line in the XYZ space. It extends from 0,0,0 to 0,0,100. From my observer PoV, where I can only see the XY plane, the line is seen end-on and will appear as a point.

Now I rotate my line about the origin by 5 degrees. I know that I've got to do sin(5), which is about .087(x100 = 8.7). So the line is now visible in my XY viewing plane connecting 0 , 0 and 8.7 , 0.

Good so far?

Ultimately, it seems to me, I'm going to end up with two equations, x=... and y=... where the right side will be a long string of trig functions with x,y and z angles and x,y and z distances as variables.

Are there more robust techniques I can use? My buddy talks about multiplying matrices, which I know nothing about (but who knows, maybe I'm a quick learn).

And then I've got to add perspective...

Last edited: Nov 28, 2008
2. Nov 28, 2008

### Staff: Mentor

First things first: It takes a minimum of three parameters to describe a rotation in three-space, not two. Pick up a book. Rotating the book by say 45 degrees about an axis normal to
• The cover of the book
• The top edge of the book
• The right edge of the book
• Some other non-principal axis
will yield different orientations. There are three rotational degrees of freedom in 3-space. For 4-space there are six rotational degrees of freedom. In generalized N-space, there are N*(N-1)/2 rotational degrees of freedom.

Rotations, in any dimension, have the following characteristics:
1. Leaving an object unrotated is a null rotation of the object. Mathematically, rotation has an identity element.
2. You can undo any rotation. Mathematically, every rotation has an inverse.
3. If you rotate an object in some way and then rotate in some other way, the result is still a rotation of the object. Mathematically, rotation is a closed operation.
4. Suppose you rotate an object 3 times: rotation a followed by rotation b followed by rotation c. Since rotation is closed, with can denote a•b to denote the end result of performing rotation a followed by rotation b, and b•c as the end result of performing rotation b followed by rotation c. So, what happens when you do rotations a then b then c? Is the result (a•b)•c or a•(b•c)? The answer is "yes". (a•b)•c = a•(b•c). Mathematically, rotation is transitive.
You can view rotation as a mathematical operator on the orientation of object. From above, there is an identity rotation, every rotation has an inverse, and rotation is closed and transitive. These are the attributes of a mathematical group. Rotation is such an important group that it has a name: SO(n), where n is the dimensionality of the space. That SO means "special orthogonal". I'll get to that later. You are interested in SO(3).

There are many different ways to represent rotations in 3-space. A few of them are
• Rotation matrices. The rotation group is called the special orthogonal group because a rotation in N-space can be described by a NxN matrix, but not just any random NxN matrix. Only NxN matrices (a) have a determinant of 1 ("special") and (b) whose inverse is equal to the transpose of the matrix ("orthogonal") describe a proper rotation, and all matrices that have these two characteristics describe a rotation. This is true for rotations in any dimension, not just three. This general characteristic is why the n-dimensional rotation group is called SO(n).

• Euler angles. This is a sequence of three rotations about three pre-specified axes. (Remember that it takes a minimum of three parameters to describe a rotation). Which pre-specified axes depends on who you talk to. Astronomers use a rotation about the z axis, followed by a second rotation about the rotated x axis, followed by a third rotation about the doubly-rotated z axis. In short, a z-x-z rotation sequence. Aerospace engineers use some permutation of rotations about the x, y, and z axes, such as an x-y-z rotation sequence or a z-y-x rotation sequence; there are six such permutations altogether. Euler angles, whatever form you choose to use, are evil.

• Eigen rotations.
• Unit quaternions.
• Rodrigues parameters.
• Modified Rodrigues parameters.
• ...
Please read this wikipedia article on rotation matrices, and then come back with questions.

On final note: Very few people make a proper distinction between rotation and transformation. They are distinct but related concepts. The distinction is easily visualizable in 2D. Imagine an opaque sheet of paper printed with X-Y grid lines (e.g., graph paper) and a transparent sheet of plastic similarly printed with X-Y grid lines. Mark some point on the graph paper and some point on the transparency; these points will serve as the origins of your graph paper and transparency reference frames. Now mark some other point on the graph paper with a dot. That point has some specific X and Y coordinates in the graph paper reference frame. Now put the plastic transparency on top of the graph paper such that the origins are collocated. Next, rotate the transparency with respect to the graph paper. Finally, determine the transparency frame X-Y coordinates of the point you marked on the graph paper.

The physical rotation of the transparency with respect to the opaque graph paper is just that: a physical rotation. You can describe this rotation in terms of a rotation matrix. The graph paper frame and transparency frame coordinates of that point you marked on the graph paper are related by a transformation matrix from the graph paper frame to the transparency frame. The rotation matrix from the graph paper frame to the transparency frame and the transformation matrix from the graph paper frame to the transparency frame are not the same thing. They are instead transposes of one another.

3. Nov 29, 2008

### DaveC426913

Where did I say two parameters? :grumpy: You misunderstood what I wrote.

I actually said six:
But ultimately, it is only displayed in two dimensions:
So, again, I'll have two equations (i,e, two outputs : x and y screen coords) that will have 6 parameters as input.

Much of the rest of what you say seems to be based on that initial misunderstanding.

Note that, ultimately, I'm representing objects in 2-space.

Last edited: Nov 29, 2008
4. Nov 29, 2008

### Staff: Mentor

Now you are the one misunderstanding. My post has four sections: (1) degrees of freedom in a rotation in N-space, (2) rotation forms a mathematical group, (3) there are many different ways to represent rotations in 3-space, and (4) difference between rotation and transformation. Only the first part, which comprises the first paragraph of my post, pertains to that original misunderstanding.

5. Nov 30, 2008

### DaveC426913

Well, I mean I don't really see how that will get me to my goal.

Oh. Ah. I see now. I said my guy wants to "learn" this; I realize that was disengenuous. I realize our goal is really to understand the specifiic steps to get us to a workable algorithm.

6. Nov 30, 2008

### Staff: Mentor

I'll start with 2D rotation and transformation matrices and extend this to 3D.

Physical rotation of a vector in 2D
Suppose you have a vector with cartesian coordinates (x,y) and want to rotate this by some angle theta. I'll use the conventional notations
• the x-axis is horizontal, positive to the right,
• the y-axis is vertical, positive upward,
• vectors are expressed as column vectors, and
• rotation is positive counterclockwise.
Expressed as a column vector, the vector from the origin to $(x,y)$ is

$$\boldsymbol{r} = \bmatrix x\\y\endbmatrix$$

The coordinates of the endpoint of the rotated vector are (derivation is an exercise left to the reader) $(x\cos\theta - y\sin\theta, x\sin\theta + y\cos\theta)$ or

$$\boldsymbol{r}' = \bmatrix x\cos\theta - y\sin\theta \\ x\sin\theta + y\cos\theta\endbmatrix = \bmatrix \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \endbmatrix \, \bmatrix x\\y\endbmatrix = \mathbf{R}(\theta) \, \boldsymbol{r}$$

where

$$\mathbf{R}(\theta) \equiv \bmatrix \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \endbmatrix$$

is the 2D matrix that physically rotates a column vector by an angle $\theta$.

Transformation of a vector in 2D
If you physically rotate the $\hat{\boldsymbol x}$ and $\hat{\boldsymbol x}$ unit vectors by some angle $\theta$ you will get a new coordinate system with unit vectors $\hat{\boldsymbol x}'$ and $\hat{\boldsymbol x}'$:

\aligned \hat{\boldsymbol x}' &= \mathbf{R}(\theta)\,\hat{\boldsymbol x} \\ \hat{\boldsymbol y}' &= \mathbf{R}(\theta)\,\hat{\boldsymbol y} \endaligned

Given some point with coordinates $(x,y)$ in the original coordinate system, the coordinates of that point in the rotated coordinate system are (derivation once again left to the reader) $(x',y') = (x\cos\theta + y\sin\theta, -x\sin\theta + y\cos\theta)$ or

$$\boldsymbol{r}' = \bmatrix x\cos\theta + y\sin\theta \\ -x\sin\theta + y\cos\theta\endbmatrix = \bmatrix \cos\theta & \sin\theta \\ -\sin\theta & \cos\theta \endbmatrix \, \bmatrix x\\y\endbmatrix = \mathbf{T}(\theta) \, \boldsymbol{r}$$

where

$$\mathbf{T}(\theta) \equiv \bmatrix \cos\theta & \sin\theta \\ -\sin\theta & \cos\theta \endbmatrix$$

is the 2D matrix that transforms a column vector to the frame physically rotated by an angle $\theta$ with respect to the original frame.

In this simple 2D world, the transformation and rotation matrices are obviously related by $\mathbf{T}(\theta) = \mathbf{R}(-\theta)$. A much better way to look at this is that the two are transposes of one another: $\mathbf{T}(\theta) = \mathbf{R}^T(\theta)$. The reason this is a much better way to look at things is that this transpose relationship holds in higher dimensions. Rotation and translation are conjugate operations.

Sequence of transformations in N-space
Suppose you have three reference frames in some N-space with a common origin but different orientations. I'll denote these as frames A, B, and C. Denote $\boldsymbol{r}_A$, $\boldsymbol{r}_B$, and $\boldsymbol{r}_C$ as the representations of some vector r in these three frames. These representations are related by the transformation matrices from one frame to another:

\aligned \boldsymbol{r}_B &= \mathb{T}_{A \to B} \boldsymbol{r}_A \\ \boldsymbol{r}_C &= \mathb{T}_{A \to C} \boldsymbol{r}_A \\ &= \mathb{T}_{B \to C} \boldsymbol{r}_B \endaligned

Combining the above,

\aligned \boldsymbol{r}_C &= \mathb{T}_{A \to C} \boldsymbol{r}_A \\ &= \mathb{T}_{B \to C} \boldsymbol{r}_B \\ &= \mathb{T}_{B \to C} (\mathb{T}_{A \to B} \boldsymbol{r}_A) \\ &= (\mathb{T}_{B \to C} \mathb{T}_{A \to B}) \boldsymbol{r}_A \endaligned

The final step uses the fact that matrix multiplication is transitive. Since the above must be true for any vector r,

$$\mathb{T}_{A \to C} = \mathb{T}_{B \to C}\,\mathb{T}_{A \to B}$$

With an even more rotations chained in a sequence,

$$\mathbf{T}_{F_1 \to F_n} = \mathbf{T}_{F_{n-1} \to F_n},\cdots\,\mathbf{T}_{F_1 \to F_2}$$

This result makes no assumptions of the dimensionality of the space. Transformation matrices for column vectors chain from right-to-left extends to all dimensions. Since rotation is the transpose of transformation, rotation matrices chain left-to-right.

The order of operations here is very important for any dimension higher than two. Two dimensional transformation (or rotation) matrices commute. For higher dimensions, multiplication of transformation (or rotation) matrices is not commutative.

Rotation/Transformation in 3-space
Rotation/transformation in 2-space is much easier to comprehend than in 3-space. Obviously, a rotation or transformation matrix for a vector in 3-space in 3-space is a 3x3 matrix. Constructing or making sense of a 2x2 rotation or transformation matrix is an easy task. One way to construct a 3x3 rotation or transformation matrix is to construct the rotation/transformation as a sequence of simpler rotations/transformations.

Euler's rotation theorem will help in this regard. Euler's rotation theorem says that any rotation in three space is a rotation can be expressed as a 2D rotation about some axis in 3-space. In particular, the transformation matrices corresponding to rotations about the x, y, and z axes are

\aligned \mathbf{T}_x(\theta) &= \bmatrix 1&0&0 \\ 0&\cos\theta&\sin\theta \\ 0&-\sin\theta&\cos\theta \endbmatrix \\ \mathbf{T}_y(\phi) &= \bmatrix -\sin\phi&0&\cos\phi \\ 0&1&0 \\ \cos\phi&0&\sin\phi \endbmatrix \\ \mathbf{T}_z(\psi) &= \bmatrix \cos\psi&\sin\psi&0 \\ -\sin\psi&\cos\psi&0 \\ 0&0&1 \endbmatrix \endaligned

While Euler angles are evil, they do serve a very useful purpose in the construction of transformation matrices.

Sources of confusion
One obvious source of confusion is the distinction between rotation and transformation. I harp on this because they are distinct concepts and because failing to distinguish between these concepts is the source of a lot of errors. In this post, I have use $\boldsymbol R$ to denote a rotation matrix and $\boldsymbol T$ to denote a transformation matrix to avoid confusion.

Another source of confusion is whether vectors are expressed as column or row vectors. While column vectors are used more widely than are row vectors, this is just convention. If you instead use a row vector convention,
• You will have to post-multiply a vector by a rotation or transformation matrix to form a rotated or transformed vector.
• The rotation matrix for a row vector is the transpose of the rotation matrix for a column vector; the same is true for transformation matrices.
• Rotation matrices for row vectors chain right-to-left and transformation matrices for row vectors chain left-to-right.

Last words
My wife has the last word. She wants me to stop playing and do some silly house chores.

7. Nov 30, 2008

### DaveC426913

Hm. Well, you lost me right about
here.

If I define a point in the 3D space at xyz, and I supply a rotation of xyz, will those equations spit out an xy coordinate for it in the viewing plane?

I'm sorry, I have great facility with conceptualizing mathematics but, without a formal post-secondary education, I'm afraid the representation on paper is beyond me. I guess I'm in over my head. :shy:

Last edited: Nov 30, 2008
8. Dec 2, 2008

### DaveC426913

I'm still at it.

My point at 0,0,100, if rotated by 5 degrees about the y axis, will end up at these coords:

$$x_g = z_{d} sin(y_a)$$
$$y_g = z_d sin(x_a)$$
where
- $$x_g$$ and $$y_g$$ are the viewing plane XY coords (i.e. the output),
- $$z_d$$ is the distance from origin in the z direction and
- $$x_a$$ and $$y_a$$ are the x and y angles of rotation, respectively.

File size:
5.9 KB
Views:
46