# OpenGL changing degrees into directions

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

## Recommended Posts

Hi all, Can anyone show me how to change degrees into directions, for example I have 4 objects which move off at 0,90,180,270 degrees, how do I change these into x and y opengl movement. It could be any number of objects it's haow to do it mathmatically rather than the above example. Thanks

##### Share on other sites
You want to convert polar coordinates to Cartesian? The transformation is straight forward:

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

Where r>0 is the distance from origin to your point and a is the angle (0≤a<2π)

For normalized directions you have r=1, so you get

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

Hope this helps

EDIT: And make sure you don't mix up radians and degrees. Most implementations of sin and cos expect input in radians, whilst OpenGL I believe uses degrees.
You can convert degrees to radians by a simple transformation

y = T(x) = x • π / 180;

Where the input x in degrees and the output y will be in radians. And the inverse converts radians to degrees

x = T-1(y) = y • 180 / π

##### Share on other sites
The right triangle is one such that the hypotenuse (which represents your direction of movement in this case) forms the angle of movement with the adjacent, while the opposite completes the triangle. Assuming sector 1, the adjacent represents movement in x and the opposite represents movement in y.

The following relationships then hold:
1. the sine of the angle is equivalent to the length of the opposite divided by the length of the hypotenuse;

2. the cosine of the angle is equivalent to the length of the adjacent divided by the length of the hypotenuse; and

3. the tangent of the angle is equivalent to the length of the opposite divided by the length of the adjacent

You may remember them from trigonometry classes. Assuming a unit movement in the hypotenuse, then:

x-displacement = cos(angle)
y-displacement = sin(angle)

Scale these displacement by the number of units of real movement you want. Test for values of 90, 180, 270 and 0/360 degrees.

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5
gaxio
13

• 24
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631734
• Total Posts
3001933
×