• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# Getting X & Y vector with a given angle

This topic is 6541 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am having a brain-cramp right now with the following problem. With a given angle (A), how do I find the X & Y vector or size? For example: In a top-down space game such as sub-space: 1. When the ship has an angle(A) of 0, it is pointing to the +y axis. 2. When the ship has an angle(A) of 90, it is pointing to the +x axis. 3. When the ship has an angle(A) of 180, it is pointing to the -y axis. 4. When the ship has an angle(A) of 270, it is pointing to the -x axis. Currently I just have a function that does a lookup to figure the x and y value. When the ship is pointing up I return a (0,1). When the ship is pointing to the right I return a (1,0), etc.... The issue I have with this is that I have to do an "if" for every possible angle. I know there is a way to do this with "simple" mathematics. So, can someone please show me the complete algorithm for this? One guy on Microsoft''s site said to use atan2(), but that really doesnt tell me anything. Help would be greatly appreciated! Thanks, LostLogic webmaster@lostlogic.com

x=sin(a)
y=cos(a)

##### Share on other sites
To expand on what Anonypous Moster said:

We have to convert your degree measurements into radians. Here''s a simple macro to do that:
#define PI 3.1415926535897932384626433832795

Normally, to convert degrees/radians to a vector you need a magnitude also, but we''ll bypass that by simple ignoring it .

Convert my structs to yours, I''ll just use them as a reference:
  #define PI 3.1415926535897932384626433832795#define DegToRad(deg) ((deg/180.0f)*PI)struct Vector2D { float x, y;};void ConvertDegToVector(long deg, Vector2D *vec) { float rad = DegToRad(deg); vec.x = cos(rad); vec.y = sin(rad);}

http://www.gdarchive.net/druidgames/

##### Share on other sites
NULL and VOID,

THANKS SO MUCH!!!! That is exactly what I needed. I totally forgot about the radian issue, duh...

It works great now, thanks!

LostLogic

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634047
• Total Posts
3015226
×