转换角度以度为向量 [英] Convert an angle in degrees, to a vector

查看:741
本文介绍了转换角度以度为向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一些游戏节目。 FWIW我使用XNA,但我怀疑,这是相关的。

I'm doing some game programming. FWIW I'm using XNA, but I'm doubtful that this is relevant.

我想度转换为方向向量(即X和Y)与幅度1

I'd like to convert degrees to a directional vector (ie X and Y) with magnitude 1.

我的原点(0,0)是在左上角。

My origin (0,0) is in the upper left.

所以我想0度转换为[0,-1]

So I'd like 0 degrees to convert to [0, -1]

我以为最好的方法,这样做是为了把我的北/上的定义,并采用矩阵旋转,但这似乎并没有工作。

I thought the best way to do this was to take my definition of North/Up and rotate it using a matrix, but this does not seem to be working.

这里是代码...

public class Conversion
{
    public static Vector2 GetDirectionVectorFromDegrees(float Degrees)
    {
        Vector2 North= new Vector2(0, -1);
        float Radians = MathHelper.ToRadians(Degrees);

        var RotationMatrix = Matrix.CreateRotationZ(Radians);
        return Vector2.Transform(North, RotationMatrix);
    }
}



...这是我的单位测试...

[TestFixture]
public class Turning_Tests
{
    [Test]
    public void Degrees0_Tests()
    {
        Vector2 result = Conversion.GetDirectionVectorFromDegrees(0);
        Assert.AreEqual(0, result.X);
        Assert.AreEqual(-1, result.Y);
    }
    [Test]
    public void Degrees90_Tests()
    {
        Vector2 result = Conversion.GetDirectionVectorFromDegrees(90);
        Assert.AreEqual(1, result.X);
        Assert.AreEqual(0, result.Y);
    }
    [Test]
    public void Degrees180_Tests()
    {
        Vector2 result = Conversion.GetDirectionVectorFromDegrees(180);
        Assert.AreEqual(0, result.X);
        Assert.AreEqual(1, result.Y);
    }
    [Test]
    public void Degrees270_Tests()
    {
        Vector2 result = Conversion.GetDirectionVectorFromDegrees(270);
        Assert.AreEqual(-1, result.X);
        Assert.AreEqual(0, result.Y);
    }

}



我是不是接近这一切都错了吗?
我应该用一个矩阵?
具有我搞砸了,并从度在错误的地方转换成弧度

Am I approaching this all wrong? Should I be using a matrix? Have I screwed up and converted from degrees to radians in the wrong place?

我见过的建议,这可以使用类似的代码来完成:

I've seen suggestions that this can be done using code like:

new Vector2((float)Math.Cos(Angle), (float)Math.Sin(Angle));



...或...有时

...or sometimes...

new Vector2((float)Math.Sin(Angle), (float)Math.Cos(Angle));



然而,这些似乎并没有工作,要么

However these don't seem to work either

有人可以把我在正确的道路上......或者更好的给我一些代码,这将导致4提供的单元测试路径?

在事先非常感谢

推荐答案

只需使用:

new Vector2((float)Math.Cos(radians), (float)Math.Sin(radians))

请务必从度转换为弧度,这种方法了。

Be sure to convert from degrees to radians with this approach too.

这会使用<$开始的数学家的约定C $ C> [1,0] 朝要去的方向 [0,1] (即逆时针方向的取向数学家用于两轴)。

This uses the mathematician's convention of starting from [1, 0] and going in the direction towards [0, 1] (that is counter-clockwise with the orientation that mathematicians use for the two axes).

要改为使用您的约定(从开始[0,-1]

To use instead your convention (starting from [0, -1] and going in the direction of [1, 0]) you need:

new Vector2((float)Math.Sin(radians), -(float)Math.Cos(radians))

请注意,您的转换,从角度到弧度永远不可能准确的(这涉及到与π的东西)。您应当允许在你的测试一些宽容。另外,如果你使用双击而不是浮动弧度,你将不得不在中间计算一些额外的精度。

Note that your conversion from degrees to radians can never be exact (it involves something with π). You should allow for some tolerance in your tests. Also, if you use double instead of float for the radians, you will have some extra precision in the intermediate calculation.

这篇关于转换角度以度为向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆