I have a small problem that i cannot work out. I'm currently learning to apply vector math and trigonometry to games, To learn i have created a game where the player fly's a space ship of sorts however i have a problem.
My movement seems to be inverted?
He will fly upwards and downwards correctly, However the left and right movement is inverted?
Here is my code:
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
angleDeg = (float)(angle * (360 / (Math.PI * 2)));
var angleRad = (float)MathHelper.ToRadians(angleDeg);
var delta = (float)gameTime.ElapsedGameTime.TotalSeconds;
xSpeed = (float)Math.Cos(angle) * speed;
ySpeed = (float)Math.Sin(angle) * speed;
position.Y += xSpeed * delta;
position.X += ySpeed * delta;
keyboardState = Keyboard.GetState();
if(angleDeg < -360 || angleDeg > 360)
{
angle = 0;
angleDeg = 0;
}
if(keyboardState.IsKeyDown(Keys.Left))
{
angle -= 1f * delta;
}
if (keyboardState.IsKeyDown(Keys.Right))
{
angle += 1 * delta;
}
if (keyboardState.IsKeyDown(Keys.Up))
{
speed -= 1f;
}
if (keyboardState.IsKeyDown(Keys.Down))
{
speed += 1f;
}
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(player, position, sourceRect, Color.White, angle, origin, 1.0f, SpriteEffects.None, 1);
spriteBatch.DrawString(font, "Rotation: " + angleDeg, new Vector2(10, 5), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
Anyone have any ideas as to why this is happening?
Thanks in advance.