# 3D rotation of a vertex about the origin

I don't want anything overly complicated nor a 500 page web site to read.

I want a mathmatical equation(s) to rotate a given vertex at (x,y,z) with a current angle Theta to the X axis, Phi to the Y axis, and Omega to the Z axis.  The defined rotation is about all three axis defined by a rotation of Theta + Theta' about the X axis, Phi + Phi' to the Y axis, and Omega + Omega' to the Z axis.  Where Theta', Phi' and Omega' are the user defined rotation to rotate beyond the current rotation about each axis.

The equation should work with no exceptions such as 90 degree rotation about the Z axis.  I've been looking at this complex matrix rotation stuff for a while now and honestly do not feel like figuring out the derrived rotation matrix for a rotation on all three axis.  Answer should be in the form:

x = something
y = something
z = something

Another question that is not required to be answered is given an arbitrary vector to rotate about how to calculate rotation about that axis.  So say given vector (v.x, v.y. v.z) and a rotation about that vector with a measure of the current angle of the point given by the dot product of the y axis and the line (giving a perpendicular line to the Y axis and the given vector) how could I rotate a point in 3D space about that arbitrary line.

Thank you!

~Aqua
LVL 10
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
To rotate a point by an angle omega around the Z axis, do this:

x' = x*cos(omega) - y*sin(omega)
y' = x*sin(omega) + y*cos(omega)
z' = z

Similarly, around the x-axis:
x' = x
y' = y*cos(theta) - z*sin(theta)
z' = y*sin(theta) + z*cos(theta)

Around the y-axis:
x' = z*sin(phi) + x*cos(phi)
y' = y
z' = z*cos(phi) - x*sin(phi)

To perform a sequence of rotations around these axes, just do those transformations in sequence.

Commented:
You don't need to worry about 'the measure of the current angle'.  These transforms work just fine without that.

Your second question about rotating around a line in space becomes very easy once you learn about matrix transforms, but it appears you don't want to do that.
It isn't that I don't want to learn about matrix transforms but I'm not on the top of my game when it comes to multiplying complex matricies with a vector matrix....  If you can explain how to do the matrix and multiply it out.. heh...  That's just what I don't understand... Matrix mathematics has now been over 4 years ago... and I haven't kept up on that knowledge so it's all but gone.

It's not that I don't want to learn how it's just reading it from crappy explinations in a book that don't step through it lose me and then I get adjitated...

:-)

If your up for a lil' teaching lesson on matrix multiplication of a 4x4 * a 1x4 matrix I'm all ears.

~Aqua
BTW:  Thank you for the solution... an accept is yours.  :-)

~Aqua
Commented:
The omega rotation above:

x' = x*cos(omega) - y*sin(omega)
y' = x*sin(omega) + y*cos(omega)
z' = z

corresponds to this matrix-vector multiplication:
[x']     [ cos(omega)    -sin(omega)     0   ] [ x ]
[y'] =  [ sin(omega)     cos(omega)     0   ] [ y ]
[z'] =  [      0                    0               1   ] [ z ]

But doing the transforms involves more than matrix-vector multiplication.  You have to do matrix-matrix multiplication to string several transforms together, and you also have to do a matrix inversion to reverse a transform.

To rotate points around an arbitrary line L0, using quaternions, this is the general process:
1.  Find a translation matrix T that moves L0 so that it crosses the origin.  Let L1 be the transformed line from this.
2.  Find a rotation matrix R1 that rotates L1 around the Z-axis so that the transformed line L2 rests in the X-Z plane.
3.  Find another rotation matrix R2 that rotates around the Y-axis so that L2 comes to be aligned with the Z-axis.
Now the transform matrix (R2 * R1 * T) takes  points along the original L0 line and aligns them with the Z-axis.
4.  Create a rotation matrix R3 that rotates everything around the Z-axis by your desired rotation angle.
Let X = (R2 * R1 * T)
Let X' = the matrix inverse of X
Let Q = (X' * R3 * X)
The matrix Q will perform the transform you want.  X pulls everything in to surround the Z-axis, R3 rotates everything around the Z axis, then X' moves everything back.

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Question on rotation:

Given point 0,1,0 rotated about the Z axis by (PI / 2) radians or 90 degrees:

I get:
Rotated by 90 degrees about the Z axis (-1 , 6.123031769111886e-17 , 0) W:1

When it should be -1,0,0 correct?

Do I need to check the values of Zero going into this?

If you are familiar with javascript here's the code.. :-P  ...Seems you've dabbled a lot in different TA's so...

function rotateZ(theta){
//Rotate around the z-axis:
var       x = this.x,
y = this.y;
this.x = x * Math.cos(theta) - y * Math.sin(theta);
this.y = x * Math.sin(theta) + y * Math.cos(theta);
}

Passed either parameter PI / 2 or 90 the above print out is with PI / 2 with 90 as a parameter I get:

Rotated by 90 degrees about the Z axis (-0.8939966636005578 , -0.4480736161291701 , 0) W:1

So I make the blind assumption that JavaScript deals with trig functions in Radians, but this has yet to explain the correct X value for 0,1,0 rotated 90 degrees about the Z axis and an incorrect Y value.

If this is just an incorrect rounding error on JavaScripts part with the ^e-17 I can truncate after so many decimal places but...  I want to make sure first.

Thank you!

~Aqua
Commented:
6.123031769111886e-17 is ridiculously close to zero, don't worry about it.

Almost all programming languages' trigonometric library functions work with radians, and I believe javascript is no exception.

Also, when it comes to doing exact comparisons of floating point numbers, you shouldn't.  Instead of checking if (x == y), you should be checking something more like if (Math.fabs(x - y) < epsilon) where epsilon is a really small number that meets the needs of your application.