c++ - Rectangle Bounce collision detection -


i've created program using sdl in rectangle continuously collides within walls of program, collision checking not working properly.

here code:`

int main(int argc, char *argv[]){ //variable initialization] width = height = 45; srcx = srcy = 0; desty = destx = 0; vlc = 1; sdl_init(sdl_init_video); screen = sdl_setvideomode(640, 480, 32, sdl_swsurface); sdl_wm_setcaption("bouncing balls","./ball.jpg"); backg = img_load("./back.png"); ball = img_load("./ball.jpg"); while (checkbounce){     //increase velocity     destx += vlc;     desty += vlc;      //collision checking         if (destx < 0){             destx = 0;             vlc = -vlc;             destx += vlc;         }         if (desty < 0){             desty = 0;             vlc = -vlc;             desty += vlc;         }         if (desty + height > 480){             desty = 480 - height;             vlc = -vlc;             }         if (destx + width > 640){             destx = 640 - width;             vlc = -vlc;         }     if (sdl_pollevent(&event)){         if (event.type == sdl_quit)             checkbounce = false;     } //applying surfaces applysurface(0, 0, backg, screen); applyball(srcx, srcy, destx, desty, width, height, ball, screen); sdl_flip(screen); } sdl_quit(); return 0; } 

here gif image happening :bouncing rectangle.gif

i'm assuming expected result rectangle bounce off of walls correctly?

you need separate velocity in x , y components rather using single number. because velocity 2 dimensional.

your program causing both x , y components become negative whenever collision detected. causes rectangle bounce backwards along path.

here's edit:

int main(int argc, char *argv[]){     //variable initialization]     width = height = 45;     srcx = srcy = 0;     desty = destx = 0;     vlcx = 1;     vlcy = 1;     sdl_init(sdl_init_video);     screen = sdl_setvideomode(640, 480, 32, sdl_swsurface);     sdl_wm_setcaption("bouncing balls","./ball.jpg");     backg = img_load("./back.png");     ball = img_load("./ball.jpg");     while (checkbounce){         //increase velocity         destx += vlcx;         desty += vlcy;         //collision checking         if (destx < 0){             destx = 0;             vlcx = -vlcx;             destx += vlcx;         }         if (desty < 0){             desty = 0;             vlcy = -vlcy;             desty += vlcy;         }         if (desty + height > 480){             desty = 480 - height;             vlcy = -vlcy;             }         if (destx + width > 640){             destx = 640 - width;             vlcx = -vlcx;         }         if (sdl_pollevent(&event)){             if (event.type == sdl_quit)                 checkbounce = false;         }         //applying surfaces         applysurface(0, 0, backg, screen);         applyball(srcx, srcy, destx, desty, width, height, ball, screen);         sdl_flip(screen);     }     sdl_quit();     return 0; } 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -