Public Group

# bounding box

This topic is 3612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I want to use a bounding box for collision detection but am unsure of how to proceed.

##### Share on other sites
What language and graphics libary are you using? In general you check the size of the two images bounding boxes in relationship to each other. An example in psudeocode: "if(manLeft > treeRight){stop}" And you will later have to make sure his top is below the tree top and his bottom is above the bottom. That is pretty much the basics if you want to know more specifics I might be able to help, if not I know someone will be able to. You can search Google for "bounding box collision detection" and there is a few articles. Hope that helps.

##### Share on other sites
I am using c++ and direct x

##### Share on other sites
Search bounding box collision detection on google and there is a gamedev article. Its a bit old but you should be able to get the theory and implement it from there. You will have to check two sides of the sprite against each other, likr this:
int manX, manY; int manLeft = manX;int manTop = manY;//etc,if(manLeft < treeRight){//stop, pick up item, take damage, etc.}

##### Share on other sites
The simplest way to detect the intersection between two axis aligned bounding boxes (AABBs) would be something like:

struct FLOATRECT{    float x,y,width,height;};bool Intersect(const FLOATRECT &A,const FLOATRECT &B){    return(A.x+A.width>B.x && A.x<B.x+B.width && A.y+A.height>B.y && A.y<B.y+B.height);}

If you run through this in your head, you can see it only returns a positive result if there is an intersection between the boxes along both axes.

##### Share on other sites
Quote:
 Original post by phil67rpgI am using c++ and direct x

Then you might be interested in the D3DXComputeBoundingBox and D3DXBoxBoundProbe functions in the SDK (check the documentation for more information).

##### Share on other sites
why does that sound like cheating to me :-p

Seriously, though, basic rectangular bounding boxes are fairly simple (as EasilyConfused posted above). If it is relevant, you may want to choose which edges you are checking based on what directions your objects are moving in... that might save a tad bit of processing (speed things up a tiny bit if you are doing a lot of collisions)

##### Share on other sites
Quote:
 Original post by medevilenemywhy does that sound like cheating to me :-p

I know your only joking, but I believe that in general, relying on the functionality that is available in the SDK or engine you're already using is a good thing, unless there are very good reasons to implement your own version (some application specific behavior perhaps). It's already written, it's already debugged, it's already optimized for maximum performance (although the latter in this case is trivial with the simple maths involved), it's already documented and others may already be familiar with it.

Of course having a proper understanding of bounding volumes and their role in collision detection is still important. If building your own collision testing code helps you achieve that, then I suppose that could be a valid reason. [smile]

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633679
• Total Posts
3013294
×