以度为单位的角度转换为矢量

Convert an angle in degrees, to a vector(以度为单位的角度转换为矢量)

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

问题描述

我正在做一些游戏编程.FWIW 我正在使用 XNA,但我怀疑这是否相关.

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

我想将度数转换为大小为 1 的方向向量(即 X 和 Y).

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]

我认为最好的方法是采用我对 North/Up 的定义并使用矩阵旋转它,但这似乎不起作用.

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));

...或者有时...

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.

这使用了从 [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] 开始并朝着 [1, 0] 的方向前进),您需要:

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))

请注意,您从度数到弧度的转换永远不会是精确的(它涉及到与 π 相关的内容).您应该在测试中允许一些容差.此外,如果您使用 double 而不是 float 作为 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.

这篇关于以度为单位的角度转换为矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:以度为单位的角度转换为矢量

基础教程推荐