Archived

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

PhilHalf

Collision Detection with RECTs

Recommended Posts

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


Link to post
Share on other sites
evaclear    166
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 this post


Link to post
Share on other sites
WORF    112
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 this post


Link to post
Share on other sites
Gladiator    127
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
}

Share this post


Link to post
Share on other sites