#### Archived

This topic is now archived and is closed to further replies.

# Using gluLookAt() function

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

## Recommended Posts

You want to keep track of the camera's rotation and position and as the player turns/moves use those variables to rotate/translate the world.

If you want to turn the camera to the right (clockwise around the y-axis), then essentially you need to rotate the world to the left (counter-clockwise around the y-axis).

It takes a little getting used to thinking this way and keeping this straight, but it'll become second nature soon.

##### Share on other sites
You probably want to set up a point that tells where the EYE (x,y,z) is and a vector that tells what direction the CENTER is from the eye. When you set up gluLookAt() you add the Eye Position to the Vector Coords and use that for the CENTER. So if you're turning right or left, the vector rotates accordingly, and if you're going forward or back, you move the EYE towards or away from the vector.

For something like this you can even just hold an angle for the direction CENTER is and use trig to translate it some arbitrary distance from the EYE. I'm getting back into this stuff and was going crazy with the same problem, and realized I did something really stupid. Since OpenGL uses degrees, and I had repressed radians from my memory, I kept on sending degrees to the math.h trig functions and was about ready to kill someone.

##### Share on other sites
Thank you all for the suggestions you've made. I am still trying to figure this out. One thing I did was to use sin() and cos() functions to get the new x and z coordinates for the look_at params that I use in gluLookAt(). When I press delete key I increment angle by 2 degrees and my head moves west, when I press end key I decrement the angle and my head rotates east, so far so good. I haven't thought about the translation though just yet. Meanwhile, I've been messing with D3DX utility lib and so far I like how they let you left and right multiply your vectors with the matrix. I think this would help when thinking about having one grand fixed coord system or many local ones attached to the objects, I think Thank you once more.

Jerry

##### Share on other sites
Hi JD

I hope that i can help you with gluLookAt so let me begin:

In a room you are moving only along the x and z axis (until you will also move up or down). So you have e.g. float x = 0, z = 0, and angle = 0; angle is where you are looking at.
If you would like to move UP then you can compute it like this: z -= cos(angle), x += sin(angle). If you would like to move DOWN then use z += cos(angle), x -= sin(angle). To turn LEFT use: angle += (PI / 180.0f) * 1; if (angle > (PI * 2.0f)) angle = 0.0f; Zhis will turn you one degree clockwise and to turn RIGHT use: angle -= (PI / 180.0f) * 1; if (angle < 0.0f) angle = (PI * 2.0f); The only thing left is calling gluLookAt. This will lokk like this: gluLookAt(x, 0, z, x + sin(angle), 0, z - cos(angle), 0, 1, 0);
Try it...

VirtualNext

##### Share on other sites
Once again you guys are the greatest!!!

I have solved the problem thanks to your suggestions and can now roam around the room It took me a while to realize how the LookAt function works. I came to the conclusion, as many of you have, that the look_at params of the function should be treated as a decomposed vector components. So if I want to look northeast I calculate the x and z components of the look_at vector then use them to move the eye point to the east by delta_x and north by delta_z. So now not only my head turned to the right corner of the room, but my feet travelled that direction also. I made the mistake of only updating my feet's z component and passing it to z_eye param without also updating my eye_x component, and the result was a movement in north direction (while looking east) but not also in east i.e. x direction. Thank you all