我创建了一个相机类.可以用我的鼠标旋转360度并上下移动.正如我的意图,就像在所有游戏中一样.也可以像所有游戏一样前进和后退.但我不知道如何实现向左和向右移动.
我做以下事情:
每帧调用一次:
gluLookAt(_posX , _posY , _posZ,
_viewX, _viewY, _viewZ,
_upX, _upY, _upZ );
Run Code Online (Sandbox Code Playgroud)
我的移动功能
不起作用:
void Camera::moveLeft()
{
float rot= (_viewY / 180 * PI);
_moveX -= float(cos(rot)) * 0.5;
_moveZ -= float(sin(rot)) * 0.5;
}
Run Code Online (Sandbox Code Playgroud)
工作是否
在场景中向前移动:
void Camera::moveForward()
{
float viewX = _viewX - _posX;
float viewY = _viewY - _posY;
float viewZ = _viewZ - _posZ;
_posX += viewX * speed
_posY += viewY * speed;
_posZ += viewZ * speed;
_viewX += viewX * speed;
_viewY += viewY * speed;
_viewZ += viewZ * speed;
}
Run Code Online (Sandbox Code Playgroud)
当我只用鼠标移动时,没有问题.但是,如果我使用此功能并使用鼠标旋转,我会得到一些奇怪的相机移动
有关如何解决这个问题的任何想法?
谢谢
@编辑
所以我删除了glTranslated语句,并将moveLeft函数更改为以下内容:
void Camera::moveLeft(){
float x = ((_viewY * _upZ) - (_viewZ * _upY));
float y = ((_viewZ * _upX) - (_viewX * _upZ));
float z = ((_viewX * _upY) - (_viewY * _upX));
float magnitude = sqrt( (x * x) + (y * y) + (z * z) );
x /= magnitude;
y /= magnitude;
z /= magnitude;
_posX -= x;
_posY -= y;
_posZ -= z;
Run Code Online (Sandbox Code Playgroud)
}
void Camera::moveLeft(){
gluLookAt(_posX , _posY , _posZ,
_viewX, _viewY, _viewZ,
_upX, _upY, _upZ );
Run Code Online (Sandbox Code Playgroud)
}
void Camera::moveLeft(){
void Camera::moveLeft()
{
float rot= (_viewY / 180 * PI);
_moveX -= float(cos(rot)) * 0.5;
_moveZ -= float(sin(rot)) * 0.5;
}
Run Code Online (Sandbox Code Playgroud)
}
我显然做错了,因为向左和向右的动作"更好",但仍然不是你所期望的.
要获得一个与包含向上和视图向量的平面成90度的向量,您需要进行交叉产品:http://en.wikipedia.org/wiki/Cross_product.任何体面的矢量数学库都有这个功能.
生成的矢量将是左矢量或右矢量(尝试并找出哪个),然后根据需要将其添加到您的位置.
请注意,如果您的视图向量与向上向量的方向完全相同,则此方法无效.
编辑:根据您编辑的问题,我认为您需要这样做:
您需要获取视图方向向量并在十字产品中使用它而不是视图向量,然后添加到位置和视图向量:
void Camera::moveLeft()
{
float viewX = _viewX - _posX;
float viewY = _viewY - _posY;
float viewZ = _viewZ - _posZ;
float x = ((viewY * _upZ) - (viewZ * _upY));
float y = ((viewZ * _upX) - (viewX * _upZ));
float z = ((viewX * _upY) - (viewY * _upX));
float magnitude = sqrt( (x * x) + (y * y) + (z * z) );
x /= magnitude;
y /= magnitude;
z /= magnitude;
_posX -= x;
_posY -= y;
_posZ -= z;
_viewX -= x;
_viewY -= y;
_viewZ -= z;
}
Run Code Online (Sandbox Code Playgroud)