我正在为自己制作一个实用程序,以便在我的游戏中轻松地将度数转换为 x 和 y 坐标,但我遇到了一个问题;试图在屏幕上以度数移动玩家。我发现了多个不起作用的公式,我需要一些帮助。这是我找到的代码:
def move(degrees, offset):
x = math.cos(degrees * 57.2957795) * offset # 57.2957795 Was supposed to be the
y = math.sin(degrees * 57.2957795) * offset # magic number but it won't work.
return [x, y]
然后我运行了这个:
move(0, 300)
输出:
[300.0, 0.0]
它工作得很好,但是当我这样做的时候:
move(90, 300)
它输出了这个:
[-89.8549554331319, -286.22733444608303]
请您参考如下方法:
您的方法几乎是正确的。您应该对 sin/cos 函数使用弧度。这是我在 C++ 中常用的方法(移植到 python)进行 2D 移动。
import math
def move(degrees, offset)
rads = math.radians(degrees)
x = math.cos(rads) * offset
y = math.sin(rads) * offset
return x, y
