0

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.

Taylor
  • 1

2 Answers2

0

As Andrew Russell said in this answer:

If you are using maths functions like sin or cos or atan2, then absolute angles always start from the X+ axis as zero radians, and the positive rotation direction rotates towards Y+.

The XNA SpriteBatch works in Client Space. Where "up" is Y-, not Y+ (as in Cartesian space, projection space, and what most people usually select for their world space). This makes the rotation appear as clockwise (not counter-clockwise as it would in Cartesian space). The actual coordinates the rotation is producing are the same.

From this piece of your code:

if(keyboardState.IsKeyDown(Keys.Left))
    {
        angle -= 1f * delta;
    }
if (keyboardState.IsKeyDown(Keys.Right))
    {
        angle += 1 * delta;
    }

Left key will turn your space ship clockwise (lower value of angle), and Right key will turn it counterclockwise (higher value of angle).

From your last comment:

My rotations are fine, It just when add speed when i pres the up arrow and turn, the movement is inverted

I would check this other part of your code:

position.Y += xSpeed * delta;
position.X += ySpeed * delta;

It seems weird to use xSpeed to update position.Y and viceversa, but also maybe you need to use -= to update position.Y

Community
  • 1
  • 1
Blas Soriano
  • 566
  • 3
  • 16
  • My rotations are fine, It just when add speed when i pres the up arrow and turn, the movement is inverted, The rotation rotates fine. – Taylor May 31 '15 at 17:30
  • @Taylor edited to add a bit more from Andrew's answer. Used bold in the relevant part. – Blas Soriano Jun 01 '15 at 06:22
0

Mathematician's conventions and screen coordinates sometimes don't match.

Try this:

xSpeed =  (float)Math.Sin(angle) * speed;
ySpeed = -(float)Math.Cos(angle) * speed; // notice '-' !

based on this answer.

Community
  • 1
  • 1
Jake
  • 115
  • 1
  • 3
  • 15