开发者

How to rotate and then move on that direction?

开发者 https://www.devze.com 2023-01-26 22:32 出处:网络
Hy, I am currently trying to make a first person game.what i was able to do was to make the camera move using the function gluLookAt(), and to rotate it using glRotatef().What I am trying to to is to

Hy, I am currently trying to make a first person game.what i was able to do was to make the camera move using the function gluLookAt(), and to rotate it using glRotatef().What I am trying to to is to rotate the camera and then move forward on the direction i have rotated on, but the axes stay the same,and although i have rotated the camera moves sideways not forward. Can someone help me ? this is my code:

glMatrixMode(GL_MODELVIEW);   
glLoadIdentity();   
glRotatef(cameraPhi,1,0,0);   
glRotatef(cameraTheta,0,1,0);
gluLookAt(move_camera.x,move_camera.y,move_camera.z,move_camera.x,move_camera.开发者_JAVA技巧y,move_camera.z-10,0,1,0);
drawSkybox2d(treeTexture);


This requires a bit of vector math...

Given these functions, the operation is pretty simple though:

vec rotx(vec v, double a)
{
    return vec(v.x, v.y*cos(a) - v.z*sin(a), v.y*sin(a) + v.z*cos(a));
}

vec roty(vec v, double a)
{
    return vec(v.x*cos(a) + v.z*sin(a), v.y, -v.x*sin(a) + v.z*cos(a));
}

vec rotz(vec v, double a)
{
    return vec(v.x*cos(a) - v.y*sin(a), v.x*sin(a) + v.y*cos(a), v.z);
}

Assuming you have an orientation vector defined as {CameraPhi, CameraTheta, 0.0}, then if you want to move the camera in the direction of a vector v with respect to the camera's axis, you add this to the camera's position p:

p += v.x*roty(rotx(vec(1.0, 0.0, 0.0), CameraPhi), CameraTheta) +
     v.y*roty(rotx(vec(0.0, 1.0, 0.0), CameraPhi), CameraTheta) +
     v.z*roty(rotx(vec(0.0, 0.0, 1.0), CameraPhi), CameraTheta);

And that should do it. Keep Coding :)


gluLookAt is defined as follows:

void gluLookAt(GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ,
               GLdouble centerX, GLdouble centerY, GLdouble centerZ,
               GLdouble upX, GLdouble upY, GLdouble upZ
              );

The camera is located at the eye position and looking in the direction from the eye to the center.

eye and center together define the axis (direction) of the camera, and the third vector up defines the rotation about this axis.

You don't need the separate phi and theta rotations, just pass in the correct up vector to get the desired rotation. (0,1,0) means the camera is upright, (0,-1,0) means the camera is upside-down and other vectors define intermediate positions.

0

精彩评论

暂无评论...
验证码 换一张
取 消