Move sprite in direction of value from rotation?

I am new to XNA and I am building an asteroid game. I have some problems to get my spaceship moving forward in the direction I want. When I press the up arrow, I want the engine to start and move the spacecraft in the same direction as the rotation of the spacecraft? Help is limited! I add a subclass that is my spaceship. The Draw method and its variables are in the main class. I'm not sure what to do in the EngineOn method?

class Spaceship: GameObject { // Konstruktor public Spaceship(Texture2D texture, Vector2 position): base(texture, position) { } public override void Update() { direction = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)); position += direction * speed; //position = position + direction //base.Update(); } // Metod som beräknar rymdskeppets färd i riktningen public void EngineOn() { Update(); } // Metod som beräknar rymdskeppets rotation motsols public void RotateLeft() { rotation -= rotationSpeed; } // Metod som beräknar rymdskeppets rotation medsols public void RotateRight() { rotation += rotationSpeed; } // Metod som beräknar bana för avlossade skott public void Fire() { } } 
+4
source share
1 answer
 public void EngineOn() { speed = 20; } public void EngineOff() { speed = 0; } 

PS Your speed is processor dependent, multiply speed by deltaTime

 public override void Update(GameTime gameTime) { direction = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)); position += direction * speed * gameTime.ElapsedGameTime.Milliseconds; } 
+3
source

Source: https://habr.com/ru/post/1397995/


All Articles