Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / 2D-Bounding box collision.

Author
Message
MegaCodeOne
10
Years of Service
User Offline
Joined: 16th Jan 2014
Location:
Posted: 1st Feb 2014 03:38
would anyone know how to write a small piece of code that would not only tell if two boxes have touched but also which side of the box it hit? I.e. Top,bottom, right or left.

Thanks
ScottieB
ShellfishGames
11
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 1st Feb 2014 09:54
Actually I've struggled with just that exact problem for quite some time throughout the last few weeks and came up with a few solutions. Haven't got any more time now to look for the code but I'll post it as soon as I get back home.

A possible approach is to check line vs Box with all 4 edges of the one bounding box against the other, this way you get all the edges colliding. You could do this for both boxes one after another if you need the colliding sides of both of them - not very efficient but at least a working solution.
Alternatively just check each line of box A vs each line of box B - line vs line collision is very easy and fast to implement using the cross product.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 1st Feb 2014 10:26 Edited at: 1st Feb 2014 10:26
First, check to see the boxes are touching/overlapping. Next, depends on how you expect your collision to work. My approach makes the assumption that box A is never inside box B. (but I could adjust it to account for that)
Assuming the boxes only hit from the outside, then the right side of box A can never hit the right side of box B, only its left. So I subtract the left side of box A from the right side of box B. If the value is between 0 and some threshold (to allow for a small amount of overlapping) then the left side of box A has been hit (or the right side of box B). I take a similar approach to the other 3 sides.

Each side hit contains a different number, each representing a binary digit so that when added up you get a single number which can represent multiple hits.
Left=1, right=2, top=4, bottom=8

Obviously you can never have both left and right sides hit simultaneously unless the boxes are the same size. But, the right and top could hit at the same time, such as in a corner hit. For that, the function returns 6. Bottom left would return 9, and so on.




ShellfishGames
11
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 1st Feb 2014 12:51
I assumed we were talking about rotated bounding boxes. Otherwise, Phaelax solution indeed seems pretty straight forward.

MegaCodeOne
10
Years of Service
User Offline
Joined: 16th Jan 2014
Location:
Posted: 2nd Feb 2014 02:59
Well, That about sums it up for me.
Thank you!
Your code is readable.
Thanks again for the help.

Login to post a reply

Server time is: 2024-04-24 08:03:56
Your offset time is: 2024-04-24 08:03:56