Public Group

# Formula for direction ?

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

## Recommended Posts

Hello , Im making a top down asteroids game and I cant re-create the formula to turn D degrees (0-360) in to a "percent" (float 0.0 - 1.0) of 180. Or at least I cant get a smooth turning effect [Edited by - jopy on February 27, 2010 8:57:35 AM]

##### Share on other sites
It's not at all clear what you're trying to do, actually.

angle  value0      090     0.5 * length179    0.994 * length180    length181    -1.006 * length/2etc.

So there's quite a discontinuity at 180.

Maybe if you let us know what are you trying to do with xlength(), someone can provide more help.

For instance, what do you want to happen at 181?

##### Share on other sites
Are you sure you're using sin and cos how you want to be? The cosine of an angle is the distance that you need to move along the x axis to form the base of a right triangle with a hypotenuse of 1 and that angle as the angle between the x axis and the hypotenuse. The sine of the angle is the length of that opposite side, or the y distance. Take a look at the graphs on the side of the wikipedia pages (clicky) to understand whats going on in your functions. Maybe you just have a transformed coordinate system and you do want to be using cos for y and sin for x.

##### Share on other sites
your right. Its not quite correct. So what do I do ?

[Edited by - jopy on February 27, 2010 8:29:23 AM]

##### Share on other sites
If my screen had 0,0 as the lower left corner, and coordinate space increased to the right and top sides of the screen, and my angles were 0 pointing right, PI/2 radians up, PI radians left, and PI*3/2 radians down, my formula for direction would be:

xSpeed = speed*cos(angle)
ySpeed = speed*sin(angle)

And then every frame I would move the objects that amount on that axis. If I wanted 0,0 to be the upper left and the angles to increase in the same direction as before (counter-clockwise) then I just make y:

ySpeed = - speed*sin(angle)

Your language's math library most likely expects radians for angles not degrees.

so 90 degrees = PI/2, etc.

Take a bit to learn this relationship for cos and sin, they're useful and trig comes up quite a bit.

Edit: I can't tell clockwise from counter-clockwise.

[Edited by - DaedalusOwnsYou on February 27, 2010 6:21:58 PM]

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 11
• 18
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632929
• Total Posts
3009282
• ### Who's Online (See full list)

There are no registered users currently online

×