Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / detect if two grids overlap

Author
Message
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 21st Aug 2011 23:39
I'm having a brain fart as this problem feels like it should be super simple.

Say I have two boxes, A and B. 'A' can be 1x1 or up to 4x4, same thing goes for box 'B'.

Because one box could be completely enclosed by the other but A isn't always bigger than B, or vice-versa, so I'm having trouble making a simple overlap function. Both boxes lie inside a larger grid, 12x6. Think inventory object placement.

I tried this thinking it work, but after thinking it through it won't.



Hopefully someone knows what I'm talking about.

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 21st Aug 2011 23:52
I think I worked something out, however using a nested FOR loop for this seems inefficient to me.



If it matters to anyone:



WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 22nd Aug 2011 00:27
Quote: "if x >= gridX and x <= gx and y >= gridY and y <= gy then exitfunction 0"

To speed it up a tad you could nest the IF's.

Warning! May contain Nuts!
BMacZero
20
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 22nd Aug 2011 01:07 Edited at: 22nd Aug 2011 01:09
It seems to me that if two rectangles overlap, at least one of their edges must fall between the two corresponding edges on the other rectangle.

I played with this idea some, and I think it is true in one dimension that if you find the leftmost edge of the two right edges of the two rectangles and the rightmost edge of the two left edges of the rectangles, you need only see if the the left one is less than the right one (less than or equal to, in this case, because the rectangles include the edges). If it is, they overlap horizontally.

In two dimensions:


That seems really crazy but I tested every case in one dimension and it worked for all of them.

EDIT: Fixed me being dumb with min and max

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 22nd Aug 2011 05:18
I guess all I had to do was sort the two different boxes. It does look odd but it seems to work. Kudos to you BMac!

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 22nd Aug 2011 05:41 Edited at: 22nd Aug 2011 06:02
hahah dang... I just came across a problem where I need to figure out if one rotated rectangle intersects or is a part of another rotated rectangle, and I hoped this was about that.


WAIT just thought of a solution

[edit]
for anyone who cares, you have two rectangles A and B (with vertices A1, A2 ... A4, B1 etc)
using the SameSide function described here (http://www.blackpawn.com/texts/pointinpoly/default.html), we can make a function:

(since rectangles are convex quadrilaterals, the verbose function name might not be needed. it's just there for clarity)

Since a rectangle is a space bounded by linear functions, for a rectangle A to have space that also belongs to a rectangle B, a vertex of A is in B, and/or a vertex of B is in A. So...
(with the pointInConvexQuadrilateral function renamed to the more concise pointInQuad(p, a,b,c,d))


not the most efficient way to do it, it's probably not the best algorithm, and the code itself could be sped up at the cost of readability, but it's good enough for my purposes. (my purposes are camera-culling which is done once per frame, on 30-40 rectangles, so that's not bad at all.)


Why does blue text appear every time you are near?

Login to post a reply

Server time is: 2026-07-10 10:19:46
Your offset time is: 2026-07-10 10:19:46