Making projectile shoot using with allegro library in C

392 Views Asked by At

I'm trying to make a projectile shoot using with allegro library in C.And I couldn't do it in no way.My all code is below.My circle goes up but then disappear.Even if not I can't bring it down to the ground.I'm not good at physic so if my equals are wrong please forgive me.

#include <allegro.h>
#include < math.h >


void StartAlleg(); // my start program function
void EndAlleg();



int main() {
    StartAlleg();

    BITMAP *buffer = create_bitmap(640, 480);

    int   g = 10, Vo = 0 , Vx = 5, Vy = 475, angle= 0;
    double time = 0, tUp = 0,hmax=0; //g is gravity

    show_mouse(screen);
    while (!key[KEY_ESC])
    {
        circle(buffer,  Vx, Vy, 5, makecol(255, 0, 0));

        if (key[KEY_UP]&&angle<360) angle++;
        if (key[KEY_RIGHT]) Vo++;
        if (key[KEY_DOWN] && angle>0) angle--;
        if (key[KEY_LEFT] && Vo>0) Vo--;

        textout_ex(buffer, font, "Player 1 : ", 0, 0, makecol(255, 255, 13), -1);
        textprintf(buffer, font, 0, 25, makecol(255, 255, 13), "Angle = %d  ",angle);
        textprintf(buffer, font, 0, 15, makecol(255, 255, 13), "Speed = %d  ", Vo);




        if (key[KEY_Z]  ){


            Vx = Vo*cos(double(angle));
            Vy = Vo*sin(double(angle));
            if (angle== 180 || angle == 360) Vy = 0;
            if (angle== 90 || angle== 270) Vx = 0;
            if (Vx < 0) Vx *= (-1);
            if (Vy < 0) Vy *= (-1);

            tUp = Vy / g;
            time = tUp * 2;

            hmax = (Vy*Vy) / (2*g);


                }

        textprintf(buffer, font, 0, 35, makecol(255, 255, 13), "tUp Value = %.2f  ", tUp);

        for (int i = 1; i <= time; i++)
        {
            if (i<tUp){ Vx = Vx + g; Vy += g; }

            else{ Vy -= g; Vx = Vx + g; }


        }


            blit(buffer, screen, 0, 0, 0, 0, 640, 480);
            rest(60);
            clear_bitmap(buffer);

    }

    EndAlleg(); // my end program function
    return 0;
}
END_OF_MAIN()



void StartAlleg() {
    int depth, res;
    allegro_init();
    depth = desktop_color_depth();
    if (depth == 0) depth = 32;
    set_color_depth(depth);
    res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
    if (res != 0) {
        allegro_message(allegro_error);
        exit(-1);
    }
    install_timer();
    install_keyboard();
    install_mouse();
    install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, "A");
}
void EndAlleg() {
    clear_keybuf();

}

I think , the main problem is here :

 for (int i = 1; i <= time; i++)
            {
                if (i<tUp){ Vx = Vx + g; Vy += g; }

                else{ Vy -= g; Vx = Vx + g; }


            }
1

There are 1 best solutions below

0
On

I didn't try to understand all your code, but it seems that your calculations are wrong.

Here is how gravity can be implemented :

First, you need to keep track of your projectile position, with variables like Px Py. This position will give you the drawing coordinates. Then you need to keep track of its speed, usually horizontal and vertical speed, with variables like Vx Vy. If your initial speed is a single vector with angle, convert it once.

Every tick of your game (every loop iteration in your case), you add the speeds to the positions. Then to add gravity, you subtract 10 to the vertical speed, also at every tick (it implements acceleration of -10).

And thats all. Negative speeds and accelerations are normal, you don't need to check, but you can check for borders for positions. Also, you should note that you usually divide the speeds and accelerations by the frequency of your ticks, or else the faster your loop the faster the projectile will move.

You should note that this isn't the best way to implement gravity, because this only approximate physics (more ticks per second will give you more accurate simulation). You should google "game implement gravity properly" for an accurate algorithm, I'm not an expert.