Does anybody know of any source code or resources for bounding box collision detection, like a website or something? Thanks for your help!
Printable View
Does anybody know of any source code or resources for bounding box collision detection, like a website or something? Thanks for your help!
You dont need a website, all you need is a brain, and a bit of thought!
This is the problem with so many programmers these days. If they have a problem, the first thing they do is jump on the web and ask someone to show them how to do it rather than having a go at doing it themselves. My advice, if you want to learn and become good at what you do, sit down and think about it for a while, and then try and do it yourself.
However, I am feeling cheerful this morning and I will give you a few pointers.
If you already know the bounding rectangles of the objects, then you know all you need to know already! You just need to perform a few simple tests (this method i'm describing is designed to help you think, not to give you the most elegant and efficient solution straight up).
First, test to see if the location of the left hand side of the rectangle of the first object is less than or equal to the right hand side of the rectangle of the second object, and that the right hand side of the first rectangle is greater than or equal to the left hand side of the second. If it is then we have a possible overlap/collision. You then just need to test if the top of the first rectangle is higher or equal to the bottom of the second, and that the bottom of the first rectangle is higher or equal to the top of the first. Here is example code:
Alternatively, if you have two RECT structures filled out, you could use the Windows API function IntersectRect(). This function actually creates a rectangle from the intersection of 2 other rectangles, but returns FALSE if the rectangles dont intersect (ie. dont collide).Code:RECT obj1, obj2;
if((obj1.left <= obj2.right && obj1.right >= obj2.left) || (obj1.right >= obj2.left && obj1.left <= obj2.right))
{
if(obj1.top <= obj2.bottom && obj1.bottom >= obj2.top)
{
// BOOM! We have a collision
}
}
I hope this helps. And next time, put some thought into it yourself!
Good luck!
U.
Got it. It is simplier than I thought. And I did ask it on the board before I tried it, but that was just to get a jump start on it.
well my friend, as you can see the solution is often easier than you think.
from now on, sit down and have a go at it yourself. you will feel like you have accomplished alot more if you do, rather than getting everyone else to do it for you.
if you think about it, if you do that all the time, then what code have you written?? really all you've done is collected other peoples' snippets and compiled them!
trust me, give it a go from now on, and i'll bet you suprise yourself!
good luck!
U.
I personally agree with Uraldor's philosophy, and I try to practice it as much as possible. Another good idea is to read some tutorials on the topic and then try to write the code yourself. If you are having a specific problem when you try that, and you can't figure it out, that is the best time to ask for help.
It's good to see another person who has the right attitude when it comes to learning :D