Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I initially tried out implementing rectangular intersection, which works well. However, when I have to apply the physics system, such as velocity, acceleration, and directional vectors, I would have to find a way to determine which side of the rectangles collide. Now, in my system, there is no rotated rectangle, so this simplified the problem. However, I couldn't find an easy way to determine which rectangle side collided. I have once deal with this problem before but failed miserably.

What I did in the past is determine the distance between each parallel rectangular sides and check if the distance is close to 0 (use some initially defined distance range) or is 0. However, for floating-point arithmetic, this proves to be unstable because of unknown time elapse. Sometime, the rectangles would actually intersect each other before it meets the defined range.

On the other hand, I was thinking about spawning multiple rectangles, each rectangle for each sides. However, after thinking again, it would be the same thing as having a parallel side with distance range checking, just that that distance range is the width of each mini-rectangle.

Therefore, any suggestion to this problem?

share|improve this question
    
Are you using discrete or continuous position updates? (are you updating your velocity by the acceleration once every frame and then calculating the position, or using a function to extrapolate the position) –  darthfett May 31 '12 at 5:56

2 Answers 2

up vote 0 down vote accepted

A line-to-line intersection test on each edge of the rectangle might help you figure out the colliding side. http://keith-hair.net/blog/2008/08/08/line-to-polygon-intersection-data/

Another way might even be more simple. After determining a rectangle has a collision, you could check the angle of collision with an "Atan2" function. If the appearence does not have to be perfect, checking if the angle is in range of top, bottom, left or right could work.

share|improve this answer
    
How would line to line intersection test help me determine the colliding side? Correct me if I am wrong, but by using line intersection test, for example, the right side of rectangle. If the top and bottom side of the rectangle intersect with left side of another rect, then I would get collision on the right side of the rectangle. However, if I remembered correctly, when I implements this, then collision at the edge will cause some problem. –  user1542 May 29 '12 at 9:21
    
However, your angle test seems worth trying. –  user1542 May 29 '12 at 9:21

Adapted from my answer to "Which Side Was Hit?":

I suggest computing the Minkowski sum of B and A, which is a new rectangle, and checking where the centre of rectangle A lies relatively to that new rectangle (to know whether a collision is happening) and to its diagonals (to know where the collision is happening):

float w = 0.5 * (A.width() + B.width());
float h = 0.5 * (A.height() + B.height());
float dx = A.centerX() - B.centerX();
float dy = A.centerY() - B.centerY();

if (abs(dx) <= w && abs(dy) <= h)
{
    /* collision! */
    float wy = w * dy;
    float hx = h * dx;

    if (wy > hx)
        if (wy > -hx)
            /* collision at the top */
        else
            /* on the left */
    else
        if (wy > -hx)
            /* on the right */
        else
            /* at the bottom */
}
share|improve this answer
    
This helped a lot, thanks! –  Spencer Jun 18 at 23:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.