Archived

This topic is now archived and is closed to further replies.

Smoke

Collision Detection

Recommended Posts

Can any tell me how i would test collision between a bounding circle and a bounding box? if so how could i quarter the bounding circle to test which one of the four quaters the rectangle is in? any help would be great! Thanks

Share this post


Link to post
Share on other sites
Before I get carried away...

What's the scenario?

It will make a big difference if, say, the box is always inside the circle somewhere, *usually* in the circle, or hardly ever in the circle.

It also matters how exact you want it to be, and how fast.


[edited by - Nypyren on October 22, 2002 2:09:21 AM]

Share this post


Link to post
Share on other sites
1: If your bounding box always has edges parallel to X and Y axes (i.e. not "rotated"), life is a lot easier. If not, you have some choices: use heinous math functions dealing with slope/etc, use vector projections, or transform the box/sphere system so that the box has sides parallel to the X/Y axes.

2: In either case, you will probably want to translate the system so that the circle is at (0,0) (just subtract the position of the center of the circle from each box corner's position and use (0,0) for the center of your circle)

// The following steps assume you want true/false results for each quadrant of the circle that might be touching the bounding box, and both the circle and the box are "solid".

3: I'll assume you mean the quarters of the circle are separated by the axis lines and the sphere is translated to (0,0)

4: Find out which quadrant each point of the box is in (don't bother with whether they are inside the circle yet). I'm assuming the box is in the form x1,y1,x2,y2 now, where x1 less than x2 and y1 less than y2. I'm going to make a bitfield here, so watch yourself. (I'm just crazy like this.)

If you have x1/x2/y1/y2 on the zero line, and you care, you will need to tweak with the code.

5: With the bitfield, do a closer inspection on the box/circle to check the range of each corner from the center of the circle.



      
// Step 4

char zones = 0;
if (x1 > 0) zones |= 0x1; // entirely to the right of Y axis

if (x2 < 0) zones |= 0x2; // entirely to the left

if (y1 > 0) zones |= 0x4; // entirely above X axis

if (y2 < 0) zones |= 0x8; // entirely below


// Step 5

switch(zones)
{
case 0: //touching everything, or on an axis

break;
case 1:
if (x1 < radius) //the two right-hand quads are hit.

break;
case 2:
if (x2 > -radius) //the two left-hand quads are hit.

break;
case 4:
if (y1 < radius) //the two bottom quads are hit.

break;
case 8:
if (y2 > -radius) // the two top quads are hit.

break;

case 5:
if (x1*x1+y1*y1 < radius*radius) //the bottom right quad is hit

break;
case 6:
if (x2*x2*y1*y1 < radius*radius) //the bottom left quad is hit

break;
case 9:
if (x1*x1*y2*y2 < radius*radius) //the top right quad is hit

break;
case 10:
if (x2*x2+y2*y2 < radius*radius) //the top left quad is hit

break;
}



If you need more (like transformations), let me know.

[edited by - Nypyren on October 22, 2002 3:10:59 AM]

Share this post


Link to post
Share on other sites
Well i''m not sure I understand all of that =c(
Especially step 2 how can i subtract the circle position from the rectangles corner points?

The situation i''m using this in is a Zelda(Gameboy version) clone. It''s really just a learning project for me.

The collision is for the sword attack. I was going to do just a simple bounding box collision for the sword (to see if anyone has been hit) but the sword swings in an arc obviously. So a box would give an area at the top corner that really shouldn''t be there and in the actual Zelda games the collision for the sword is very exact. My solution was to use a bounding circle divide into for by the XY axis and just test for the section that would corrisopnd to the animation. But the more i read about 2D collision detection it seems less likely that the actual Zelda game would use this process, but i aint sure. I also considerd using pixle precision detection but the sword is part of the character, could i bound the pixle detection to just the sword? Could I also use pixle detection from the sword against enemy bounding boxes to save processing time??

I''m not sure how it is actually done in Zelda. I even have a sneaking suspicion that sword is a seperate sprite alltogether
but that still dosen''t over come the box issue.

Wow that was way to long.. anyway any ideas about the best way to do this?


PS. I''m a new guy to this and i''m really only scripting this game in Java Script using a game creation engine called Sphere (http://sphere.sourceforge.net/ if your interested)but pseudo code and C++ examples still help! Thanks!!

Share this post


Link to post
Share on other sites