# [SDL] Collision detecting problems

## Recommended Posts

nooblet    167
Hello, I am using Visual C++ 2005 Express Edition and I am trying to create pong ^_^. Well I am trying to test collision stuff and I know I haven't made the ball randomly go yet because well I am just testing the collision stuff. Well the ball goes to the left like I want it to. But when it hits the paddle it gets stuck.. and it keeps coming back and forth, any solution?
void check_collision()
{
{
ball_x += 2;
}
{
ball_x -= 2;
}
{
ball_x -= 1;
}
}


All of the source..
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <time.h>

SDL_Event event;

SDL_Surface *screen = NULL;
SDL_Surface *ball = NULL;
SDL_Surface *background = NULL;

int randomNumber = rand();

int ball_x = 297;
int ball_y = 220;
int dir = (randomNumber % 1) + 1;

void apply_surface( int x, int y, SDL_Surface *source, SDL_Surface *dest )
{
SDL_Rect offset;

offset.x = x;
offset.y = y;

SDL_BlitSurface( source, NULL, dest, &offset );
}

void check_collision()
{
{
ball_x += 2;
}
{
ball_x -= 2;
}
{
ball_x -= 1;
}
}

bool init()
{
if( SDL_Init(SDL_INIT_EVERYTHING) < 0 )
{
printf("Unable to load SDL: %s", SDL_GetError());
return false;
}
else
{
}

screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
if(!screen)
{
printf("Unable to set 640x480 screen: %s", SDL_GetError());
return false;
}
else
{
printf("Set window to 640x480\n");
}

if(!ball)
{
printf("Unable to load image: %s", SDL_GetError());
return false;
}
else
{
}

if(!background)
{
printf("Unable to load image: %s", SDL_GetError());
return false;
}
else
{
}

{
printf("Unable to load image: %s", SDL_GetError());
return false;
}
else
{
}
{
printf("Unable to load image: %s", SDL_GetError());
return false;
}
else
{
}

SDL_WM_SetCaption("Pong Clone", NULL);

return true;
}

int main( int argc, char* argv[] )
{
bool quit = false;

init();

while( quit == false )
{
SDL_FillRect( screen, NULL, 0 );
apply_surface( 0, 0, background, screen );
apply_surface( ball_x, ball_y, ball, screen );

check_collision();

while( SDL_PollEvent(&event) )
{
if(event.type == SDL_QUIT)
{
printf("User quit the program.\n");
quit = true;
}
}

SDL_Flip(screen);
}

SDL_Quit();

return 0;
}



##### Share on other sites
XG08Zero    138
I didn't take too much time analyzing your code, but when I coded pong, this is what I did when I made the ball bounce the other direction...

ball.setSpeed(-ball.getSpeed());

which, in turn, just reversed the direction

i'm guessing your 3rd if statement is to see whether the ball went past the paddle? Wouldn't it be better to use x<paddle_x or x>paddle_x (depending on left or right) Overall on all of your if statements, I think you need to revise them. I had this problem too. Take it step by step- first determine the direction, then the x, then the y, etc.

From your 1st if statement, I think I see a flaw...

o

|
|
|

Wouldn't the above condition turn out true? I think you need an && operator- also, you need to think about the height of the paddle. If you simply do...

wouldn't this happen?

___|
_o_| o
___|

the ball would pass through the middle of the paddle, because the paddle's y value is at the top isn't it?

Then again, I did this in a basic Java Graphics library, so maybe SDL is diff? I'd reconsider your if statements though- break it apart and try not to fit it all in one. That's how I eventually fixed all my collision errors. Hope this helps...