Multiple rectangle collisions/overlaps

BlitzMax Forums/BlitzMax Beginners Area/Multiple rectangle collisions/overlaps

fhetskr(Posted 2012) [#1]
I was wondering if there is some way to test for a whole ton of rectangle collisions with on main rectangle without writing down each collision. It would make my life a while lot easier. I was thinking something like a list/array.
Thanks in advanced, and if there is only one way to do this, I guess I'm screwed...


Derron(Posted 2012) [#2]
simple and straight forward:
- loop through all rects (if mainrect is included: "if rect=mainrect then continue"
  - check if mainrect.x is within rect.x and rect.x+rect.w
  - check if mainrect.y is within rect.y and rect.y+rect.h
  -> "foundrect = rect; exit"
  - check if mainrect.x+mainrect.w is within rect.x and rect.x+rect.w
  - check if mainrect.y+mainrect.h is within rect.y and rect.y+rect.h
  -> "foundrect = rect; exit"


so it is just checking: is top left corner of my rectangle within one of the others - OR is my bottom right corner within one.

The "continue" command skips further commands and continues with the next rect in the "for loop".

the "exit" command exits the for loop - we found the one we want.


bye
Ron


edit: if you REALLY check vs 1000s of rectangles.
Use the "split region into sub regions" method:
screen = 4 parts, those 4 parts eg again 4 parts ...

Each rect keeps track of the regions it "uses".
Now check only rects using that regions too.

Last edited 2012


Wiebo(Posted 2012) [#3]
You could use a quadtree to eliminate most collision checks. I wrote a module for that. Take a look over here: http://code.google.com/p/noisycode/wiki/quadtree


fhetskr(Posted 2012) [#4]
thank you for the quick replies. at first, I didn't think this would work, as none of the rectangles have names, but I was just being stupid. I can just use the x,y,w,h variables from the objects they extend. thank you for all the help! Also: Wiebo, the module you made looks interesting and I might use it later, but it's a bit too complex for what I'm doing right now


Corum(Posted 2012) [#5]
Good to know, Wiebo.
I wasn't aware of that module.
Very handy! Thanks.

Last edited 2012


Wiebo(Posted 2012) [#6]
Not a lot of people know about my modules :) haha. I hope it is of use to you.