   [SDL] Pong Collision Error
  Hey everyone, I'm attempting a Pong clone using the SDL library and C++, who hasn't?! :P. Anyways, I'm having an error with my code, tell me if you can see the error, sorry if it's sloppy for you.// HEADERS#include <SDL/SDL.h>// SET BOOLEANS && EVENTSDL_Event event;bool GAMEOVER = false;// SET SURFACESSDL_Surface *screen = NULL;SDL_Surface *bg = NULL;SDL_Surface *paddle = NULL;SDL_Surface *ball = NULL;// SET VARIABLESint p1_x = 0;int p1_y = 150;int p1_w = 27;int p1_h = 117;int p1_sx = 50;int p1_sy = 50;int p2_x = 613;int p2_y = 150;int p2_w = 27;int p2_h = 117;int p2_sx = 5;int p2_sy = 5;int ball_x = 320;int ball_y = 240;int ball_w = 22;int ball_h = 22;int ball_sx = 1;int ball_sy = 1;int screen_width = 640;int screen_height = 480;int screen_bpp = 32;void draw(int x, int y, SDL_Surface *src, SDL_Surface *dst){SDL_Rect offset;offset.x = x;offset.y = y;SDL_BlitSurface(src, NULL, dst, &offset);}int main(int argc, char* argv[]){// START SDLSDL_Init(SDL_INIT_EVERYTHING);// LOAD SURFACESscreen = SDL_SetVideoMode(screen_width, screen_height, screen_bpp, SDL_SWSURFACE);bg = SDL_LoadBMP("bg.bmp");paddle = SDL_LoadBMP("paddle.bmp");ball = SDL_LoadBMP("ball.bmp");// SET SCREENSDL_WM_SetCaption("My Pong Clone", NULL);SDL_ShowCursor(SDL_DISABLE);while(GAMEOVER == false) // MAIN GAME LOOP{// DRAW IMAGESdraw(0, 0, bg, screen);draw(p1_x, p1_y, paddle, screen);draw(p2_x, p2_y, paddle, screen);draw(ball_x, ball_y, ball, screen);// SET SPEEDSball_x += ball_sx;ball_y += ball_sy;// HANDLE MOVEMENTwhile(SDL_PollEvent(&event)){if(event.type == SDL_QUIT){GAMEOVER = true;}else if(event.type == SDL_KEYDOWN){if(event.key.keysym.sym == SDLK_UP){p1_y -= p1_sy;}else if(event.key.keysym.sym == SDLK_DOWN){p1_y += p1_sy;}}else if(event.type == SDL_KEYUP){if(event.key.keysym.sym == SDLK_UP){p1_y += p1_sy - 46;}else if(event.key.keysym.sym == SDLK_DOWN){p1_y -= p1_sy - 46;}}}// CHECK COLLISIONif(p1_y < 0){p1_y += p1_sy;}else if(p1_y + p1_h >= screen_height){p1_y -= p1_sy;}else if(p2_y < 0){p2_y += p2_sy;}else if(p2_y + p2_h >= screen_height){p2_y -= p2_sy;}else if(ball_x <= p1_x + p1_w){ball_sx *= -1;}else if(ball_x + ball_h >= p2_x){ball_sx *= -1;}else if(ball_y + ball_h >= p1_y){ball_sy *= -1;}else if(ball_y * ball_h >= p2_y){ball_sy *= -1;}else if(ball_y <= p1_y + p1_h){ball_sy *= -1;}else if(ball_y <= p2_y + p2_h){ball_sy *= -1;}// REFRESH SCREENSDL_Flip(screen);} // END LOOP// CLEAN UPSDL_FreeSurface(bg);SDL_FreeSurface(paddle);SDL_FreeSurface(ball);SDL_Quit();return 0;}
  Hey, can you elaborate on 'collision error'?Is the ball not colliding with the paddle at all? Is it colliding but coming off at the wrong angle? etc..;  Well I looked at it a little bit more, and found out the problem, but I don't know the solution. What's happening, is the ball keeps going back to the other side, even if the paddle is moving up. It's drawing a huge line for the X coordinate, and the ball is hitting the x coordinate and bouncing back because of...else if(ball_x <= p1_x + p1_w){ball_sx *= -1;};  Instead of just using position variables, use an SDL_Rect to represent the paddles and ball. Then write a generic rect-collision function, that you can use in almost every game you write from now to 3D. Then, at least section off your screen bounds-checking with comments like "player 1 bounds checking", "ball bounds-checking", etc. Use slighly longer, more descriptive variable names.Actually, this would be a good place to wrap the players and ball in a class. Just look at all the variables you prefix with p1 or p2, and put those in a class Player. then you can just go Player p1, p2. put their bounds checking in a member function. Same with the ball. Later, try making them both inherit from a base class Sprite. As it is, you may as well be programming in C.Make sure you check every SDL error return.On the whole, other than that, not bad!   