Camera rotation with gluLookAt

Started by
4 comments, last by Zakwayda 14 years, 12 months ago
Hi! I am trying to build myself a little camera rotation around the y axes with OpenGL. My problem is that I cannot figure out the x and z position of the camera. What I have is the radius and the y angle. I drew a little picture to show it better xD http://www.bilder-hochladen.net/files/big/4nc2-26.png I hope somebody can help me with my Problem. PhiLLe Note: I research and tried but nobody seams to have a solution.
Advertisement
Short answer:

float z = cos(y_angle) * radius;float x = sin(y_angle) * radius;glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(x, 0.f, z, 0.z, 0.z, 0.z, 0.f, 1.f, 0.f);

If any part of that is unclear, post back and I'll try to explain it in more detail.
Thank you very much! The rotation works.

But it is not like I thought it would be. When I debugged it I had this:

y angle: 0
z: 7 (my ratio)
x: 0
------------------------------------
y angle: 0.78
z: 4.9764 // I already rotated 90 degrees?? why? my angle is 0.78!
x: 4.92295
------------------------------------
y angle: 1.56
z: 0.0755791 // 180 degrees
x: 6.99959
------------------------------------
y angle: 2.36
z: -4.96854 // 270 degrees
x: 4.93088


How can I calculate it, so that one rotation is 360°?
Quote:y angle: 0.78
z: 4.9764 // I already rotated 90 degrees?? why? my angle is 0.78!
x: 4.92295
Actually, that's ~45 degrees, not 90.

In any case, it looks like you're missing a degrees-to-radians conversion somewhere (I don't know what language you're programming in, but it's likely that the sine and cosine functions you're using expect the angle in radians, not degrees).
Thank you very much! I didn't think about that.
Btw: I am using c++ with the cmath for cos and sin.

My calculation looks like this now:

double z = cos(PI * angleY / 180) * 7;
double x = sin(PI * angleY / 180) * 7;

Works fine.

Now I am trying to figure out how this would work in 3D. So I want to add a camera rotation around the x axes.

This means I have a x angle variable. But it's the same radius.
I also know I have to add another calculation for y:

double y = sin(PI * angleX / 180) * 7;

Now I don't know how to add both the x and y axes together.

Can you help me with that?
Quote:Original post by Phille
Thank you very much! I didn't think about that.
Btw: I am using c++ with the cmath for cos and sin.

My calculation looks like this now:

double z = cos(PI * angleY / 180) * 7;
double x = sin(PI * angleY / 180) * 7;

Works fine.

Now I am trying to figure out how this would work in 3D. So I want to add a camera rotation around the x axes.

This means I have a x angle variable. But it's the same radius.
I also know I have to add another calculation for y:

double y = sin(PI * angleX / 180) * 7;

Now I don't know how to add both the x and y axes together.
When you only have the y angle to worry about, you can compute the camera position using a polar-to-Cartesian coordinate conversion. A polar coordinate is expressed in terms of an angle and a radius, while Cartesian coordinates are the usual rectangular coordinates that we're all familiar with (e.g. x, y, and z). The bit of code I posted earlier performs this conversion.

When you introduce a second angle (e.g. theta, phi, and a radius - in your case y would be theta and x would be phi), you have what are called spherical coordinates. Converting spherical coordinates to Cartesian coordinates is similar to the Polar-to-Cartesian conversion, but is a bit more involved.

I find it easy to get this wrong when writing it off the top of my head, but in your case the conversion would look something like this:
z = cos(y_angle) * cos(x_angle) * radius;x = sin(y_angle) * cos(x_angle) * radius;y = sin(x_angle) * radius; // Which you already have :)

This topic is closed to new replies.

Advertisement