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.