#### Archived

This topic is now archived and is closed to further replies.

# Collision Detection Help

This topic is 6072 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hey, i am trying to make a breakout game. My collision works for the top and bottom but it doesnt work for the left or right sides.I am using blt to display the images.Heres my function. if((x >= bc2Dest.top && x <= bc2Dest.left || x >= bc2Dest.right && x<= bc2Dest.bottom) && (y >= bc2Dest.bottom && y<= bc2Dest.right || y >= bc2Dest.top && y <= bc2Dest.left )) thanks. "Good,evil,I got the gun" Ash- Evil Dead

##### Share on other sites
if (x+ball_width >= destRect.left && x <= destRect.right && y+ball_height >= destRect.top && y <= destRect.bottom)
{
// the ball hit that specific block
}
else
{
// no hit happened
}

(x,y) is the location of the ball -- notice that the x, y coordinate specify the location of the ball at it''s upper left corner and not the middle of the square that the ball is represented by.

ball_width, ball_height -- the ball''s width and height respectively

destRect -- the block''s rectangle

So you basically go through all the blocks for your current level, and check to see if the ball''s rectangle (we get that by using the x,y coordinates and the ball''s dimensions) has collided with any of the blocks'' rectangle on the screen.

Hope this helps...

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 27
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
631407
• Total Posts
2999914
×