如何实现FPS相机? [英] How would one implement an FPS camera?

查看:139
本文介绍了如何实现FPS相机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我目前正在玩一些OpenGL(尤其是JOGL)的FPS游戏编程,只是为了好玩,我想知道推荐什么方式来创建类似FPS的相机?

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?

目前,我基本上已经有了一个面向玩家所面对方向的矢量,在按下"w"或前进键时,该矢量将被添加到当前玩家位置.该向量的负数当然用于"s"或后退键.对于"a",左"和"d",右我使用方向矢量的法线. (我知道这会让玩家飞行,但是目前这不是问题)

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)

在移动鼠标时,将使用三角函数和矩阵旋转方向矢量.当然,对所有矢量都进行了归一化以便于速度控制.

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?

推荐答案

I 一直看到的方法是使用两个角度,即偏航角和俯仰角.鼠标移动的两个轴对应于这些角度的变化.

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.

您可以使用球到矩形坐标转换轻松地计算forward向量. (pitch = latitude =φ,yaw =经度=θ)

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

您可以使用固定的up向量(例如(0,0,1)),但这意味着您不能直接向上或向下看. (大多数游戏通过让您看起来不超过89.999度来解决此问题.)

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

right向量是正向向量和向上向量的叉积.由于向上矢量始终垂直于接地平面,因此它将始终与接地平面平行.

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.

左/右strafe键,然后使用+/-right向量.对于平行于地平面的forward向量,可以取rightup向量的叉积.

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.

对于GL部分,您只需使用播放器的原点,原点加上forward向量和up向量的gluLookAt()即可.

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.

哦,请添加一个反转鼠标"选项.

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

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.

这篇关于如何实现FPS相机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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