Allegro Trigonometric Conversion

From Allegro Wiki

Jump to: navigation, search

When doing the slightest amount of trigonometry in a game, the confusing part is converting between the various types of degrees. Here is an overview, plus code to convert between them:

  • Degrees range from 0 to 360
  • Radians range from 0 to 2 * PI, and are used by e.g. the libc functions sin() and cos()
  • Allegro degrees range from 0 to 255, and are used by e.g. rotate_sprite()
//radians to degrees:
float degrees = radians * 180 / PI;

//degrees to radians:
float radians = degrees * PI / 180;

//degrees to allegro degrees
fix al_deg = ftofix (degrees * 256 / 360);

//allegro degrees to degrees
float degrees = fixtof (al_deg) * 360 / 256;

//radians to allegro degrees
fix al_deg = ftofix (radians * 128 / PI);

//allegro degrees to radians
float radians = fixtof (al_deg) * PI / 128;

For more info, see the article sin & cos, the programmer's pals

Personal tools