Sign in to follow this  
knobby67

OpenGL changing degrees into directions

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


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


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this