如何实现FPS相机?

时间:2022-05-27 08:08:54

So I'm currently working on some FPS game programming in OpenGL (JOGL, more specifically) just for fun and I wanted to know what would be the recommended way to create an FPS-like camera?

因此,我目前正在OpenGL (JOGL,更具体地说)开发一些FPS游戏编程,只是为了好玩,我想知道创建FPS类相机的推荐方式是什么?

At the moment I basically have a vector for the direction the player is facing, which will be added to the current player position upon pressing the "w" or forward key. The negative of that vector is of course used for the "s" or backward key. For "a", left, and "d", right I use the normal of the direction vector. (I am aware that this would let the player fly, but that is not a problem at the moment)

目前,我基本上有一个方向球员所面对的方向的矢量,在按下“w”或向前键时,它将被添加到当前的播放器位置。这个向量的负数当然是用来表示“s”或“后向键”的。对于a,左边和d,我用方向向量的法向量。(我知道这会让玩家飞起来,但现在这不是问题)

Upon moving the mouse, the direction vector will be rotated using trigonometry and matrices. All vectors are, of course, normalized for easy speed control.

在移动鼠标时,方向向量将使用三角和矩阵旋转。当然,为了便于速度控制,所有的向量都是归一化的。

Is this the common and/or good way or is there an easier/better way?

这是常见的和/或好方法,还是有更简单/更好的方法?

4 个解决方案

#1


6  

The way I have always seen it done is using two angles, yaw and pitch. The two axes of mouse movement correspond to changes in these angles.

我经常看到的方法是用两个角度,偏航和俯仰。鼠标移动的两个轴对应于这些角度的变化。

You can calculate the forward vector easily with a spherical-to-rectangular coordinate transformation. (pitch=latitude=φ, yaw=longitude=θ)

你可以用一个球对直角坐标变换很容易地计算出前向量。(距= =φ纬度,经度偏航= =θ)

You can use a fixed up vector (say (0,0,1)) but this means you can't look directly upwards or downwards. (Most games solve this by allowing you to look no steeper than 89.999 degrees.)

你可以用一个固定的向上的向量(比如(0,1))但是这意味着你不能直接向上或向下看。(大多数游戏解决这个问题的方法是,让你看上去不会比89.999度陡峭。)

The right vector is then the cross product of the forward and up vectors. It will always be parallel to the ground plane since the up vector is always perpendicular to the ground plane.

正确的向量就是前向量和上向量的外积。它总是平行于地面,因为向上的矢量总是垂直于地面。

Left/right strafe keys then use the +/-right vector. For a forward vector parallel to the ground plane, you can take the cross product of the right and the up vectors.

左/右strafe键然后使用+/-右向量。对于一个与地面平行的前向矢量,你可以取右边的外积和上积。

As for the GL part, you can simply use gluLookAt() using the player's origin, the origin plus the forward vector and the up vector.

至于GL部分,你可以简单地使用gluLookAt()使用播放器的原点,原点加上前进矢量和向上的矢量。

Oh and please, please add an "invert mouse" option.

哦,拜托,请添加一个“倒置鼠标”选项。

Edit: Here's an alternative solution which gets rid of the 89.9 problem, asked in another question, which involves building the right vector first (with no pitch information) and then forward and up.

编辑:这里有一个替代的解决方案可以解决89.9的问题,在另一个问题中,这个问题涉及到先构建正确的向量(没有音高信息),然后再向前向上。

#2


4  

Yes, thats essentially the way I have always seen it done.

是的,这就是我一直看到的。

#3


1  

Yeah, but in the end you will want to add various other attributes to the camera. To spell it n00b: keep it tidy if you want to mimic Quake or CS. In the end might have bobing, FoV, motion filtering, network lag suspension and more.

是的,但是最终你会想要给相机添加不同的属性。拼写为n00b:如果你想模仿地震或CS,请保持整洁。最后可能会有bobing, FoV,运动滤波,网络滞后暂停等。

Cameras are actually one of the more difficult parts to make in a good game. That's why developers usually are content with a seriously dull, fixed 1st/3rd person ditto.

相机实际上是一款好游戏中最难制作的部分之一。这就是为什么开发人员通常满足于一种非常乏味的、固定的第一/第三人称同义。

#4


1  

You could use Quaternions for your camera rotation. Although I have not tried it myself, they are useful for avoiding gimbal lock.

你可以用四元数来旋转你的相机。虽然我自己还没有尝试过,但它们对于避免万向节锁是有用的。

#1


6  

The way I have always seen it done is using two angles, yaw and pitch. The two axes of mouse movement correspond to changes in these angles.

我经常看到的方法是用两个角度,偏航和俯仰。鼠标移动的两个轴对应于这些角度的变化。

You can calculate the forward vector easily with a spherical-to-rectangular coordinate transformation. (pitch=latitude=φ, yaw=longitude=θ)

你可以用一个球对直角坐标变换很容易地计算出前向量。(距= =φ纬度,经度偏航= =θ)

You can use a fixed up vector (say (0,0,1)) but this means you can't look directly upwards or downwards. (Most games solve this by allowing you to look no steeper than 89.999 degrees.)

你可以用一个固定的向上的向量(比如(0,1))但是这意味着你不能直接向上或向下看。(大多数游戏解决这个问题的方法是,让你看上去不会比89.999度陡峭。)

The right vector is then the cross product of the forward and up vectors. It will always be parallel to the ground plane since the up vector is always perpendicular to the ground plane.

正确的向量就是前向量和上向量的外积。它总是平行于地面,因为向上的矢量总是垂直于地面。

Left/right strafe keys then use the +/-right vector. For a forward vector parallel to the ground plane, you can take the cross product of the right and the up vectors.

左/右strafe键然后使用+/-右向量。对于一个与地面平行的前向矢量,你可以取右边的外积和上积。

As for the GL part, you can simply use gluLookAt() using the player's origin, the origin plus the forward vector and the up vector.

至于GL部分,你可以简单地使用gluLookAt()使用播放器的原点,原点加上前进矢量和向上的矢量。

Oh and please, please add an "invert mouse" option.

哦,拜托,请添加一个“倒置鼠标”选项。

Edit: Here's an alternative solution which gets rid of the 89.9 problem, asked in another question, which involves building the right vector first (with no pitch information) and then forward and up.

编辑:这里有一个替代的解决方案可以解决89.9的问题,在另一个问题中,这个问题涉及到先构建正确的向量(没有音高信息),然后再向前向上。

#2


4  

Yes, thats essentially the way I have always seen it done.

是的,这就是我一直看到的。

#3


1  

Yeah, but in the end you will want to add various other attributes to the camera. To spell it n00b: keep it tidy if you want to mimic Quake or CS. In the end might have bobing, FoV, motion filtering, network lag suspension and more.

是的,但是最终你会想要给相机添加不同的属性。拼写为n00b:如果你想模仿地震或CS,请保持整洁。最后可能会有bobing, FoV,运动滤波,网络滞后暂停等。

Cameras are actually one of the more difficult parts to make in a good game. That's why developers usually are content with a seriously dull, fixed 1st/3rd person ditto.

相机实际上是一款好游戏中最难制作的部分之一。这就是为什么开发人员通常满足于一种非常乏味的、固定的第一/第三人称同义。

#4


1  

You could use Quaternions for your camera rotation. Although I have not tried it myself, they are useful for avoiding gimbal lock.

你可以用四元数来旋转你的相机。虽然我自己还没有尝试过,但它们对于避免万向节锁是有用的。