【发布时间】:2020-12-15 20:30:58
【问题描述】:
我正在使用 C# 在自定义引擎中制作游戏。 (不是统一)
我有一个大网格和两个对象的 x/y 坐标。 Player 对象和 Destination 对象。以及玩家当前的旋转度数 (0-360)。
我已经变得过于依赖现有的游戏引擎,无法弄清楚如何找到我需要让玩家面对目标的轮换。
playerRotation;//0 to 360 degrees.
playerX double = 47.43;
playerY double = 43.36;
targetX double = 52.15;
targetY double = 38.67;
我目前的方法是尝试通过以下方式获取对象之间的距离:
float distanceX = Math.Abs(playerX - destinationX);
float distanceY = Math.Abs(playerY - destinationY);
这似乎工作正常。然后我需要旋转玩家面对目的地并让他们朝着目的地移动直到距离X/Y
编辑:我一直在搞乱 Atan2 试图得到答案。
Vector2 playerCoords = new Vector2(playerX, playerY);
Vector2 targetCoords = new Vector2(targetX, targetY);
double theta = Math.Atan2((targetCoords.yValue - playerCoords.yValue), (targetCoords.xValue - playerCoords.xValue));
theta = theta * (180 / Math.PI);//Convert theta to degrees.
double sigma = playerRotation;//Direction in degrees the player is currently facing.
double omega = sigma - theta;
OutputLog("omega: " + omega);
我的输出日志应该向我显示我的玩家需要面对的角度才能面对目标。但它给了我错误的结果。
玩家:(4782, 4172) 和 目标:(4749, 4157)
角度应该是286~。
但是 Theta = -155 和 omega = 229。
【问题讨论】:
-
我看到你交换了Atan2函数的参数,y向量应该是第一个参数,x向量应该是第二个。
-
你得到的角度可能是逆时针的,所以也要记住这一点
-
哦,哎呀!我把它换成了:
double theta = Math.Atan2((targetCoords.yValue - playerCoords.yValue), (targetCoords.xValue - playerCoords.xValue));但我的结果仍然是错误的。有时大于 360 或小于 0。是因为它是逆时针的吗?我将如何解决这个问题? -
我不太确定 double 到 int 的转换。将“theta = theta * (180 / Math.PI)”替换为“theta = theta * (180.0 / Math.PI)”只是为了保存。 (注意 180 之后的 .0)并且你得到的 Atan2 角度已经是玩家应该看到的角度。因此,您可以删除该 sigma omega 计算。你要找的角度是theta,在输出日志中看一下
-
似乎仍然不起作用。玩家:(4782, 4172) 目标:(4749, 4157) 角度应该在 74~ 左右。但是 Theta = -155 和 omega = 229。
标签: c# rotation grid coordinates