Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

c# - Convert an angle in degrees, to a vector

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

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

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

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.

Here is the Code...

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

... and here are my unit tests...

[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

Can someone put me on the right path... or better yet give me some code which causes the 4 provided unit tests to path?

Many thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just use:

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

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...