OpenGL camera question

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

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 on other sites
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

Code:

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 on other sites
Quote:
 Original post by gseed101ive 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 on other sites
thanks for that,
that helps a lot :)

Share on other sites
there s already a function glulookat(...) use this and you are fine

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 19
• 15
• 10
• 9
• 11
• Forum Statistics

• Total Topics
634097
• Total Posts
3015508
×