Led*_*Led 12 xna 2d vector angle direction
2D中的XNA是什么,矢量角度的标准方式有效?
向右0度,向上90度,向左180度,向下270度?
什么是'标准'实施
float VectortoAngle(Vector2 vec)
Run Code Online (Sandbox Code Playgroud)
和
Vector2 AngleToVector(float angle)
Run Code Online (Sandbox Code Playgroud)
那么VectortoAngle(AngleToVector(PI))== PI?
Ven*_*rix 21
要回答你的第一个问题,0度向上,向右90度,向下180度,向左270度. 这是一个简单的2D XNA旋转教程,为您提供更多信息.
至于将矢量转换为角度和背面,我在这里找到了几个好的实现:
Vector2 AngleToVector(float angle)
{
return new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
}
float VectorToAngle(Vector2 vector)
{
return (float)Math.Atan2(vector.Y, vector.X);
}
Run Code Online (Sandbox Code Playgroud)
此外,如果您是2D编程的新手,您可能需要研究Torque X 2D,它为您提供了很多这样的功能.如果您已经为XNA开发付费,那么您可以免费获得引擎二进制文件,并且有一个实用程序类可以从角度转换为向量和返回,以及其他有用的函数.
编辑:正如Ranieri在评论中指出的那样,当向上为0度时,该功能没有意义.这是一个(向上是(0,-1),右边是(1,0),向下是(0,1),左边是(-1,0):
Vector2 AngleToVector(float angle)
{
return new Vector2((float)Math.Sin(angle), -(float)Math.Cos(angle));
}
float VectorToAngle(Vector2 vector)
{
return (float)Math.Atan2(vector.X, -vector.Y);
}
Run Code Online (Sandbox Code Playgroud)
我还要注意,我已经使用Torque了一段时间,它使用0度向上,所以这就是我得到那部分的地方.向上意义,在这种情况下,将纹理绘制到屏幕上的方式与文件中的方式相同.所以下来就会把纹理颠倒过来.