bounding box

Started by
6 comments, last by Wan 15 years, 2 months ago
I want to use a bounding box for collision detection but am unsure of how to proceed.
Advertisement
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.
I am using c++ and direct x
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.}
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.
Quote:Original post by phil67rpg
I 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).
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)
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
Quote:Original post by medevilenemy
why 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]

This topic is closed to new replies.

Advertisement