# SDL_Rect collision detection

## Recommended Posts

##### Share on other sites
Is it possible for you to give a description of what is actually happening? As in, is it not compiling or is it just not detecting collision at all? And please find the problem area and post it, as I personally would not like to wade through a large block of code.

##### Share on other sites
It's compiling and running, but the collision detection is not working on the paddle or the blocks (bricks). The collision box is where the paddle starts off, but when the user moves the paddle, the collision box does not move with it. It stays where the paddle starts off and is an invisible wall where the ball bounces off of it. As for the bricks, or blocks as they are called in the code, has a collision box, but another invisible one appears below it. I'm not sure where the problem is occuring in the code or why it is happening. I tried to give the code that I thought might be causing a problem in this post, but if you need more of a reference, the complete code is in the original post.

I think the problematic portions of the code would be this:

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;

//Get offsets
offset.x = x;
offset.y = y;

//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}

/////////////////////////////////////////////////////////

bool check_collision( SDL_Rect A, SDL_Rect B )
{
//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;

//Calculate the sides of rect A
leftA = A.x + 1;
rightA = A.x + A.w - 1;
topA = A.x + 1;
bottomA = A.y + A.h - 1;

//Calculate the sides of rect B
leftB = B.x + 1;
rightB = B.x + B.w - 1;
topB = B.y + 1;
bottomB = B.y + B.h - 1;

//If any of the sides from A are outside of B
if( bottomA <= topB )
{
return false;
}

if( topA >= bottomB )
{
return false;
}

if( rightA <= leftB )
{
return false;
}

if( leftA >= rightB )
{
return false;
}

//If none of the sides from A are outside B
return true;
}

//////////////////////////////////////////////

{
//Initialize the offsets
box.x = 300;
box.y = 500;

//Set the square's dimentions
box.w = 115;
box.h = 20;

//Initialize the velocity
xVel = 0;
yVel = 0;
}

Block::Block()
{
//Initialize the offsets
block1.x = 100;
block1.y = 200;
block1.w = 90;
block1.h = 26;
}

{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
switch( event.key.keysym.sym )
{

case SDLK_LEFT: xVel -= box.w / 6 ; break;
case SDLK_RIGHT: xVel += box.w / 6; break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
switch( event.key.keysym.sym )
{

case SDLK_LEFT: xVel += box.w / 6; break;
case SDLK_RIGHT: xVel -= box.w / 6; break;
}
}
}
////////////////////////////////////////////////

{
apply_surface( box.x, box.y, paddle, screen );
}

{
//Move the square left or right
box.x += xVel;

//If the square went too far to the left or right or has collided with the wall
if( ( box.x > SCREEN_WIDTH - box.w ) || ( check_collision( box, ball ) ) )
{
//Move back
box.x -= xVel;
}
else if( box.x < 0)
box.x += xVel;

}

##### Share on other sites
Surely you thought that posting that much code oustide of source tags would make it unreadable, right? There is a link to the faq at the top right corner of the screen. It will tell you which source tags to use.

##### Share on other sites
Hmmm... Your collision boxes are set correctly (I fill each one with red, and each matches up with their corresponding object). So, this means that your collision detection is doing something funky. And, by the way, you should make one single class for all three of these things and, also, you don't need to name functions differently if they're in different classes (for example, you don't need a show1() in Paddle and a show2() in Block). I'll look at your code further to see what the problem is.

##### Share on other sites
Hey, wait, you have two SDL_Rect's for each collision box declared in your program, once globally, and once in the class. Why so?

##### Share on other sites
Hmmm... Honestly, this is not very well-structured code. I don't understand why you did some of the things you did. This is not an SDL problem as much of a basic C++ one. Though I have no hard feelings, I'd request you to read a C++ book and then come back and fix this code. Sorry, but I don't think I can fix this unless I rewrite most of it, and I'm not willing to do that. I'll attempt to next morning, but I'd suggest you try to do so now. Again, I don't mean to be rude, but I really can't help you, for I'd be doing everything. Good luck!

##### Share on other sites
It's messy because I'm working with another programmer on it and we have been updating each other's version back and forth. I did not create the class structure or the functions for it, he did and I'm trying to help him with the paddle and its collision detection. Code clean up will come after we get the initial parts of the game working.

As for the rects globally and in the class, we had to do this because it would not work in collision detection. For instance, when the collision detection was called for (ball, paddle) inside the function move() there was an error stating that paddle was not in the scope of that funciton.

##### Share on other sites
From what i can see it seems you're using the wrong collision box when determining if the ball is colliding with the paddle or not in the ball's move() function:
void Dot::move()if( ( ball.x &gt; SCREEN_WIDTH - ball.w ) || ( ball.x &lt; 0 ) || ( check_collision( ball, box ) ) )if( ( ball.y &gt; SCREEN_HEIGHT - ball.h ) || ( ball.y &lt; 0 ) || ( check_collision( ball, box ) ) || (check_collision(ball, block1)) )

You are here checking if you are colliding with the SDL_Rect box you made globaly in the start of the program and is set in the main() function:
box.x = 300;box.y = 500;box.h = 20;box.w = 115;

This box is never updated, therefor it doesn't move with the paddel.

what you need to do is change the box you're checking collision with at the moment with the box form the paddle class

##### Share on other sites
Quote:
 Original post by MowkageIt's messy because I'm working with another programmer on it and we have been updating each other's version back and forth. I did not create the class structure or the functions for it, he did and I'm trying to help him with the paddle and its collision detection. Code clean up will come after we get the initial parts of the game working. As for the rects globally and in the class, we had to do this because it would not work in collision detection. For instance, when the collision detection was called for (ball, paddle) inside the function move() there was an error stating that paddle was not in the scope of that funciton.

Oh, OK, this makes more sense. I'm sorry for assuming you had created this by yourself (I made an @ out of you and me). Still, I suggest keeping your code clean from the start so that any problems you face are because of some minor mistake somewhere or a decrepit function or something. The mistakes are easier to fix when there isn't other stuff getting in the way. For example, this time, your double-declaration messed up your collision detection. It's good you have a solution now, though.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628316
• Total Posts
2982032

• 9
• 9
• 13
• 11
• 14