Rectangle to Rectangle collision

BlitzMax Forums/BlitzMax Beginners Area/Rectangle to Rectangle collision

YellBellzDotCom(Posted 2010) [#1]
Hello!
I am trying out the BMax demo and so far love it! One thing I was looking for is an BMax Function for Rectangle to Rectangle Collision

I understand that I can write a simple...
Method CheckRectCollision(r1X:FLOAT,r1Y:FLOAT,r1W:INT,R1H:INT,r2X:FLOAT,r2Y:FLOAT,r2W:INT,R2H:INT)
     If(r1X>=r2X AND r1X<r2X+r2W AND r1Y>=r2Y AND r1Y<r2Y+r2H)
          Collide = "TRUE"
     ENDIF
END METHOD

Or something along those lines, but I was wondering if there was a built in function to already do this.

I've been searching through the forums and the code archives but cant seem to find something as simple as this.

Thanks for the help!


kragoth74(Posted 2010) [#2]
You're right. BMax is strangely missing a RectsOverlap function, like good old Blitz.


therevills(Posted 2010) [#3]
I normally use this:

Function RectsOverlap:Int(x0:Float, y0:Float, w0:Float, h0:Float, x2:Float, y2:Float, w2:Float, h2:Float)
	If x0 > (x2 + w2) Or (x0 + w0) < x2 Then Return False
	If y0 > (y2 + h2) Or (y0 + h0) < y2 Then Return False
	Return True
End Function


BMax is strangely missing a RectsOverlap function

It isnt hard to write your own though...


Bremer(Posted 2010) [#4]
I have a related question regarding rect to rect collision.

For a card game, where i would want to check if a player moves a card and places it right ontop of another card within say a 10 pixel buffer, how would you change the RectsOverlap function above?

So the function would need to not only know if the cards overlap, but if they are placed so close they almost cover each other, so that I would know to move the card somewhat, when the player drops it, so they do not fall directly ontop of each other.


kragoth74(Posted 2010) [#5]
Something like that:

if abs(x2 - x1) < 10 and abs(y2 - y1) < 10 then...



Czar Flavius(Posted 2010) [#6]
What's with the CAPS?


Jesse(Posted 2010) [#7]
what I did on my card game is check the mouse position agains the card area where I want it to go. If a valid test is found, then align the new card with the card found in the test. To select a card check the mouse position card by card from the top card to the bottom. The first test to be true is the card to select and the tests are finished.


Warpy(Posted 2010) [#8]
zawran:
If RectsOverlap(x0+5,y0+5,w0-5,h0-5,x2+5,y2+5,w2-5,h2-5)



YellBellzDotCom(Posted 2010) [#9]
Thanks for the responses! Thats what I needed to know.

"What's with the CAPS?"
It was after a long session of keyboard mashing.


Mahan(Posted 2010) [#10]
This is a Rect base class i use in a current project myself (feel free to use if you find it useful):




edit:
Note the difference (important): Overlap() is when 2 rects actually are on top of eachother i.e. at least 1 pixel overlap, while Collision() is true even when at least 2 pixels of the different rects are right beside each other.


Bremer(Posted 2010) [#11]
Thanks for the responses guys.

Sometimes it difficult to spot the easy solutions even though they are right there in front of you, you know "I cannot see the forest because of all those trees being in the way" kind of thing. :)