|
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 ? 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
|
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 |
Angeljruiz
Member #14,553
September 2012
|
Soooo how do i fix it ? |
Raidho36
Member #14,628
October 2012
|
Just define something like that: |
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
|
Make sure your tank coordinates are't of integral type. |
Angeljruiz
Member #14,553
September 2012
|
Explain? |
Raidho36
Member #14,628
October 2012
|
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. |
|