gpt4 book ai didi

game-physics - 在 3D 游戏中处理扫射/侧步

转载 作者:行者123 更新时间:2023-12-04 12:12:11 25 4
gpt4 key购买 nike

我对游戏开发很陌生,并且成功地对 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);
}

其中 Vector3 是一个简单的 X、Y、Z 结构,弧度的度数是:
inline float degreesToRadians(const float degrees) {
const float PIOver180 = 3.14159f / 180.0f;
return degrees * PIOver180;
}

为了处理向前移动,我只需在参数中放置一个正浮点数,向后移动一个负浮点数。

这当然允许我向前和向后移动 - 我也可以通过更改 pos.y 向上移动。但是我不知道如何侧步/左右扫射。

翻转 xz如果偏航角为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com