<legend id='spzmh'><style id='spzmh'><dir id='spzmh'><q id='spzmh'></q></dir></style></legend>
    <tfoot id='spzmh'></tfoot>

      • <bdo id='spzmh'></bdo><ul id='spzmh'></ul>
    1. <small id='spzmh'></small><noframes id='spzmh'>

    2. <i id='spzmh'><tr id='spzmh'><dt id='spzmh'><q id='spzmh'><span id='spzmh'><b id='spzmh'><form id='spzmh'><ins id='spzmh'></ins><ul id='spzmh'></ul><sub id='spzmh'></sub></form><legend id='spzmh'></legend><bdo id='spzmh'><pre id='spzmh'><center id='spzmh'></center></pre></bdo></b><th id='spzmh'></th></span></q></dt></tr></i><div id='spzmh'><tfoot id='spzmh'></tfoot><dl id='spzmh'><fieldset id='spzmh'></fieldset></dl></div>

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

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

            <tbody id='vRLIg'></tbody>
          <tfoot id='vRLIg'></tfoot>

          <legend id='vRLIg'><style id='vRLIg'><dir id='vRLIg'><q id='vRLIg'></q></dir></style></legend>

          1. <small id='vRLIg'></small><noframes id='vRLIg'>

              <bdo id='vRLIg'></bdo><ul id='vRLIg'></ul>
            • <i id='vRLIg'><tr id='vRLIg'><dt id='vRLIg'><q id='vRLIg'><span id='vRLIg'><b id='vRLIg'><form id='vRLIg'><ins id='vRLIg'></ins><ul id='vRLIg'></ul><sub id='vRLIg'></sub></form><legend id='vRLIg'></legend><bdo id='vRLIg'><pre id='vRLIg'><center id='vRLIg'></center></pre></bdo></b><th id='vRLIg'></th></span></q></dt></tr></i><div id='vRLIg'><tfoot id='vRLIg'></tfoot><dl id='vRLIg'><fieldset id='vRLIg'></fieldset></dl></div>
              • 本文介绍了以度为单位的角度转换为矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在做一些游戏编程.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.

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

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                Cannot remove items from ListBox(无法从列表框中删除项目)
                Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)

                <small id='wuX5p'></small><noframes id='wuX5p'>

                    <bdo id='wuX5p'></bdo><ul id='wuX5p'></ul>

                    <legend id='wuX5p'><style id='wuX5p'><dir id='wuX5p'><q id='wuX5p'></q></dir></style></legend>

                        <tbody id='wuX5p'></tbody>
                    • <tfoot id='wuX5p'></tfoot>
                      1. <i id='wuX5p'><tr id='wuX5p'><dt id='wuX5p'><q id='wuX5p'><span id='wuX5p'><b id='wuX5p'><form id='wuX5p'><ins id='wuX5p'></ins><ul id='wuX5p'></ul><sub id='wuX5p'></sub></form><legend id='wuX5p'></legend><bdo id='wuX5p'><pre id='wuX5p'><center id='wuX5p'></center></pre></bdo></b><th id='wuX5p'></th></span></q></dt></tr></i><div id='wuX5p'><tfoot id='wuX5p'></tfoot><dl id='wuX5p'><fieldset id='wuX5p'></fieldset></dl></div>