Checking for overlap with rectangles and circles
Displaying 1-20 of 21 total.
12 next
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
Sungam

Does anyone have a good function, to check if a circle and a rectangle (bonus points if it can also check between two circles) overlap?

I managed, after hours of debugging, to get rectangle overlapping working (right idea from the start, but kept feeding it wrong values :p)... but when I attempt to imagine a function that does the same for circles, I get a little dizzy.

I'm certain that there's a fantastically simple way of doing it, to which I will say 'duh' quite loudly. Can anyone point me in the right direction?

Posted on 2004-09-19 19:43:44

mcgrue

Sungam:

Draw an invisible rectangle around your circles for collision checking. If needbe, you can easily modify a simple rectangle-maker that takes a midpoint and a radius.

Posted on 2004-09-19 20:56:31

Omni

Between 2 circles:

Get the radius of each circle. Then get the distance between the origin points of the two circles using the 2D distance formula,

something like sqrt((x1-x2)^2 + (y1-y2)^2),

You know, with x1, y1, and x2, y2 for the two midpoints, ie, get the square root of the squares of the deltas, or something, it's a really simple math formula and you can look it up.

Anyway, that formula gives you the distance between two points.

Then, if that distance is less than either of the radii of the two circles, then the circles collide.

Posted on 2004-09-19 21:34:24

Gayo

If the distance between the centres of two circles is less than the sum of the radii of the circles, they overlap.

Edit: Doh, I was beaten to it. Need to pay more attention.

Posted on 2004-09-19 22:05:52 (last edited on 2004-09-19 22:08:09)

Omni

Gayo had it better though--the distance must be less than the sum of the radii, which is better than checking each radii individually, which is more like doing circle-point collision.

Posted on 2004-09-19 22:11:29

Sungam

Quote:Originally posted by mcgrue

Sungam:

Draw an invisible rectangle around your circles for collision checking. If needbe, you can easily modify a simple rectangle-maker that takes a midpoint and a radius.


I'm probably just failing to see the obvious here, but could you elaborate a bit on this? If all you would do is draw a rectangle with the height and width of the circle's vertical and horizontal radii, centered around the center of the circle, then I expect it would be much too inaccurate to be useful.
If we're talking about drawing multiple rectangles inside the circle, and checking for each one (I think I've seen a graphical version of that somewhere...), I'm afraid I'll need a few more pointers.

Omni and Gayo, about two circles
Excellent. That, I understand. And the math rings faint bells from high school. I'm really starting to wish I'd paid just a little attention back then. I swear, if the teacher had suggested that I could use this stuff for game programming, I'd never have told her to mind her own business.
That distance formula also helps me with another problem I had, so you're looking at quadruple points, I think.

Posted on 2004-09-19 22:23:37

mcgrue

Sungam, I was explaining, crudely, the concept of Dirty Rectangles as bounding boxes.

Googling for 'Dirty Rectangles' will illuminate you.

It is not as accurate as other methods. It is, however, fast.

Posted on 2004-09-19 22:26:22 (last edited on 2004-09-19 22:26:23)

Omni

Yeah, the idea with a rectangle around the circles:

In order to get perfect circle-rectangle collision...well, I can't think of how I'd do that, and the perfect way would likely be very slow.

So, McGrue says, form a rectangle around your circles using their horizontal and vertical radii. I mean, unless your circles are like, 180x60 large, there's no major loss of accuracy for something small, like a 16x32 sprite against a 8x8 circle.

Now, if you are using incredibly large things...I'd say, either break it into smaller rects, or...I dunno. There's no simple way to do near perfect circular or pixel-detection that's also very fast and simple to explain, I think.

But then, what do I know?

By the way, for a brush up on all those trig math skills you thought you didn't need:

Sin and Cos: The Programmer's Pals!
(and a ton of other useful math techniques.)

Posted on 2004-09-19 22:30:12 (last edited on 2004-09-19 22:30:49)

mcgrue

Thanks omni, added that to the links page.

Posted on 2004-09-19 22:42:24

Sungam

Quote:Originally posted by Omni

Yeah, the idea with a rectangle around the circles:

In order to get perfect circle-rectangle collision...well, I can't think of how I'd do that, and the perfect way would likely be very slow.

So, McGrue says, form a rectangle around your circles using their horizontal and vertical radii. I mean, unless your circles are like, 180x60 large, there's no major loss of accuracy for something small, like a 16x32 sprite against a 8x8 circle.

Now, if you are using incredibly large things...I'd say, either break it into smaller rects, or...I dunno. There's no simple way to do near perfect circular or pixel-detection that's also very fast and simple to explain, I think.

But then, what do I know?

By the way, for a brush up on all those trig math skills you thought you didn't need:

Sin and Cos: The Programmer's Pals!
(and a ton of other useful math techniques.)


What I'm getting at is a tactical battle system which is pixel-based, not tile-based. I think La Pucelle (or one of those) does the same thing, though I haven't seen or played it. So for targeting stuff (say, choosing someone to attack) would have a set distance from the initiator - not just directly above, below or to the sides. In some cases (ranged attacks, magic, whatever), those ranges might become rather big, and with that comes a fairly high level of inaccuracy (I think.. maybe I'm overestimating it).
Anyway, it's only just occurred to me that I could really just treat the entity as a circle, slightly larger than it really is, and go for that. Or just decide that the range has to hit the center of the entity's hot spot. I think either of them would be fairly close, as long as the entities aren't huge.

Thanks much for the help, everyone, on all the topics. I think I can figure something out from this. And I'm reading up on that math stuff right now! I remember my calculator could do stuff like this. If I hadn't been too busy programming lame text-adventures in TI-Basic, maybe I'd bothered to use it for that ;)

Posted on 2004-09-19 22:44:55

RageCage

oh yeah? that was actually one of the first things I made. unfortunately my programming skills increased so much durring it that I couldent stand to work on it any more with the crappy ways I was doing things. its pretty muich working though, if you want to take a look at how I did things.

Posted on 2004-09-19 23:52:50

Gayo

I'm not sure how picky you want to be about the square-to-circle, but if you want to make a circle that is circumscribed about a square, the radius of the circle should be equal to half the diagonal distance across the square. That is to say, for an inscribed square with width w and height h, the circumscribed circle would have a radius of sqrt((w*w)+(h*h))/2. This will ensure that each vertex of the square is located on the circumference of the circle. I'm not sure if you care about it being that accurate, though.

Posted on 2004-09-20 03:02:18 (last edited on 2004-09-20 03:08:14)

Technetium

I have one of my usually-inefficient methods to add here. This one is for testing overlap with any two irregularly-shaped images.

You need to create a new image the size of the screen. Fill it black. Then silhouette the first image in solid white, at the same coordinates as on the screen image. Then setlucent(50) and silhouette the second image in black at the same coordinates as where it is on the screen image. Then, within nested for-loops for X and Y, test each pixel color. Say something like 'if(getr(getpixel(X,Y))>0 && getr(getpixel(X,Y))<255)' for the condition. If that is true, then there is overlap.

This would probably kill your framerate at anything higher than 320x240.

Posted on 2004-09-20 18:54:32

Gayo

That is the worst idea I have ever heard.

Posted on 2004-09-20 23:21:36

mcgrue

Quote:Originally posted by Gayo

That is the worst idea I have ever heard.


Have I ever told you about my plans to copulate with a blender?

Posted on 2004-09-20 23:27:37

Omni

Wait!

Variation on Troupe's method.

Blit both objects to a testing buffer, then SCALE THE BUFFER DOWN to something small, like 20x20 pixels. The basic shape is there, so you can still check for obstructions, but extremely quickly.

Then do your testing! You can even say the size of the test buffer is the detail of the obstruction check. The less detail/resolution, the much faster the check is.

It's a lot faster than scanning through 320x240--use different sizes for varying levels of detail.

Posted on 2004-09-21 01:08:06

Gayo

Good god, people, you'd be better off just checking if every single pixel of the rectangle is within the circle. It's not that hard. It's slow as shit on toast, but it's still faster than this blitting crap.

Posted on 2004-09-21 02:24:24

Omni

Well, I mean, that's what we're doing. We're just doing it in pixels instead of ...uh...

Heck, you know, GetPixel is probably slower than any type of looped math, so nevermind. If you're looking for a fast solution, don't use the graphical method.

Posted on 2004-09-21 04:19:57

rpgking

Bounding boxes are the best solution for everything...

Posted on 2004-09-21 09:21:50

zaril

Best solutions so far in my opinion:

if (R) is a rectangle and (C) is a circle.

1. if the (R) is far from a square shape, you could by some hacking split it up to two more square:ish rectangles or even at best, two squares and through some simple 'radius = sqr(x^2+y^2) / 2' math get circles for them, then do circle collision.

2. if you can lose some small accuracy, just make a square obstruction out of the (C) and do rectangle vs rectangle collision.

when you think of it, in most games rectangle and circle collision is not near as precise as you could be, but any cool programer knows you sacrifice accuracy for speed, because no one wants AWESOME collision code, but something that runs at 3 FPS. well, except retards.

Posted on 2004-09-21 14:16:17


Displaying 1-20 of 21 total.
12 next
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.