## Formula for converting angle to vector?

Nibbie
Posts: 1
Joined: 2011.07
Post: #1
Hi guys

Basic game math help needed.

I need a formula to convert an angle in to (x, y).

For example 135 deg = (1, 1) and 90 = (1, 0)

Moderator
Posts: 713
Joined: 2002.04
Post: #2
Code:
```double XComponentOfVector( const double p_direction, const double p_magnitude ) {     return ( sin( p_direction ) * p_magnitude ); }```

...and...

Code:
```double YComponentOfVector( const double p_direction, const double p_magnitude ) {     return ( cos( p_direction ) * p_magnitude ); }```

Mark Bishop
--
Student and freelance OS X & iOS developer
Moderator
Posts: 1,563
Joined: 2003.10
Post: #3
sin and cos are what you want. If your angles are in degrees, remember to convert to radians first. Also note that 135 degrees in your example will be {0.707106781186548, 0.707106781186548}, not {1, 1}; if you want square coordinates rather than circular ones, you'll need to do something like divide both numbers by the larger of the two.