如何旋转然后在那个方向移动? [英] How to rotate and then move on that direction?

查看:196
本文介绍了如何旋转然后在那个方向移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hy,我目前正试图做一个第一人称game.what我能做的是使相机移动使用函数gluLookAt(),并使用glRotatef()旋转它。我想尝试是旋转相机,然后在我已经旋转的方向上向前移动,但轴保持不变,虽然我已经旋转相机移动侧向不向前。有人可以帮助我吗?这是我的代码:

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.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);
}



假设你有一个方向向量定义为{CameraPhi,CameraTheta,0.0}那么如果你想沿着矢量v相对于摄像机轴的方向移动摄像机,则将其添加到摄像机的位置p:

Assuming you have an orientation vector defined as {CameraP 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);


Keep Coding:)

And that should do it. Keep Coding :)

这篇关于如何旋转然后在那个方向移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆