Jump to content
  • Advertisement

Archived

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

LostLogic

Getting X & Y vector with a given angle

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

If you intended to correct an error in the post then please contact us.

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

Share this post


Link to post
Share on other sites
Advertisement
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
#define DegToRad(deg) ((deg/180)*PI)

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 this post


Link to post
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!