我对游戏开发很陌生并成功建模了3D地形,并且拥有一个能够在游戏世界中自由移动的相机,但是我遇到了一个我无法弄清楚的障碍.
目前,为了向前/向后移动,我有如下方法:
void Player::moveForward(const float speed)
{
Vector3 pos = getPosition();
float cosYaw = cosf(degreesToRadians(m_yaw));
float sinYaw = sinf(degreesToRadians(m_yaw));
pos.x += float(cosYaw)*speed;
pos.z += float(sinYaw)*speed;
setPosition(pos);
}
Run Code Online (Sandbox Code Playgroud)
Vector3是一个简单的X,Y,Z结构,而弧度是:
inline float degreesToRadians(const float degrees) {
const float PIOver180 = 3.14159f / 180.0f;
return degrees * PIOver180;
}
Run Code Online (Sandbox Code Playgroud)
为了处理前进,我只需在参数中放置一个正浮点数,然后向后移动一个负浮点数.
这让我当然可以向前和向后移动 - 我也可以通过改变pos.y位置来向上移动但是我无法弄清楚如何左右移动步/拉.
如果偏航是360/0,270,180或90,则翻转x并z导致我能够侧步,但是在它们之间的任何角度都会使摄像机向前/向后退步或向前/向后移动.
无论如何,这一直困扰着我,所以我试图在纸上找到解决方案 - 这有效:
inline float degreesToStrafe(const float degrees)
{
const float PIOver90 = 3.14159f / 90.0f;
return degrees * PIOver90;
}
Run Code Online (Sandbox Code Playgroud)
但是,在游戏中,这与交换x和z结果完全相同.
任何帮助将不胜感激; 我努力在网上找到关于此的任何内容但是我可能只是错误地搜索了!
我认为你不能只交换x和z结果.考虑cosYaw == sinYaw的情况:翻转它们将导致与未折叠相同的方向,这显然是不需要的行为,因为您试图将向量移动90度,而不是朝同一方向移动.
相反,你想要为MoveForward做同样的事情,先将你的偏航旋转90度.您基本上将已知的合成速度分解为x和z分量,您可以这样做,因为您知道角度.如果你考虑前进和扫射之间的角度差异,它是90度,所以只需减去或加上你的偏航90度,然后使用相同的数学.
void Player::strafe(const float speed)
{
Vector3 pos = getPosition();
float cosYaw = cosf(degreesToRadians(m_yaw - 90.f));
float sinYaw = sinf(degreesToRadians(m_yaw - 90.f));
pos.x += float(cosYaw)*speed;
pos.z += float(sinYaw)*speed;
setPosition(pos);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
703 次 |
| 最近记录: |