Jump to content
  • Advertisement
Sign in to follow this  
jjavahe

Something fun - 3rd person tank controls, openGL

This topic is 3857 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!