# Rotating objects in x y and z

by DaveC426913
Tags: objects, rotating
 P: 15,319 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...
P: 15,319
 Quote by D H First things first: It takes a minimum of three parameters to describe a rotation in three-space, not two.
Where did I say two parameters? You misunderstood what I wrote.

I actually said six:
 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.
But ultimately, it is only displayed in two dimensions:
 I'm going to end up with two equations, x=... and y=...
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.

Mentor
P: 15,170
Rotating objects in x y and z

 Quote by DaveC426913 Where did I say two parameters? You misunderstood what I wrote.

 Much of the rest of what you say seems to be based on that initial misunderstanding.
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.
P: 15,319
 Quote by D H 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.
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.
 Mentor P: 15,170 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 notationsthe 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.
P: 15,319
Hm. Well, you lost me right about
 Quote by D H 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 notationsthe 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$.
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.
 P: 15,319 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. Attached Thumbnails

 Related Discussions Introductory Physics Homework 1 Classical Physics 3 General Physics 27 Introductory Physics Homework 1 General Physics 3