作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对游戏开发很陌生,并且成功地对 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);
}
inline float degreesToRadians(const float degrees) {
const float PIOver180 = 3.14159f / 180.0f;
return degrees * PIOver180;
}
pos.y
向上移动。但是我不知道如何侧步/左右扫射。
x
和
z
如果偏航角为 360/0、270、180 或 90,我就可以侧步,但是它们之间的任何角度都会导致相机向前侧步/向后侧步或向前/向后移动。
inline float degreesToStrafe(const float degrees)
{
const float PIOver90 = 3.14159f / 90.0f;
return degrees * PIOver90;
}
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);
}
关于game-physics - 在 3D 游戏中处理扫射/侧步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8522202/
我是一名优秀的程序员,十分优秀!