Advertisement

rotation again (no angles, no quaternion...)

Started by April 13, 2003 09:10 AM
1 comment, last by mininova 21 years, 10 months ago
Hello. I did not find anything specifically addressing my problem: once more, a free camera in 3D space, but I decided to evade quaternions until I fully understand them. After meeting the gimbal lock :-) I got to using four vectors: ''position'' (the position in space), ''lookdir'' (a normalized vector pointing into looking direction), ''right'' and ''up'' (perpendicular to ''lookdir''). Using separate functions to rotate about the local x, y, and z axes. Now I am trying to set up my camera like this: (OpenGL of course...) void SetCamera() { Vector3D xaxis(1, 0, 0), yaxis(0, 1, 0), zaxis(0, 0, -1); /// from the Red Book: Vector3D rot = CrossProduct(zaxis, lookdir); glRotatef(DEGREES(acos(DotProduct(zaxis, lookdir))), rot.x, rot.y, rot.z); glTranslated(-position.x, -position.y, -position.z); } It works quite good when rotating about the y OR the x axis only. The Red Book says that using the arccos of the dot product is not very accurate for small angles, but this is not the single problem. How could I use my RIGHT and UP vectors in a set of instructions to achieve the desired effect? Is it possible at all? Up to now, performance is no problem (heavy use of sin and cos in the rotating funcs anyway).
So, my understanding is that you have position, lookdir, right, and up vectors. If this is true, then I believe there is a better approach than trying to figure out a rotation. The better approach is to allocate a vector:

GLfloat transformation[16];

Then populate that transformation according to your 4 vectors. This is quite simple, and although this sort of is an OpenGL discussion, it is also math/geometry related so I will show the answer here:

Stuff the position vector into transformation[12], [13], and [14]
Stuff the right vector into transformation[0], [1], and [2]
Stuff the up vector into transformation[4], [5], and [6]
Stuff the lookdir into transformation[8], [9], [10]

Make transformation [3], [7], and [11] equal to 0.0
Make transformation[15] equal to 1.0

Then, once you have transformation, in place of your glRotatef and glTranslatef just do this:

glLoadMatrixf(transformation);

You may have to negate your lookdir or camera position, but basically if you have all 4 vectors then there is simply no need to go and calculate a rotation and use glRotate/glTranslate. Just populate and stuff the matrix.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Advertisement
Thanks! Meanwhile I figured this out myself, but I needed to negate all vectors, which is something I cannot fully understand. Since I wrote a matrix multiplication function there might be an error in there somewhere. But it works this way. When using the vectors for placing the camera, I also need to change the rows for the columns of the rotation matrix (means the right vector goes to elements [0], [4] and [8]).
Again, thank you!

This topic is closed to new replies.

Advertisement