Sign in to follow this  
nooblet

[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()
{
	if( ball_x == paddle_stop_x || ball_y == paddle_y )
	{
		ball_x += 2;
	}
    if( ball_x == cpaddle_x || ball_y == cpaddle_y )
	{
		ball_x -= 2;
	}
	if( ball_x != paddle_x && ball_x != cpaddle_x || ball_x != paddle_stop_x )
	{
		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;
SDL_Surface *paddle = NULL;
SDL_Surface *cpaddle = NULL;

int randomNumber = rand();

int ball_x = 297;
int ball_y = 220;
int paddle_x = 25;
int paddle_y = 180;
int cpaddle_x = 583;
int cpaddle_y = 180;
int paddle_stop_x = 50;
int paddle_stop_y = 155;
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()
{
	if( ball_x == paddle_stop_x || ball_y == paddle_y )
	{
		ball_x += 2;
	}
    if( ball_x == cpaddle_x || ball_y == cpaddle_y )
	{
		ball_x -= 2;
	}
	if( ball_x != paddle_x && ball_x != cpaddle_x || ball_x != paddle_stop_x )
	{
		ball_x -= 1;
	}
}

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

	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");
	}

	ball = IMG_Load("1_a.PNG");
	if(!ball)
	{
		printf("Unable to load image: %s", SDL_GetError());
		return false;
	}
	else
	{
		printf("Loaded: 1a.png\n");
	}

	background = IMG_Load("2a.PNG");
	if(!background)
	{
		printf("Unable to load image: %s", SDL_GetError());
		return false;
	}
	else
	{
		printf("Loaded: 2a.png\n");
	}

	paddle = IMG_Load("3a.PNG");
	if(!paddle)
	{
		printf("Unable to load image: %s", SDL_GetError());
		return false;
	}
	else
	{
		printf("Loaded: 3a.png\n");
	}
	cpaddle = IMG_Load("3a.PNG");
	if(!cpaddle)
	{
		printf("Unable to load image: %s", SDL_GetError());
		return false;
	}
	else
	{
		printf("Loaded: 3a.png\n");
	}

	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 );
		apply_surface( paddle_x, paddle_y, paddle, screen );
		apply_surface( cpaddle_x, cpaddle_y, cpaddle, 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 this post


Link to post
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...

ball_y == paddle_y
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...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this