Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Degrees to Radians

This thread is locked; no one can reply to it. rss feed Print
Degrees to Radians
Angeljruiz
Member #14,553
September 2012

Hey rrybody,

Im trying to simulate a tank right now and the problem i have is that every rotate method i find uses degrees, not radians, so how would i be able to convert it ?
My current code is

#SelectExpand
1void CheckKeys() 2{ 3 4 if (!kLeft && !kRight && !kUp && !kDown) 5 { 6 KeyDown = false; 7 return; 8 } 9 10 KeyDown = true; 11 12 if (kLeft) 13 { 14 if (KeyCounter >= KeyTick) 15 { 16 TankRotation -= 1; 17 Redraw = true; 18 KeyCounter = 0; 19 } 20 } 21 22 if (kRight) 23 { 24 if (KeyCounter >= KeyTick) 25 { 26 TankRotation += 1; 27 Redraw = true; 28 KeyCounter = 0; 29 } 30 } 31 32 if (kUp) 33 { 34 if (KeyCounter >= KeyTick) 35 { 36 TankX += (cos(ALLEGRO_PI/180 * TankRotation) * Speed); 37 TankY += (sin(ALLEGRO_PI/180 * TankRotation) * Speed); 38 Redraw = true; 39 KeyCounter = 0; 40 } 41 } 42 43 if (kDown) 44 { 45 if (KeyCounter >= KeyTick) 46 { 47 TankX += (cos(ALLEGRO_PI/180 * TankRotation)) * (Speed * -1); 48 TankY += (sin(ALLEGRO_PI/180 * TankRotation)) * (Speed * -1); 49 Redraw = true; 50 KeyCounter = 0; 51 } 52 } 53 54}

    al_draw_rotated_bitmap(Tank, 32, 32, TankX, TankY, TankRotation*(ALLEGRO_PI/180), NULL);

EDIT: Updated code

SiegeLord
Member #7,827
October 2006
avatar

Every Allegro function uses radians, not degrees. Also, ALLEGRO_PI / 180 is not 0.017453277777.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Angeljruiz
Member #14,553
September 2012

Soooo how do i fix it ?

Raidho36
Member #14,628
October 2012
avatar

Just define something like that:
#define DEGTORAD (ALLEGRO_PI/180.0)
Multiply by DEGTORAD value if you convert degrees to radians and divide by it if it's radians to degrees. It can also be a function-like, if that's more convenient to you:
#define DEGTORAD(x) ((x)*(ALLEGRO_PI/180.0))
#define RADTODEG(x) ((x)/(ALLEGRO_PI*180.0))
Already 4000 years, and no other solution yet.

Angeljruiz
Member #14,553
September 2012

Okay i think i got a handle on whats happening now, but still its not working properly. It only moves if its facing directly up down right or left, it doesnt move diagonally at all.

Raidho36
Member #14,628
October 2012
avatar

Make sure your tank coordinates are't of integral type.

Angeljruiz
Member #14,553
September 2012

Explain?
EDIT: Never mind i got it :P I just put it as a float and it started to work. Could you explain why that happens though?

Raidho36
Member #14,628
October 2012
avatar

Integer variables can only store integral values themselves. That's why they called that, obviously.

Result of any operation on integer is rounded so one could store it. For you that means that if you add too small value, no change will even happen at all. And on top of that, discarded decimal digits are not accumulated. So if you expect your integer to increase by 1 after you add 0.4 three times, you wrong: nothing will change because decimal part is always discarded.

That's a basics of integral types, actually. I am surprised you didn't knew that.

Go to: