• Advertisement

Archived

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

Collision Detection Help

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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...

Share this post


Link to post
Share on other sites

  • Advertisement