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; Has anybody ever used bounding box collision detection? I am wanting to optimize my collision detection code, and I was ...

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738

    Question bounding box collision detection

    Has anybody ever used bounding box collision detection? I am wanting to optimize my collision detection code, and I was told it might help if I did a bounding box collision check before I did any of my pixel perfect collision checking. So if the boxes didnt collide then I wouldnt have to go in and do pixel perfect calculations, but if they did collide then I would go in and do the calculations for my pixel perfect collision detection which I have set in place already.

    So I am thinking about implementing this bounding box collision detection above the pixel perfect code already implemented, but I have no idea on how to do it. I was told you see if the two boxes intersect anywhere.

    Anybody had any experience with it? Any quick examples that I can look at to start me off, or websites?
    My Website

    "Circular logic is good because it is."

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    All you have to do is create a RECT which surrounds each object, and then use the IntersectRect function to see if they intersect. The RECT structure and IntersectRect are both contained in windows.h.

    Code:
    typedef struct _RECT { 
      LONG left; 
      LONG top; 
      LONG right; 
      LONG bottom; 
    } RECT, *PRECT;
    
    BOOL IntersectRect(
    LPRECT lprcDst, //Pointer to RECT structure which will contain the area of intersection (can't be NULL)
    const RECT *lprcSrc1, //Pointer to first RECT
    const RECT *lprcSrc2 //Pointer to second RECT
    );

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    I believe that there are a couple tutorials on bounding box collision on Gameprogramming.com under Tutorials > C or maybe C++...at least I think that is where they were. However, they aren't very good IMO because it is just source code with some comments, but maybe they can help you out a little.

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    okay, xquared posted that as I typed mine, so I'll ask him a quick question that maybe someone else is wondering about...how exactly does that IntersectRect function work? I looked at the code, but I didn't quite get it...

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    PHP Code:
    #include <windows.h>
    #include <stdio.h>
    #include <conio.h>

    int main()
    {
        
    RECT SpriteOne;        // Imaginary sprite
        
    RECT SpriteTwo;        // Imaginary sprite
        
    RECT SpriteResult;    // Sprite to hold overlapping area
        
    SetRect(&SpriteOne,50,50,100,100);   // A 50x50px sprite, with top-left @ (50,50)
        
    SetRect(&SpriteTwo,300,300,425,350); // A 125x50px sprite, with top-left @ (300,300)
        // Loop until they overlap
        
    while(!IntersectRect(&SpriteResult,&SpriteOne,&SpriteTwo))
        {
            
    // Simulate movement of the sprites
            
    SpriteOne.bottom += 1;
            
    SpriteOne.left += 1;
            
    SpriteOne.right    += 1;
            
    SpriteOne.top += 1;
            
    SpriteTwo.bottom -= 1;
            
    SpriteTwo.left -= 1;
            
    SpriteTwo.right -= 1;
            
    SpriteTwo.top -= 1;
        }    
        
    // Output the coordinates of the area where the sprites overlap
        
    printf("The rectangles have intersected!\nHere are the coords for pixel-perfect examination:\nLeft: %ldpx\nTop: %ldpx\nRight: %ldpx\nBottom: %ldpx\n\nPress any key to continue...",SpriteResult.left,SpriteResult.top,SpriteResult.right,SpriteResult.bottom);
        
    getch();
        return 
    0;

    This would allow you to do pixel-perfect collision detection only where the sprites overlap. Don't forget that .bottom = .top + height, and .right = .left + width.

  6. #6
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    thanks for that info XSquared, it could help me out a lot. and thanks for the site, blackrat364, i will take a look at it..
    My Website

    "Circular logic is good because it is."

  7. #7
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    >how exactly does that IntersectRect function work?

    a simplified version might look like

    Note: i havn't tested this this, it is off the top of my head... and all concept work. so no guarantees it even works.

    Code:
    #include <math.h>
    #include <windows.h>
    
    // not sure exactly what LPRECT lprcDst is so im gonna ignore it
    BOOL SimpleIntersect(const RECT *lprcSrc1,const RECT *lprcSrc2)
    {
    	if(lprcSrc1->right < lprcSrc2->left && lprcSrc1->left > lprcSrc2->right)
    		if(lprcSrc1->bottom < lprcSrc2->top && lprcSrc1->top > lprcSrc2->bottom)
    			return TRUE;
    		
    	return FALSE;
    }
    ::edit::
    Rect collision is done in concept by checking to see if either the bottom left or top right vertice(x,y) is inside of another rects.
    ::edit::
    Last edited by no-one; 07-06-2002 at 02:23 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    If you insist on using bounding box collision detection might I add one thing. Just write the intersection function yourself. Here is why:

    A) Chances are it is going to be faster
    B) You will learn more doing it yourself
    C) Non M$ specific

    Just my .02, I wrote my own and I was glad I did. It doesn't take long at all, a few SIMPLE calculations and you are on your way.

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 Crossbow in forum Game Programming
    Replies: 5
    Last Post: 07-13-2002, 05:20 PM

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