• Create Account

## Camera orbiting around object

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1Alejandro  Members

142
Like
0Likes
Like

Posted 06 March 2011 - 09:36 PM

Hi,

I'm trying to achieve an orbiting camera using gluLookAt and spherical coordinates but I just can't get it right


#define PI 3.14159265

struct Camera{
float x, y, z;			// Camera position
float atX, atY, atZ;		// Where is it looking
float uX, uY, uZ;		// Where is up
Camera(){}
};

Camera c;
float rho		= 5.;
float phi		= 0.;
float theta	= 0.;

{
return degrees * (PI / 180.);
}

void draw()
{
glMatrixMode(GL_MODELVIEW);
// To Carthesian Coordinates
// Where the camera is looking
c.atX	= 0.;
c.atY	= 0.;
c.atZ	= 0.;
// What the hell is this for? Can't remember <img src='http://public.gamedev.net/public/style_emoticons/<#EMO_DIR#>/sad.gif' class='bbc_emoticon' alt=':(' /> where I took this from
c.uZ	= 1;

gluLookAt(c.x, c.y, c.z, c.atX, c.atY, c.atZ, c.uX, c.uY, c.uZ);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutWireCube(1);
glutSwapBuffers();
}


rho++ gets me a zoom in
rho-- gets me a zoom out
phi++ the camera goes 'up' but at a certain point it messes and it starts going down
phi-- the camera goes 'down' but at a certain point it messes and it starts going down
theta++ the camera rotates on z
theta-- the camera rotates on z

The main problem is when I modify phi I think that the problem is on UP vertex

### #2Bow_vernon  Members

238
Like
0Likes
Like

Posted 06 March 2011 - 09:55 PM

Maybe I could help:
CVector vOffset;
vOffset.x = sin(xzrot) * sin(yrot) * distance;
vOffset.y = cos(yrot) * distance;
vOffset.z = cos(xzrot)*sin(yrot)*distance;

CVector vCamFocus = object.pos;
CVector vCamPos = vCamFocus + vOffset;

xzrot is rotation in xzplane, yrot is in yplane..

### #3Seriphis  Members

188
Like
0Likes
Like

Posted 06 March 2011 - 10:12 PM

To help you think the problem through, what do you expect to happen when phi > 180 degrees?

Spoiler

### #4Alejandro  Members

142
Like
0Likes
Like

Posted 07 March 2011 - 08:00 AM

To help you think the problem through, what do you expect to happen when phi > 180 degrees?

Ok, so Phi has to go from 0 - 180... Will try it later

Thank you

Bow_vernon: Haven't checked your solution, also will try it later

### #5ericbeg  Members

108
Like
0Likes
Like

Posted 08 March 2011 - 08:53 AM

Hello,

Orbiting the camera is equivalent to rotating the camera location around the target position. There is a transformation matrix that allows you to rotate a point about an angle around an arbitrary axis.

Let's call this matrix R, you camera position c and you target position t. Orbiting the camera is done as follow:

c' = translate(t)*R*translate(-t)*c

Where c' is the new position of your camera after orbiting and translate( . ) is a translation matrix.

You still have to choose around which axis you want to orbit.

### #6haegarr  Members

7188
Like
1Likes
Like

Posted 08 March 2011 - 09:33 AM

The code snippet in the OP does the following:

It computes a unit length vector that points from the origin towards the location where the camera is placed.

 sin(toRadians(phi)) * cos(toRadians(theta));

and scales the vector by the camera distance rho, yielding in the position of the camera:

 c.x = rho * sin(toRadians(phi)) * cos(toRadians(theta));

Hence rho isn't a zoom factor, and changing rho doesn't zoom in/out. Instead, it moves the camera towards the origin (rho++) or away from it (rho--).

BTW: The formula used above is the conversion from spherical to cartesian co-ordinates, as can be read e.g. here on wikipedia. You can find the condition mentioned by Seriphis (and the other conditions) there.

Next, the ( c.atX, c.atY, c.atZ ) is set to the point where the camera should look at.

Now, a (not 0) vector in 3D space defines a direction but not an orientation. For an orientation you need a 2nd direction vector that is not co-linear to the 1st one. For this purpose typically the so-called up vector is used. In your case it is ( c.uX, c.uY, c.uZ ).

All this is pushed into gluLookAt (read about its parameters e.g. here).

### #7Alejandro  Members

142
Like
0Likes
Like

Posted 08 March 2011 - 01:53 PM

Thanks Haegarr for your explanation also eribeg.

I was debugging today and I noticed that when phi reaches 45º -> 0.785398 radians, the object that I'm looking (0, 0, 0) dissapears, after that in 46º, it appears to going down... also in -135º

Will try to find the answers in the documentation provided.