Asteroids and Vector Forward

Hi everyone, it’s been a while since my last post but i’ve been busy with college.

Now that I have some free time I was trying (again) to learn how to use MonoGame so I’m working on a little Asteroids clone game, I’ve succesfuly drawn my spaceship and the background on the screen and now I’m trying to rotate it with “A” and “D” and make it move forward and backwards with “W” and “S”.

My problem is that when I rotate the sprite using the Draw function the sprite rotates but when I move it forward or backwards it doesn’t move towards de direction it is rotated so I was assuming to rotate the forward vector but I can’t really find how, to so I was hoping someone could help me or give me some advice.

Thanks for your time.

This may help… big playlist…

EDIT

Came across this gem while finding you something, go to the bottom of this page:

XNA Tutorials - XNA Meeting Point (weebly.com)

Wow, I’ve checked the first link and it seems a nice guide, although I took an overview and it doesn’t look like he makes the sprite rotate, but it has a lot of contents that might be useful for the future. I’ll save it, thanks.

Now I’m gonna check the second link.

Thanks!!

1 Like

Could you not take the Cosine of the Angle to get the X and the Sine to get the Y. That should give you a vector “forward” in XNA it will likely default to Vector2(1,0) for rotation 0 degree. Then you can just multiply your acceleration/speed by the Velocity Direction?

That’s basically where I got so far, I think I might be lacking the last part cause it still does some weird things.
I’ll add my code and a video showing how it behaves right now. I think right now the sprite direction and the forward direction are 90 degrees apart, so I might have to solve that next.


Image from Gyazo

Yeah, that is because in XNA “Forward” is actually right. 0 degree rotation == Vector2(1,0). So you should rotate your ship sprite in an image editing software to face right. That way it will all be lined up, if you need ship pointing up at game start simply set the sprite rotate in xna to 90 degrees.

Yeah, that totally does the trick, I was looking for a way to rotate the vector to match the sprite at the beginning through code, rather than edit the main asset, but this will work too.
Thanks!

For completeness, you can technically transform the vector with a matrix that’s rotated 90 degrees. But it’s not worth all the extra code and math when you can pre rotate the asset to align with xna.

I think I’ll try to do that more in the future, just for the sake of knowledge, for now I’m gonna stick with the first solution.
Thank you!!