Archived

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

Collision Detection with RECTs

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

Recommended Posts

Hi I have just got my first game to the stage where it is playable. It is a breakout clone and works better than I expected it to. The only problem is the collision detection between the ball on the blocks. At the moment, I have some if statements (probably not the best way and this probably answers my question, but hey it's my first game... ever) that check the blt destination rectangles for both the ball and bricks. i.e. if (rcBallDest.top == rcBlockDest.bottom) This works ok, but sometimes the ball is going through the blocks and bouncing off of the top inside. As far as I can tell, this is because there's too much happening that it's not checking the if statements quick enough. This could be completely wrong and I apologize for any newbie-ness. Are there any better (but still fairly simple) ways of detecting collision between RECTS? Thanks for any help PhilHalf Edited by - PhilHalf on October 16, 2001 10:54:58 AM

Share on other sites
The problem you have is simple:

Lets say you have a bounding box like this x1 = 0 y1= 0, x2= 10, y2 = 10. Now you''ve got a ball at x = 5, y 50. Your ball moves with a velocity of xv = 0, yv= -15 each frame.

By frame 1 your balls x,y position is x = 5, y = 35.
Frame 2 x,y position is x = 5, y = 20.
and by Frame 3 x,y position is x = 5, y = 5.

Now note in Frame 2 we did not yet make a collision with the bottom of our bounding box. But in Frame 3 we are already inside of the box. Yet because of the velocity the ball is moving at is never EQUIL to the bounding box''s Bottom we never made a collision.

The problem here is that your thinking of your box as if it were an actual cardboard box. If you never touch the sides you never make a collision. What you want to do is think of your box as if it were a block of rubber. If your ball is inside of the box then a collision has happened.

So logic similar to the following is in order:
if( rcBallDest.top <= rcBlockDest.bottom && rcBallDest.top >= rcBlockDest.top ) // Then collision has happened.

Share on other sites
Yeah, and don''t forget that if the test returns true, you''re going to need to reset the ball.top equal to the brick.bottom so that the bouncing rebound doesn''t turn out flaky.

Share on other sites
What happens if the ball goes all of the way through the "rubber" box before the if statment gets checked?

Is there a better way to check for a collision when you have velocities?

Share on other sites
For simplicity take a look at IntersectRect:

-=Description-
The IntersectRect function calculates the intersection of two source rectangles and places the coordinates of the intersection rectangle into the destination rectangle. If the source rectangles do not intersect, an empty rectangle (in which all coordinates are set to zero) is placed into the destination rectangle.

BOOL IntersectRect(
LPRECT lprcDst, // pointer to structure for intersection
CONST RECT *lprcSrc1, // pointer to structure with first rectangle
CONST RECT *lprcSrc2 // pointer to structure with second rectangle
);

Parameters
lprcDst
Pointer to the RECT structure that is to receive the intersection of the rectangles pointed to by the lprcSrc1 and lprcSrc2 parameters.
lprcSrc1
Pointer to the RECT structure that contains the first source rectangle.
lprcSrc2
Pointer to the RECT structure that contains the second source rectangle.
Return Values
If the rectangles intersect, the return value is nonzero.

If the rectangles do not intersect, the return value is zero.
========
so a check collision might be done like this:

RECT rcDest;
if( IntersectRect( &rcDest, &rcBallDest, &rcBlockDest ) )
{
// collision
}
else
{
// no collision
}

• 16
• 11
• 11
• 9
• 49
• Forum Statistics

• Total Topics
631394
• Total Posts
2999749
×