XNA Angle Forward

I want to move the object forward, depending on what angle it collides with, in this case it’s a tank. So what I want to do is:
When I press W, the tank should move forward.
When I press A or D, it rotates a corner.

While I can turn the corner, but I have no idea how to make the tank move forward. I have tried such things.

if (key.IsKeyDown(Keys.W)) { TankPos.Y = TankPos.Y - 4; } 

But of course, this only makes it rise, regardless of the angle. Any simple solution?

+4
source share
3 answers

Answers to participants' answers and depending on whether you use a fixed time step or not, you also need to multiply the delta with the time elapsed since the last frame.

 var deltaX = Math.Sin(angleRad) * tankSpeed * gameTime.ElapsedGameTime.Milliseconds; var deltaY = -Math.Cos(angleRad) * tankSpeed * gameTime.ElapsedGameTime.Milliseconds; 

(milliseconds or seconds, depending on what measurement you use for the speed of your tank.

+4
source

Assuming 0.0 is the top left and 0 degrees is up:

 var deltaX = Math.Sin(angleRad) * distance; var deltaY = -Math.Cos(angleRad) * distance; 
+2
source

Do you rotate the camera or tank when you a / d? If you just rotate the tank, it will quickly leave the camera. If you rotate the camera, you can use the x and y components of the camera direction vector to get the tank's direction of movement.

0
source

All Articles