Bounding box collision detection?

This is a discussion on Bounding box collision detection? within the Game Programming forums, part of the General Programming Boards category; Does anybody know of any source code or resources for bounding box collision detection, like a website or something? Thanks ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    162

    Question Bounding box collision detection?

    Does anybody know of any source code or resources for bounding box collision detection, like a website or something? Thanks for your help!

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    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:

    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
    	}
    }
    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).

    I hope this helps. And next time, put some thought into it yourself!

    Good luck!
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    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.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    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.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    It's good to see another person who has the right attitude when it comes to learning
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Collision Detection Problems
    By Dark_Phoenix in forum Game Programming
    Replies: 1
    Last Post: 12-17-2006, 02:25 PM
  2. Collision Detection
    By Grantyt3 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2005, 03:21 PM
  3. Bounding Box Collision...not working
    By psychopath in forum Game Programming
    Replies: 4
    Last Post: 05-31-2005, 06:31 AM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 07:00 PM
  5. bounding box collision detection
    By DavidP in forum Game Programming
    Replies: 7
    Last Post: 07-07-2002, 11:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21