Advertisement

Box Collision

Started by January 15, 2001 01:24 AM
1 comment, last by vbisme 24 years ago
The ordinary box collision involves let's say 2 square: ._.........._ |_| and |_| normally you would do something like this to detect:
        
if (
    (box1.y + box1.height) >= (box2.y) &&
    (box1.y) <= (box2.y + box2.height) &&
    (box1.x + box1.width >= (box2.x) &&
    (box1.x) <= (box2.x + box2.width)
    }
{
//do whatever

}
        
Now then, how do you detect whether box1 hits box two on the top, left, right, or bottom so that you could act accordingly? Like if box1 hit box2 on the left: ._........._ |_| -> |_| the it would bounce back and move to as a result: ...._......_ <- |_| |_| (Ignore the dots) Edited by - vbisme on January 15, 2001 1:46:12 PM Edited by - vbisme on January 15, 2001 1:47:35 PM Edited by - vbisme on January 15, 2001 1:49:42 PM
Any body?
Advertisement
Consider that the position of the boxes at the new position doesn''t really tell you anything other than the fact that there is a collision. You need to consider the position of one box verses it''s last position.

A way of doing this is to draw a line from the centre of the box in the direction of travel and see which side of the other box it intersects. The boxes have to be regular or this won''t work quite so well. If they are irregular then you need to consider doing the line direction test (travel vector) from the corners of the first box, usually from two corners.

The line that intersects first is the one of concern because obviously that will be the corner that collided first.

This is very convenient because if you also want to resolve the collision 100% accurately then you can determine how far the offending line penetrated and subtract this amount from the new position of the cube.

This topic is closed to new replies.

Advertisement