# rotation mathematics....

• 12-07-2003
rotation mathematics....
ok, I am reading the chapter on transformations in "Mathematics for 3D Game Programming & Computer Graphics" and am having trouble figuring out which equations to use in rotating a camera vector.

equation 3.12
Code:

``` // R is the rotated vector P, O is the angle of the rotation R.x = P.x cos O - P.y sin O; R.y = P.y cos O + P.x sin O;```
judging by how their is no "center" for this rotation equation, I gather it rotates around <0,0,0>(what i mean to say, is that it is no different than the basic glrotate3f?).

now, then there is equation 3.19 which is rotating about an arbitrary axis represented by a unit vector

Code:

``` //again, R is the rotated vector, A is the axis, and P is the original Vector, and O is the angle which the vector is rotated //through R = P cos O + (A X P) sin O + A(A . P) (1 - cos O);```
However, this is only for rotating in 90 degree increments from what I see.

now my question is, how does game tutorials get this :

Code:

``` void CCamera::RotateView(float angle, float x, float y, float z) {         CVector3 vNewView;         // Get the view vector (The direction we are facing)         CVector3 vView = m_vView - m_vPosition;                        // Calculate the sine and cosine of the angle once         float cosTheta = (float)cos(angle);         float sinTheta = (float)sin(angle);         // Find the new x position for the new rotated point         vNewView.x  = (cosTheta + (1 - cosTheta) * x * x)                * vView.x;         vNewView.x += ((1 - cosTheta) * x * y - z * sinTheta)        * vView.y;         vNewView.x += ((1 - cosTheta) * x * z + y * sinTheta)        * vView.z;         // Find the new y position for the new rotated point         vNewView.y  = ((1 - cosTheta) * x * y + z * sinTheta)        * vView.x;         vNewView.y += (cosTheta + (1 - cosTheta) * y * y)                * vView.y;         vNewView.y += ((1 - cosTheta) * y * z - x * sinTheta)        * vView.z;         // Find the new z position for the new rotated point         vNewView.z  = ((1 - cosTheta) * x * z - y * sinTheta)        * vView.x;         vNewView.z += ((1 - cosTheta) * y * z + x * sinTheta)        * vView.y;         vNewView.z += (cosTheta + (1 - cosTheta) * z * z)                * vView.z;         // Now we just add the newly rotated vector to our position to set         // our new rotated view of our camera.         m_vView = m_vPosition + vNewView; }```
Do I need to do something as messed up looking as Gametutorial's method(I would like to know how they derived that equation), or can I make a better, simpler formula. I know I could do a simple "copy/paste" manuver and just rewrite it to use my classes, but I want to understand this fully as its alot easier to "re-derive" in case of losing the equation if I understand it and the math behind it.
• 12-07-2003
nevermind, i figured it out.
• 12-08-2003
Silvercord
yeah it's on pages 61-62

it's kinda tricky, but the meat of the math is on page 61 where they describe the parallel and perpendicular components. To be honest, if you read and understand the math of vector projections, you'll have enough understanding to program a pretty cool 3d graphics engine (that's basically all I know besides a little physics here and there)
• 12-09-2003
Well, i was mistaking the crossproduct to meaning it rotated in only 90 degree increments when all that is doing is providing a plane to rotate on. i.e. an axis of <0,1,0> and a vector <1,0,1> , the cross would be a vector along the X-axis. I know I could do better on explaining, but its hard enough learning it without having to explain it.

P.S. @ Silver : that whiteboard idea did wonders for figuring this one out.
• 12-09-2003
Silvercord
yep, best piece of hardware i've bought in a long time
• 12-09-2003
Now the question still remains, how did gametutorials get it the way they have it? With my rotate function, it looks pretty much like the second equation because I have overloaded operators for my Vector class so I dont have to do things individually. does gametutorials(which i have noticed has bugs in quite a bit of its programs) just simply use the same function, just dividing it into all the different components?

Also, I figured out most of it, however, i am still somewhat unsure on what
Code:

`A(A.P)(1 -Cos O)`
does, i know A(A.P) is the shared parallel component of the vectors A and P, however, what is the purpose of the (1- Cos O)? I presume it is to keep the vector the same length throughout the rotation?
• 12-09-2003
Silvercord
it's just re-arranged algebraically.

the real equation, in the normal human form is this;

(P - (A . P)A) * cos + (AxP)sin + (A.P)A

the final they give is:
Pcos + (AxP)sin + A(A.P)(1-cos)

the 1-cos comes from the P-(A.P)A. In my code, I use the first version

P - (A.P)A gives you the perpendicular component. This is hard to explain if you don't already understand it, but A.P gives you a length (its a dotproduct), and when you multiply that by A (which should be a normalized unit vector) you get the vector component that is the projection of P onto A. When you subtract the projection of P onto A from P it gives you the perpendicular component.

you don't need the last part in the normal human form of the equation if the vectors are already orthogonal, but if they aren't already orthogonal you need to add it in in order for it to represent a rotation (preserve lengths).
• 12-09-2003
ah, i figured it had something to do with the (P -A(A.P)), but i was looking at it with the wrong perspective(should of used a whiteboard or a piece of paper instead of trying to think it out).

in full, the way the final form was derived was:

(P - A(A.P))Cos + (AxP)sin + A(A.P)
Pcos + (AxP)sin + A(A.P) - A(A.P)cos
which factored down becomes:

Pcos + (AxP)sin +A(A.P)(1 - cos)

The original equation I find much easier to visualize(because its in parts that make sense by themselves, thank god for trig).

P.S.
Quote:

This is hard to explain if you don't already understand it
I understand it enough to know how to use it.
i.e.
with two vectors, A = <0,1,0> and L= <3,4,0> it is pretty easy to see that when we use the dot product which is 4 (0*3 + 1*4 + 0*0), which when multiplied times A gives us <0,4,0> that when we subtract that from L, we get <3,0,0> which is orthagonal to A.

P.S.S.

Quote:

if you read and understand the math of vector projections, you'll have enough understanding to program a pretty cool 3d graphics engine (that's basically all I know besides a little physics here and there)
because everything major like collision detection, gravity,etc. is based on Vectors and in most cases these relatively simple mathematics?(if we wanted to get a physics engine going, vectors can be....quite interesting especially in electromagnetism).

P.S.S. how is your engine going by the way? I havent seen anything new on it recently.
• 12-09-2003
Silvercord
vector projections and mathematics using dotproducts are used in almost every aspect of a major 3d engine, from the collision detection to lighting routines to AI. my engine is going good, but there's still a small problem with collision detection every once in a great while. we're making a small level with a 'horny bert' that chases you around and you have to kill him with grenades.
• 12-10-2003
Quote:

you have to kill him with grenades
ah, so you have weapons implemented? Also, how did you solve the problem that you had with gravity being too fast?

have you ever thought about making any 3D math tutorials?

btw, my project isnt going that well, as the way I have tiles, it isnt very "realistic", im going to have to probably come up with a new format for my tiles that would lend itself better to heightmapping(thanks to DavidP for using heightmaps for tiles).

I decided after a long hard look at 2D that I just wouldnt like doing 2D as I would rather have something that would heavily challenge my math skills ,and since I would be implementing 3d objects, mine as well make the whole thing 3d.

In otherwords, Im keeping with my project, but I am still in the phase of putting together prototypes to see what works best.
• 12-10-2003
Silvercord
the acceleration due to gravity problem had to do with my equations ( i was using the wrong equations. I can explain in more detail if you want me to). I don't have weapons implemented really, I've just got it so that you shoot a ball around the world and it bounces around the world. It looks incredibly realistic, and the physics for it will be used for the grenade launcher. Also, horny bert doesn't have animations...it's going to be stupid :)
• 12-10-2003