实施扫射 - Opengl - 相机
我创建了一个相机类。可以用鼠标进行 360 度旋转以及上下移动。正如我的意图,就像所有游戏一样。也可以像所有游戏一样向前和向后移动。但我不知道如何实现向左和向右移动。
我执行以下操作:
每帧都会调用此函数:
gluLookAt(_posX , _posY , _posZ,
_viewX, _viewY, _viewZ,
_upX, _upY, _upZ );
我的移动函数
不起作用:
void Camera::moveLeft()
{
float rot= (_viewY / 180 * PI);
_moveX -= float(cos(rot)) * 0.5;
_moveZ -= float(sin(rot)) * 0.5;
}
起作用
在场景中向前移动:
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;
}
当我仅用鼠标移动时,没有问题。但是,如果我使用此功能并用鼠标旋转,我会得到一些奇怪的相机移动,
有什么想法可以解决这个问题吗?
谢谢
@edit,
所以我删除了 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;
}
我显然做错了什么,因为向左和向右的运动“更好”,但仍然不是你所期望的。
I created a camera class. It is possible to rotate 360 degrees with my mouse and move up and down. Just as I intended, just like in all games. It is also possible to move forward and backward just like in all games. But I don't know how to implement moving to the left and right.
I do the following:
This gets called every frame:
gluLookAt(_posX , _posY , _posZ,
_viewX, _viewY, _viewZ,
_upX, _upY, _upZ );
My move function
Doesnt Work:
void Camera::moveLeft()
{
float rot= (_viewY / 180 * PI);
_moveX -= float(cos(rot)) * 0.5;
_moveZ -= float(sin(rot)) * 0.5;
}
Does Work
move forwards in the scene:
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;
}
When I move with my mouse only, there is no problem. But if I use this function and rotate with my mouse, I get some strange camera movements
Any ideas of how to solve this?
Thank you
@edit
So I removed the glTranslated statement and I changed my moveLeft function to the following:
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;
}
I am clearly doing something wrong because the movements to left and right are "better", but still not what you would expect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要获得与包含向上和视图向量的平面成 90 度的向量,您需要进行叉积: http://en.wikipedia.org/wiki/Cross_product。任何像样的向量数学库都会有一个函数。
生成的向量将是左向量或右向量(尝试一下并找出哪个),然后将其添加到适当的位置。
请注意,如果您的视图向量与向上向量的方向完全相同,则此方法将不起作用。
编辑:根据您编辑的问题,我认为您需要这样做:
您需要获取视图方向向量并使用它而不是叉积中的视图向量,然后添加到两个位置和视图向量:
To get a vector which points at 90 degrees to the plane that contains your up and view vectors, you need to do a cross product: http://en.wikipedia.org/wiki/Cross_product. Any decent vector maths library will have a function for this.
The resulting vector will either be a left vector or a right vector (try it out and find out which) and then add it to your position as appropriate.
Note that this won't work if your view vector is in exactly the same direction as your up vector.
Edit: Based on your edited question I think you need to do this:
You need to get the view direction vector and use that instead of your view vector in the cross product, and then add to both the position and view vector: