# Thread: 3d rotational displacement formula?

1. ## 3d rotational displacement formula?

I only recently started programing and after reading a little about vertices and polygons I was trying to figure out how rotations of vertices are calculated. I figure that if an object is composed of polygons, and polygons are composed of vertices then an object could be described as,
Code:
``` struct strobject{
Struct strpolygon[var1];
// other relevant members
}
Struct strpolygon{
Struct strvert[3];
}
struct strvert{
Float x;
Float y;
Float z;```
Moving the object in 3D space would be easy, just apply an offset to all of the members of polygons; but how would you re- orient the object?

2. X=xCos(angle)-ySin(angle)
Y=xSin(angle)+yCos(angle)
Z=xSin(angle)+zCos(angle)

I think, so i'm just working with two axis to find a third axis.

3. Time to brush up on that linear algebra (Rotation Matrix - Wikipedia)

There's also a set of articles about rotations on this website.

4. You will be concatenating matrices for this operation. However note that if you use the standard Rotation = Z * Y * X it will gimbal lock. You will need to look into using either quaternions or axis-angle rotation matrices to resolve the gimbal lock.

Once you get the final rotation matrix R your final world matrix is:

W = Root * S * R* T

Where Root is either the parent matrix or the identity matrix, S is scale, and T is translation.

5. Yes, I will have too look into quaternions, I read a little about it on wikipedia but I don't understand it yet. I ran into a similar problem with "dev superman mode" camera movements but I was able to solve it with the above formulas and map keys to tilt up/down pan left/right and move forward/backward on the camera/camera focus axis. I haven't ran into any issues with it yet but I do need to put a restriction on (opposite of azimuth, going brain dead) since "flying" can get confusing with out it.

6. Eh? Not sure I understand what you said. Gimbal lock occurs when a rotation about one axis results in an unintended rotation about another axis or quite simply one axis of rotation is mapped onto another axis of rotation.

This is what you should be doing for translations and rotations:

Strafe = translate along camera / object right vector
Fly = translate along camera / object up vector
Walk = translate along camera / object look vector

Yaw = rotate around camera / object up vector
Pitch = rotate around camera / object right vector
Roll = rotate around camera / object look vector