Help me withe the camera please...
I have made a program in witch i want to move my camera like the one in any FPP game. I create a map, lets say it is a square. But the first vertex of the square is at (0,0) and the last is at (64,64)...ahh its something like
-Z ^ -------- B(-64,-64)
| | |
| | |
| | |
|A --------
--------------> +X
(0,0)
A = (0,0)
i hope you know what i mean... And now i want to rotate my camera, but i dont want to use gluLookAt, only glRotate and glTranslate. I firts do the translations and rotating and then draw my map. And when i want to rotate, i dont truly rotate the camera, but i rotate the whole scene. The problem is that my map rotates along the Y axis starting at point(0,0), and that means it looks awful and it doesnt look like in FPP games. I want it to rotate along the center of the map, not along the (0.0) point. Is this possible to achieve?? Ahhh sorry for my english :)
Ohhh shit the picture didn`t work :) But i guess you know what i mean... The map start point is at (0,0) and the top right corner is at -64,-64 on Z axis.. it lies on the XZ ahmm.. surface?
If I assume you're talking about a first person shooter type movement, where you walk and strafe around, have the ability to turn left/right and look up/down, you could do something like this:
That help?
float camX = 0.0f, camY = 0.0f, camZ = 0.0f;float camHeading = 0.0f, camPitch = 0.0f;// If you want to move forward by a distance d, then:float tempX, tempY, tempZ;tempX = -sin(camHeading) * d; // remember deg->rad convert!tempY = 0.0f;tempZ = -cos(camHeading) * d;camX += tempX;camY += tempY;camZ += tempZ;// moving backwards would be the same but with d as a negative// value// increasing heading and pitch angles will make you look left// and up, respectively. decreasing will go the other way// Then in your draw function:glMatrixMode(GL_MODELVIEW); // you probably (or should) haveglLoadIdentity(); // these already// This order matters:glRotatef(-camPitch, 1.0f, 0.0f, 0.0f);glRotatef(-camHeading, 0.0f, 1.0f, 0.0f);glTranslatef(-camX, -camY, -camZ);// draw stuff
That help?
Thanks chawk you dont realize how you helped me!!! But i have annother problem because everything works fine, but when i rotate down 90.0f and i want to walk DOWN, it walks down and front!!! I will paste you some code. angleh is the angle on the XZ surface (is this called a surface or somehow else?)(heading?) and angle v is on the ZY surface (Pitch?). So please hepl beacuse I dont know what to do. Here s the code ....
void camera::moveForward()
{
float t_x, t_y,t_z;
t_x = (float)sin(kamera.angleh*3.141592/180.0f);
t_z = (float)-cos(kamera.angleh*3.141592/180.0f);
t_y = (float)sin(kamera.anglev*3.141592/180.0f);
kamera.x += t_x;
kamera.z += t_z;
kamera.y += t_y;
};
when i move backwards I but -= . Please help :) I guess that code will tell you something if not please tell so.
void camera::moveForward()
{
float t_x, t_y,t_z;
t_x = (float)sin(kamera.angleh*3.141592/180.0f);
t_z = (float)-cos(kamera.angleh*3.141592/180.0f);
t_y = (float)sin(kamera.anglev*3.141592/180.0f);
kamera.x += t_x;
kamera.z += t_z;
kamera.y += t_y;
};
when i move backwards I but -= . Please help :) I guess that code will tell you something if not please tell so.
OK i have it but here comes another problem. So I`ve figured it out how to move along the Y axis. I have something like this:
void camera::moveForward()
{
float t_x,t_z ,t_y;
t_x = (float)-sin(kamera.angleh*3.141592/180.0f) ;
t_z = (float)-cos(kamera.angleh*3.141592f/180.0f);
t_y = (float)sin(kamera.anglev*3.141592/180.0f);
if(cos(kamera.anglev*3.141592f/180.0f) < 0)
{
kamera.x -= t_x;
kamera.z -= t_z;
}
else
{
kamera.x += t_x;
kamera.z += t_z;
};
kamera.y += t_y;
};
but the problem is that even when i look down and the pitch is -90 deg. the camera is going down and forward at the same time, and i would like it to go only down when i look exactly at -90 deg. I think i could make a variable DELTA_Z and multiply t_z by DELTA_Z. DELTA_Z would have to decrease everytime the pitch decreases from 0 to -90 so that if pitch is 0 delta_Z is 1 and when its -90, delta_Z is 0 and when pitch is between 0 and -90 the pitch is between 1 and 0. My only problem is that it would have to change everytmie the pitch decreases or increases... I hope you know what i mean... but i dont know how to do it...
void camera::moveForward()
{
float t_x,t_z ,t_y;
t_x = (float)-sin(kamera.angleh*3.141592/180.0f) ;
t_z = (float)-cos(kamera.angleh*3.141592f/180.0f);
t_y = (float)sin(kamera.anglev*3.141592/180.0f);
if(cos(kamera.anglev*3.141592f/180.0f) < 0)
{
kamera.x -= t_x;
kamera.z -= t_z;
}
else
{
kamera.x += t_x;
kamera.z += t_z;
};
kamera.y += t_y;
};
but the problem is that even when i look down and the pitch is -90 deg. the camera is going down and forward at the same time, and i would like it to go only down when i look exactly at -90 deg. I think i could make a variable DELTA_Z and multiply t_z by DELTA_Z. DELTA_Z would have to decrease everytime the pitch decreases from 0 to -90 so that if pitch is 0 delta_Z is 1 and when its -90, delta_Z is 0 and when pitch is between 0 and -90 the pitch is between 1 and 0. My only problem is that it would have to change everytmie the pitch decreases or increases... I hope you know what i mean... but i dont know how to do it...
Hey i made it! I have this delta variable and i multiply t_x and t_z by it. Everything works fine and delta is just cos() of the pitch :) Thanks everyone for help
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement