简体   繁体   中英

how to get a vector from an angle

I'm making a game in pygame and I want a spaceship to move in the direction it's facing. I have the angle and the magnitude, how can I get the direction in the form of a normalized vector that I can then add to the spaceship's x and y to make it move?

What you want to do is change polar coordinates to Cartesian coordinates.

So as I see it you are in 2D. Now you can not avoid using trigonometry. Let's assume that you are facing with an α angle beginning from the x axis, and growing towards y. Let d be the magnitude.

Vx = cos(α) * d

Vy = sin(α) * d

Now this is not normalized, as I have already multiplied it with d, the magnitude of the speed.

Please check what type of angle your sin and cos functions take, and give matching inputs (it can be radians or degrees). An easy way to check this is to print out sin(30). If it is 0.5, you are in degrees. If it is -0.988... you are in radians.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM