user1942541
user1942541

Reputation: 347

OpenGL Rotate camera around center-of-scene

I have a scene which is basically a square floor measuring 15x15 (a quad with coordinates (0,0,0) (0,0,15) (15,0,15) (15,0,0) ).

I 've set the center-of-scene to be at (7.5,0,7.5). Problem is I can't figure out how to rotate the camera horizontally around that center of scene (aka make the camera do a 360 horizontal circle around center-of-scene). I know you need to do something with sin and cos, but don't know what exactly.

Here is the code (plain C):

//set camera position
//camera height is 17
GLfloat camx=0, camy=17, camz=0;

//set center of scene
GLfloat xref=7.5, yref=0, zref=7.5;

gluLookAt(camx, camy, camz, xref, yref, zref, 0, 1, 0);

//projection is standard gluPerspective, nothing special
gluPerspective(45, (GLdouble)width/(GLdouble)height, 1, 1000);

Upvotes: 4

Views: 12559

Answers (2)

Danstahr
Danstahr

Reputation: 4319

You need to modify the camx and camz variables.

The points you want to walk through lie on the circle and their coordinates are determined by x = r*sin(alpha) + 7.5, z = r*cos(alpha) + 7,5, where r is the radius of the circle and alpha is the angle between xy plane and the current position of your camera.

Of course the angle depends on the rotation speed and also on the time from the beginning of the animation. Basically, the only thing you need to do is to set the right angle and then calculate the coordinates from the expressions above.

For more info about the circle coordinates, see Wiki : http://en.wikipedia.org/wiki/Unit_circle

Upvotes: 5

micha
micha

Reputation: 49542

I think there are two ways you can use:

You can use sin/cos to compute your camx and camz position. This picture is a good example how this works.

An alternative would be to move the camera to 7.5, 0, 7.5, then rotate the camera with the camera angle you want. After that you move the camera by -7.5, 0, -7.5.

Upvotes: 1

Related Questions