Rotating the Camera about itself

Started by
7 comments, last by Winegums 18 years ago
Hi I feel bad making a post about camera rotation, but this confuses me and the other thread hasn't helped. I am trying to make a camera rotate about itself. that is, it is the equivelant of standing in teh middle of the room and spinning on the spot. I have tried modifying the glLookAt function like so:

float yaw = 0;
void DrawScene() 
{	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Clear The Screen And The Depth Buffer
	glLoadIdentity();// load Identity Matrix

	yaw+=0.01;

	//set camera looking down the -z axis,  6 units away from the center
	gluLookAt(WWA[0], WWA[1], WWA[2],     sin((float)yaw), WWLA[1], -cos((float)yaw),     0, 1, 0); //Where we are, What we look at, and which way is up

<Lighting and drawing>
what happens is the cube that is drawn and seems to swing back and forth (though this may be the camera moving). Can someone tell me where i've gone wrong?
Advertisement
I don't know what WWLA is, but I'm assuming WWA is the camera position. If so, you might try this:
gluLookAt(    WWA[0],    WWA[1],    WWA[2],    WWA[0] + sin((float)yaw),    WWA[1],    WWA[2] - cos((float)yaw),    0,    1,    0);
Simple solution - use glRotate*() to rotate the camera.
so:

float yaw = 0;
void DrawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Clear The Screen And The Depth Buffer
glLoadIdentity();// load Identity Matrix

yaw+=0.01;

//set camera looking down the -z axis, 6 units away from the center
glRotatef(yaw, 0.0f, 1.0f, 0.0f); //apply yaw rotation.
Quote:Original post by Degra
Simple solution - use glRotate*() to rotate the camera.
so:

float yaw = 0;
void DrawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Clear The Screen And The Depth Buffer
glLoadIdentity();// load Identity Matrix

yaw+=0.01;

//set camera looking down the -z axis, 6 units away from the center
glRotatef(yaw, 0.0f, 1.0f, 0.0f); //apply yaw rotation.
The above example has a couple of problems, such as that it doesn't take position into account. Here's a modified version:
glLoadIdentity();yaw+=0.01;glRotatef(-yaw, 0.0f, 1.0f, 0.0f);glTranslatef(-camx, -camy, -camz);
Also, don't forget to convert from radians to degrees if needed.
thanks for the replies guys...i changed the LookAt function to:

gluLookAt(WWA[0], WWA[1], WWA[2], WWA[0] - sin((float)pitch), WWA[1] - sin((float)yaw), WWA[2] - cos((float)yaw), 0, 1, 0); //Where we are, What we look at, and which way is up

WWA is a 3 element array (Where We're At). This code kinda works...though it doesn't rotate properly all the time. I think the problem may be 'Gimbal Lock'? Is there a tutorial/explanation on how to solve the problem (if it is indeed this)?
Quote:Original post by Winegums
thanks for the replies guys...i changed the LookAt function to:

gluLookAt(WWA[0], WWA[1], WWA[2], WWA[0] - sin((float)pitch), WWA[1] - sin((float)yaw), WWA[2] - cos((float)yaw), 0, 1, 0); //Where we are, What we look at, and which way is up

WWA is a 3 element array (Where We're At). This code kinda works...though it doesn't rotate properly all the time. I think the problem may be 'Gimbal Lock'? Is there a tutorial/explanation on how to solve the problem (if it is indeed this)?
Hehe, I think 'WWA' for 'where we're at' gets the prize for most obscure variable name :-) Anyway, to get the effect I'm guessing you're after, you just need to handle the trig a little differently. I could give you an example, but first I'd need to know which axis is considered to be up in your simulation (it's probably either y or z, but it's not clear from looking at your example).
Y is up...i though the "which way is up" bit showed this?

and yeah i guess WWA is quite obscure...i have a habit of naming things for myself... :/
Quote:Original post by Winegums
Y is up...i though the "which way is up" bit showed this?
It suggests it, but then other things in your code seem to contradict it.

Anyway, here's something else you can try:
float sy = sin(yaw);float cy = cos(yaw);float sp = sin(pitch);float cp = cos(pitch);gluLookAt(    WWA[0],    WWA[1],    WWA[2],    WWA[0] + sy * cp,    WWA[1] - sp,    WWA[2] + cy * cp,    0,    1,    0);
that doesn't seem to do it either, could it be the code i use to find the pitch and yaw?

pitch += (CompareValues(MousePos.oldx, MousePos.x)/100);yaw -= (CompareValues(MousePos.oldy,MousePos.y)/100);


compare values subtracts one from the other and returns the answer

EDIT: Swapped pitch and yaw and i think it works. thanks for your help :)

EDIT 2:

Ok new problem...trying to move the camera in the direction its facing. I thought to use the values from the look at coordinates to translate to, but that doesn't work. Is there a set method for this?

[Edited by - Winegums on April 5, 2006 6:02:52 PM]

This topic is closed to new replies.

Advertisement