PDA

View Full Version : trouble moving "camera"



MicahN
09-02-2010, 01:54 PM
Hello

i have been googling and looking for a while and i don't know if im just dumb and not thinking right or am doing something wrong.

i have some experience with 3d graphics and other 3d engines(ogre, irrlicht and others i can't remember right now) but this is my first time working with opengl directly(well through sdl anyways), this seems like a very simple problem but for some reason i just can't get it to work right. even drawing it out on paper doesn't seem to work when i put whats on paper in to code.

so here is the problem.
i have my camera set up at 0,0,0 looking directly ahead with the plane right under me. i hit "w" on my keyboard and the plane moves towards me. then i turn the camera 90 degrees to the right hit "w" and now the plane moves to the right. instead of towards me.

here is the code im using for updating the coordinates.


void Camera::AddX(float X)
{
x += X * float(cos(yrot));
printf("%f",x);
}
void Camera::AddY(float Y)
{
y += Y;// * float(cos(xrot)); //Commented out the trig for the y axis until i get the rest worked out
printf("%f",y);
}
void Camera::AddZ(float Z)
{
z += Z * float(sin(yrot));
printf("%f",z);
}



theCam.AddYRot(Event->motion.xrel * mouseSensitivity * 3.14159265f / 180);
theCam.AddXRot(Event->motion.yrel * mouseSensitivity * 3.14159265f / 180);


and here is my render code.


glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// glPushMatrix();
glRotatef(theCam.GetXRot(),1,0,0);
glRotatef(theCam.GetYRot(),0,1,0);
glRotatef(theCam.GetZRot(),0,0,1);
glTranslatef(-theCam.GetX(),-theCam.GetY(),-theCam.GetZ());
glColor3f(.3,.3,.3);
glBegin(GL_QUADS);
glVertex3f( -5,0, -5);
glVertex3f( -5,0,5);
glVertex3f(5,0,5);
glVertex3f(5,0, -5);
glEnd();
// glPopMatrix();
SDL_GL_SwapBuffers();

i have been able to make it work once before however instead of rotating the "camera" it would rotate the object...as if i was orbiting around the object instead of sitting on top and moving my head. so as soon as i moved off of 0,0,0 then everything would be rotating weird. but since it was a accident im not sure how to do it again.

Thanks in advance,

Micah

thinks
09-03-2010, 01:07 AM
Assuming that the code you show is applied to the modelview matrix, how are you setting up the projection matrix?

Perhaps you are getting the view directions confused? Try doing a search for modelview matrices, and you will find how to extract the view direction from such a matrix. This will allow you to verify that the camera is actually pointed where you think it is.



glLoadIdentity();
glRotatef(theCam.GetXRot(),1,0,0);
glRotatef(theCam.GetYRot(),0,1,0);
glRotatef(theCam.GetZRot(),0,0,1);
glTranslatef(-theCam.GetX(),-theCam.GetY(),-theCam.GetZ());

Looks correct to me, which suggests that there is a problem with the way you set the values in the camera.