我的弹丸更新功能有什么问题?
What's wrong with my projectile update function?
以下是所有相关的代码。
当射弹初始化时,它就会运行:
slope = (yTarget - yPos) / (xTarget - xPos);
if (xTarget >= xPos)
xDir = 1;
else
xDir = -1;
if (yTarget >= yPos)
yDir = 1;
else
yDir = -1;
每次更新都会运行,每次游戏循环都会发生:
xPos += t*speed*xDir;
yPos += t*speed*yDir * abs(slope);
XTarget和yTarget是投射物应该去的地方,xPos和yPos是投射物当前所在的地方。速度目前为1,所以忽略它,t是自上次更新以来经过的节拍数(ms)。(在我的电脑上通常是0-2)一切都很好,只是子弹的速度似乎取决于(xTarget-xPos)离0的距离,炮弹越近就越快。我会试着直观地解释一下。如果我向角色的右侧或左侧射击,子弹会以所需的速度移动。然而,如果我在角色上方或下方拍摄,它的拍摄速度会非常快。有人能告诉我一个解决这个问题的方法,或者一个更好的编码方法吗?谢谢
dx = xTarget - xPos;
dy = yTarget - yPos;
norm = sqrt(dx*dx + dy*dy);
if (norm != 0) {
dx /= norm;
dy /= norm;
}
稍后:
xPos += t*speed*dx;
yPos += t*speed*dy;
相关文章:
- 警告处理为错误这里有什么问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 违反const正确性:我应该现实地期待什么问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 我的逻辑反转字符串中的元音有什么问题?
- 需要以下代码的帮助,下面的代码有什么问题
- 常量公共成员有什么问题?
- 以下代码中的函数模板有什么问题?
- 这个返回元素位置的基于循环的函数有什么问题?
- creat_list2功能有什么问题?
- 格式说明符C++有什么问题
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 从 argv[1] 转换为字符 * 字符串后有什么问题?
- 我的堆栈和库存清单程序的结构有什么问题?
- 此工厂功能有什么问题?
- 以下 C++ 代码有什么问题?
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- reinterpret_cast,只读访问,简单的可复制类型,会出什么问题?
- 它解决了什么问题,对于非真空初始化,生命周期在初始化之前就开始了