Something fun - 3rd person tank controls, openGL

Started by
0 comments, last by speciesUnknown 16 years ago
So, I've got this tank. It has a turret that is hierarchically on top of its base. I'm trying to setup this camera view that will always be behind the turret, even when the turret rotates. However, the camera should always be centered and focused on the base. This seems simple enough, right? Well I cant figure it out. Some information that might help you help me... I have two variables which control tank movement, baseTranslateX, and baseTranslateZ. The tank moves in the XZ plane. I have one variable, turretRot, which controls the angle the turret rotates. This is the angle the camera will have to follow, so that the turret will always face forward in relation to the camera. Im trying to do this using a gluLookAt(...) function or a series of glRotatef's and glTranslatef's, in the display function in openGL. Any help would really be appreciated!
Advertisement
Quote:Original post by jjavahe
So, I've got this tank. It has a turret that is hierarchically on top of its base. I'm trying to setup this camera view that will always be behind the turret, even when the turret rotates. However, the camera should always be centered and focused on the base. This seems simple enough, right? Well I cant figure it out.

Some information that might help you help me...

I have two variables which control tank movement, baseTranslateX, and baseTranslateZ. The tank moves in the XZ plane.

I have one variable, turretRot, which controls the angle the turret rotates. This is the angle the camera will have to follow, so that the turret will always face forward in relation to the camera.

Im trying to do this using a gluLookAt(...) function or a series of glRotatef's and glTranslatef's, in the display function in openGL.

Any help would really be appreciated!


this can be done with some euler rotation matrixes and vector maths.

step 1: calculate the matrix R representing the rotation of the turret around its origin.

step 2: calculate the vector T where the camera must lie away from the turret. e.g. if the camera is 1 unit above the turret, and 3 units behind, the vector will be 0,1,-3.

step 3: multiply T by R to get the rotated position of the camera relative to the turret origin. this gives you vector E.

step 4: P is the vector from the turret to the origin. E = E + P.

step 5: use gluLookAt(E.x,E.y,E.z,P.x,P.y,P.z,0,1,0); this puts the "Eye" or vector E on the position of the camera and the "center" of the scene on point P (where the turret is).

inputs:
P = position of turret. (1x3 vector)
R = turret rotation matrix. (3x3 vector)
T = desired offset from turret to eye of camera

output:
E = absolute position of camera relative to scene origin.
= (T*R)+P;

This can all be done using the OpenGL view matrix and translate / rotate commands. but, its better to learn about matrices and vector maths if your not already familiar with it.
Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!

This topic is closed to new replies.

Advertisement