Jump to content
  • Advertisement

Archived

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

vbisme

Box Collision

This topic is 6424 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

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

Share this post


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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!