所以我在让我的放置者面向行星时遇到了困难。我有玩家和星球之间的角度,我也有玩家当前所在的角度,现在我想要做的是让我的玩家面对星球,但有增量变化。 (我这样做是因为我希望我的放置者能够绕行星运行)
问题在于数学,我增加玩家旋转以匹配玩家和行星之间的角度,但是因为角度在 0 到 360 之间工作,所以我的玩家不会绕轨道运行,因为玩家旋转可能是 2,但与行星的角度是280 所以游戏会让玩家转身,抱歉解释不好。
有人知道如何让我的玩家成功绕我的星球运行吗?
这是我的代码:
double rotation = Math.toDegrees(Math.atan2(currentPlanet.pos[1]-currentPlayer.pos[1], currentPlanet.pos[0]-currentPlayer.pos[0]));
if(rotation < 0)
{
rotation += 360;
}
if(currentPlayer.rotation < rotation)
{
currentPlayer.rotation += 0.15*delta;
}
if(currentPlayer.rotation > rotation)
{
currentPlayer.rotation -= 0.15*delta;
}
最佳答案
问题是 350° 也是 -10°。您想要较小的绝对值。
解决方案非常简单。使用模运算将角度转换到正确的范围内。
/* returns angle x represented in range -180.0 ... 180.0 */
double clampAngle(double x) {
return (x%360.0+360.0+180.0)%360.0-180.0;
}
将角度差传递给此函数。结果的符号会告诉您应该转向哪个方向:
double rotation = Math.toDegrees(Math.atan2(currentPlanet.pos[1]-currentPlayer.pos[1], currentPlanet.pos[0]-currentPlayer.pos[0]));
double diff = ((rotation-currentPlayer.rotation)%360.0+360.0+180.0)%360.0-180.0;
if(diff>0)
turn right
else
turn left
如果 abs(diff)
非常小,您可能根本不想转动。
我不确定它是否会让你的玩家绕你的星球运行。您需要设置正确的角速度和线速度。
关于java - 旋转玩家面对行星,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11486453/