Sign in to follow this  
phil67rpg

bounding box

Recommended Posts

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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).

Share this post


Link to post
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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this