如何将法线转换为度数?
我正在开发一个游戏,并且正在编写一个 Entity 基类。由于许多实体的行为类似于粒子(2D),我想使用法线而不是以度数为单位的旋转。然而,由于我使用的是 OpenGL,所以我需要有法线的角度来旋转。从法线转换为度数最快的方法是什么,反之亦然。我知道我可以使用三角函数,例如 atan2
sin
cos
等,但我很确定有一种更快的方法。任何帮助或指导将不胜感激。
I am working on a game and I am writing an Entity
base class. Since many of the entities will behave like particles(2D) I want to use a normal instead of a rotation in degrees. However since I am using OpenGL I need to have the degree of the normal to rotate. What is the fastest way to convert from the normal to degrees and vice versa. I know that I can use trigonometric functions such as atan2
sin
cos
etc, but I am pretty sure there is a faster method. Any help or direction would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您受限于二维并且尝试将方向向量 (x, y) 转换为距 x 轴的度数,那么
atan2(y, x)
几乎肯定会是你最快的方法,除非你将 x 和 y 的可能值限制在一些非常微不足道的情况下。当然,要获取 y 轴的度数,只需使用atan2(x, y)
即可。该角度将以弧度为单位。乘以 180/pi 即可转换为度数。这应该需要很短的时间。您绘制的图形表明
atan2(x, y) * 180 / Math.PI
将为您提供所需的结果。除非您已经分析了代码并确定此计算中存在瓶颈(这不太可能),否则不要担心速度。
If you're constrained to two dimensions and you're trying to convert a directional vector (x, y) to degrees from the x-axis, then
atan2(y, x)
is almost definitely going to be your fastest method, unless you're constraining the possible values of x and y to some pretty trivial cases. To get the degrees from the y-axis, just useatan2(x, y)
, of course. This angle will be in radians. Multiply by 180/pi to convert to degrees. This should require a trivial amount of time.The figure you're drawing suggests that
atan2(x, y) * 180 / Math.PI
will give you the results you desire.Don't be concerned with speed unless you've profiled your code and have determined that there is a bottleneck in this calculation (which is unlikely).