I started trying to write a game in C# with XNA. The movement is done with an angle that you are facing. I went under the impression that sin(1) == sin(91) (Which Kerm proved false). I keep a look-up table for which direction you should move depending on what quadrant you are in. For instance, if you are in quadrant 2 (top-left in my case), then you are decreasing the X and increasing the Y values. It multiplies the look-up table value by the appropriate Sin/Cos values I get. int Angle is between 0-359 (can be 0 and can be 359), tempAngle is the MOD 90 of Angle. int speed is the speed at which you are moving. piUnder180 is literally 180 / MathHelper.pi, and is used to turn Degrees to Radians.
The main problem I am running into is: when you change quadrants, your character "jumps". I'll include the important parts of my code below if it helps people better than my awful introduction above.
Edit: Actually... piUnder180 = Pi / 180; It still does the correct transformation from degrees to radians though.
Code:
The main problem I am running into is: when you change quadrants, your character "jumps". I'll include the important parts of my code below if it helps people better than my awful introduction above.
Edit: Actually... piUnder180 = Pi / 180; It still does the correct transformation from degrees to radians though.
Code:
[. . .]
int speed = 15;
float[] QuadrantLookUp = new float[8] { 1, 1, -1, 1, -1, -1, 1, -1 }; //Angle == 0 means you are looking to the right.
float[] Degree90LookUp = new float[8] { 1, 0, 0, -1, -1, 0, 0, 1 };
int Angle = 0;
[. . .]
double piUnder180 = (double)MathHelper.Pi / 180;
[. . .]
float FrontBack = speed;
if (keyState.IsKeyDown(Keys.Down))
FrontBack *= -1;
if (keyState.IsKeyDown(Keys.Down) || keyState.IsKeyDown(Keys.Up))
{
int tempAngle = (int)Angle % 90;
if (tempAngle != 0)
{
mPosition.X += FrontBack * QuadrantLookUp[(int)(Angle / 90 * 2)] * (float)Math.Cos((double)tempAngle * piUnder180);
mPosition.Y += FrontBack * QuadrantLookUp[(int)(Angle / 90 * 2 + 1)] * (float)Math.Sin((double)tempAngle * piUnder180);
}
else
{
mPosition.X += FrontBack * Degree90LookUp[(int)(Angle / 90 * 2)];
mPosition.Y += FrontBack * Degree90LookUp[(int)(Angle / 90 * 2 + 1)];
}
}
if (keyState.IsKeyDown(Keys.Right))
Angle = (Angle + 1) % 360;
if (keyState.IsKeyDown(Keys.Left))
Angle = (Angle + 358) % 360;
[. . .]