# Rotation in 3D?

• 12-23-2006
Queatrix
Rotation in 3D?
Say I have this:

Xpos = cos( myRot )
Ypos = sin( myRot )

What would this be:

Zpos = ?

I am assuming I would use another rotation var, but I would have to use it in my Y and Z dimentions. Am I going to use tan() at all? cos() or sin() again? Where would I use them? I am at a loss here, please help!
• 12-23-2006
CornedBee
First, your 2d rotation is wrong. What you get from sine and cosine are the axis-aligned offsets for movement of 1 at an angle given by the trig argument.
Correct 2d rotation is calculated as follows. Given a point P, you get the point P', which is P rotated around the origin by an angle of A in your favourite angle system, with the formula
P' = P cos(A) + Q sin(A) where Q is the point rotated counterclockwise by 90 degrees. In other words,
P'x = Px cos(A) - Py sin(A)
P'y = Py cos(A) + Px sin(A)
or in matrix form:
Code:

```P' = / cos(A)  -sin(A) \     \ sin(A)  cos(A) / * P```
Now it gets complicated. See, a 2d rotation is pretty simple, because it is always around the z-axis, i.e. if you stuck a pencil through the sheet of paper, you could then rotate around that pencil.

In 3d, rotation is far more complex, because it can be around an arbitrary axis. There are the three base axis rotations pitch, yaw and roll. In the typical object space, where x is left-right, y is up-down and z is front-back, i.e. depth, pitch is rotation about the x axis, yaw is around the z axis and roll is about the y axis. Think of a plane.
When you have these rotations, it's only the other two coordinates that change, using the standard sine/cosine scheme. Not sure which is sine and which is cosine, but that's pretty easy to work out and doesn't really matter anyway.
These rotations are simple and can be represented by a 3x3 matrix that consists of the 4 fields in the above 2x2 matrix in the right places, filled up with the values of the identity matrix in the other fields.

Rotations about an arbitrary axis are more complex. I won't go into deriving the formula. Look it up somewhere on the web or buy the excellent book "Mathematics for 3d Game Programming and Computer Graphics".
The matrix for a rotation of an angle of R about an axis represented by the unit vector A is given by this formula and matrix (C is cos(R) and S is sin(R)):
Code:

```    / C + Ax&#178;(1-C)      AxAy(1-C) - AzS  AxAz(1-C) + AyS \ P' = | AxAy(1-C) + AzS  C + Ay&#178;(1-C)      AyAz(1-C) - AxS | * P     \ AxAz(1-C) - AyS  AyAz(1-C) + AxS  C + Az&#178;(1-C)    /```
• 12-23-2006
Queatrix
I don't quite understand what you are saying, it's "over my head"; which is okay though, becuase I got it to work. My resualt: Zpos = sin( mySecondRot ). But with a couple tweaks, I had to use 90 degrees less than X and Y, and I had to make it negitive.
• 12-23-2006
Cactus_Hugger
But if zpos = sin ( some angle ) - where does the original point's position come in? All points do not rotate to the same z position. (Ditto for your original equations.)
About the only 3D math I remember is the distance formula, unfortunately...

Looking at Wolfram's "Rotation Matrix" article, it's simple enough for me to implement. (Although I'd have to remember how to do matrix multiplication apperantly, and memory seems to say that was painful...) (Also, I got there by google-ing for "vector rotation")
Hope that helps a bit.
Edit: I learned matrix multiplication in Alegbra II, and vectors in Precalc - to give you and idea of what math you might need.
• 12-24-2006
CornedBee
Quote:

Originally Posted by Queatrix
I don't quite understand what you are saying, it's "over my head";

Do you at least understand the part where I said that your rotation calculation is completely wrong in the first place? The only point you can rotate correctly this way is (1,0).