Jump to content
  • Advertisement
Sign in to follow this  
gseed101

OpenGL camera question

This topic is 4839 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

hi, im quite new to opengl and have this problem. ive implemented a camera in my program which uses the arrow keys to move the forward/backwards and rotates left/right. i have an object on my screen and was wandering how i can get the camera to rotate left/right around the object because right now the camera only rotates relative to the 'eye' of the camera. thanks

Share this post


Link to post
Share on other sites
Advertisement
That's a different type of movement. If you only have one object, you could rotate the object instead of the camera. Otherwise:

Variables:

AngleX, AngleY, AngleZ - Angles of how to look at the object
ObjX, ObjY, ObjZ - Object position in your world
Radius - Radius of rotation around your object

Code:

glLoadIdentity
glTranslate(0, 0, -Radius)
glRotate(AngleY, 0, 1, 0)
glRotate(AngleX, 1, 0, 0)
glRotate(AngleZ, 0, 0, 1)
DrawYourObjectAtHere
glTranslate(-ObjX, -ObjY, -ObjZ)
DrawRestOfWorldAsIfWereAt000Position

To "move" the camera, change the angles and the radius.

Share this post


Link to post
Share on other sites
Quote:
Original post by gseed101
ive implemented a camera in my program which uses the arrow keys to move the forward/backwards and rotates left/right. i have an object on my screen and was wandering how i can get the camera to rotate left/right around the object because right now the camera only rotates relative to the 'eye' of the camera.

It depends how you're doing your current camera movement... suppose you extract the local orientation vectors from camera's transformation matrix. Then all you need, is to build a 'look at' matrix for your camera to rotate it correctly at the end of each 'tick'


// matrix made out of 4 vector4's: x, y, z and t(ranslation)
matrix4::set_lookAt( const vector3& Eye, const vector3& Target ) {

// Eye can be either current camera position or new spot for it
// Target is the spot to look at
vector3 direction = Target - Eye; direction.normalize();
vector3 right;
if( is_zero( direction.x ) && is_zero( direction.z ) )
right = cross( direction, vector3::unit_x );
else right = cross( direction, vector3::unit_y );
right.normalize();
vector3 up = cross( right, direction );

set_identity();
x = right;
y = up;
z = -direction;
t = Eye;
}


now, if you use such matrix to place/rotate your camera, because at each frame your camera is re-aligned to face the target, the regular left-right movement becomes in fact orbitting around the target... the forward/backward keys bring the camera closer/farther from the target, and up/down work in similar manner moving your camera along arc 'above' the target. And when you want to detach camera from target, just disable the building of look-at matrix in camera code.

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!