Tank rotation

Hey everybody

I was wondering how i could simulate tank like movement.
What i was planing on doing is changing the rotation using left and right keys (Already using a library for input) then moving the tank with both the up and down keys. Usually movements pretty easy as in if the right button is pressed then increase the x coord but i wouldnt know how to do that with rotation. Sorry, its kind of hard for me to explain.
1
2
3
4
5
6
7
8
Tank heading += TurningRate * LeftKeyPressed
Tank heading -= TurningRate * RightKeyPressed

Tank X Position += cos(Tank heading) + MovementRate * FwdKeyPressed
Tank X Position -= cos(Tank heading) + MovementRate * AftKeyPressed

Tank Y Position += sin(Tank heading) + MovementRate * FwdKeyPressed
Tank Y Position -= sin(Tank heading) + MovementRate  * AftKeyPressed
Why multiply the turning rate by LeftKeyPressed?
The exact implementation will differ depending on what libraries you're using to handle keyboard input; however, the rough logic is as follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if (W == Pressed) {
    location.x += (cos(0.017453277777 * heading)) * speed;
    location.y += (sin(0.017453277777 * heading)) * speed;
}

else if (S == Pressed) {
    location.x += (cos(0.017453277777 * heading)) * (speed * -1);
    location.y += (sin(0.017453277777 * heading)) * (speed * -1);
}

else if (A == Pressed) {
    heading -= 1;
    if (heading < 0) heading = 360;
}

else if(D == Pressed) {
    heading += 1;
    if (heading > 360) heading = 0;
}


Hopes this helps. If you happen to be using SFML I can help you with implementation
Its not really working as expected. It only moves if the angle is just right and even then it just moves up.
Heres my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
void CheckKeys()
{
    
    if (!kLeft && !kRight && !kUp && !kDown)
    {
        KeyDown = false;
        return;
    }
    
    KeyDown = true;
    
    if (kLeft)
    {
        if (KeyCounter >= KeyTick)
        {
            TankRotation -= 1;
            Redraw = true;
            KeyCounter = 0;
        }
    }
    
    if (kRight)
    {
        if (KeyCounter >= KeyTick)
        {
            TankRotation += 1;
            Redraw = true;
            KeyCounter = 0;
        }
    }
    
    if (kUp)
    {
        if (KeyCounter >= KeyTick)
        {
            TankX += (cos(0.017453277777 * TankRotation)) * Speed;
            TankY += (sin(0.017453277777 * TankRotation)) * Speed;
            Redraw = true;
            KeyCounter = 0;
        }
    }
        
}

and im drawing it using this
 
            al_draw_rotated_bitmap(Tank, 32, 32, TankX, TankY, TankRotation*0.017453277777, NULL);

Tank is the bitmap im drawing, both 32s are the x and y values of the middle of the bitmap, the next to arguments are the screen coords im drawing to, and the next is the angle to rotate by
Last edited on
closed account (o1vk4iN6)
Well for converting from deg to rad I would use a name instead of the actual number or an inlined function might be even better. It also seems everything is a global variable, why not make a class call it, vec2 use that for position instead, then make a class for tank or entity to hold the position etc...
Last edited on
Well how would you convert it exactly?
And i know everythings a global right now, im just going to leave it as is right now.
closed account (o1vk4iN6)
It's a simple mathematical equation you learn in highschool. 0.017blahblah is just PI / 180. It really is simple when you think about it. 2PI is a complete circle in degrees it is 360, so 2PI/360 is rad per degree (can be simplified to PI/180) that multiplied by any degree will get you the radians, which is what all the mathematical functions use.

Why start a bad habit ? Save it now by removing all the globals before it's too late.
Oh thats why i dont know that, im still in highschool - My teacher only briefly mentioned sin cos and tang so i dont really know what its used for.
So what i have is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
if (kLeft)
    {
        if (KeyCounter >= KeyTick)
        {
            TankRotation -= 1;
            Redraw = true;
            KeyCounter = 0;
        }
    }
    
    if (kRight)
    {
        if (KeyCounter >= KeyTick)
        {
            TankRotation += 1;
            Redraw = true;
            KeyCounter = 0;
        }
    }
    
    if (kUp)
    {
        if (KeyCounter >= KeyTick)
        {
            TankX += (cos(ALLEGRO_PI/180 * TankRotation) * Speed);
            TankY += (sin(ALLEGRO_PI/180 * TankRotation) * Speed);
            Redraw = true;
            KeyCounter = 0;
        }
    }
    
    if (kDown)
    {
        if (KeyCounter >= KeyTick)
        {
            TankX += (cos(ALLEGRO_PI/180 * TankRotation)) * (Speed * -1);
            TankY += (sin(ALLEGRO_PI/180 * TankRotation)) * (Speed * -1);
            Redraw = true;
            KeyCounter = 0;
        }
    }

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


But its still not working as expected, it rotates fine and smooth but it doesnt move as it should. Thanks for your help so far though.
And ehh i know globals are bad. Ill fix it right now
Topic archived. No new replies allowed.