May 21, 2019, 10:48:12 PM

Author Topic: Can anybody provide a RectsOverlap function for AGK?  (Read 229 times)

Offline Amon.

  • Jr. Member
  • **
  • Posts: 97
  • What? There's no ceiling outside?
    • Amon.Pro
Can anybody provide a RectsOverlap function for AGK?
« on: April 01, 2019, 08:49:27 AM »
Can anybody provide a RectsOverlap function for AGK?
Windows 10 Pro - 32GB DDR4 RAM - GEFORCE RTX 2070 8GB - AMD RYZEN 7 8 CORE - WATERCOOLING.

Online Derron

  • Hero Member
  • *****
  • Posts: 2083
Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #1 on: April 01, 2019, 08:54:43 AM »
Isn't this the same basic algorithm in most languages?

point in rect:
return ( otherX >= x And otherX < x+w And otherY >= y And otherY < y+h )

Now just do that for "top left" and "bottom right" of an rectangle - if one of both returns true then they intersect/overlap.


bye
Ron

Offline Amon.

  • Jr. Member
  • **
  • Posts: 97
  • What? There's no ceiling outside?
    • Amon.Pro
Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #2 on: April 01, 2019, 09:00:24 AM »
Yes it is the same principle in nearly all programming languages but I need an AGK version. I notice this forum has blitz code archives. I'm going to hunt down a blitz one and see if I can convert it.
Thanks though.
Windows 10 Pro - 32GB DDR4 RAM - GEFORCE RTX 2070 8GB - AMD RYZEN 7 8 CORE - WATERCOOLING.

Online Derron

  • Hero Member
  • *****
  • Posts: 2083
Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #3 on: April 01, 2019, 09:09:57 AM »
What do you want to look for in the archives? All you need is already written in my previous post: a line of code to check if a point (x,y) is in a rectangle and the instruction to do that for the top left and the bottom right point of a rectangle.
Of course you could use other approaches like "abs(x2-x1) < width" but they call a function "abs" which does some stuff too + a potential function call overhead.

Seems you want it to be chewed already :-)

bye
Ron

Offline Amon.

  • Jr. Member
  • **
  • Posts: 97
  • What? There's no ceiling outside?
    • Amon.Pro
Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #4 on: April 01, 2019, 09:14:10 AM »
How does this look, Derron?

Code: [Select]
function CheckCollide( x1 as float, y1 as float, x2 as float, y2 as float, width as float, height as float )
    local r as integer
    If abs( x1 - x2 ) < width and abs( y1 - y2 ) < height
        r = 1
        return
    else
        r = 0
        return
    endif
endfunction r
Windows 10 Pro - 32GB DDR4 RAM - GEFORCE RTX 2070 8GB - AMD RYZEN 7 8 CORE - WATERCOOLING.

Online Derron

  • Hero Member
  • *****
  • Posts: 2083
Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #5 on: April 01, 2019, 09:22:58 AM »
It only checks if a point is within a rectangle.

You need to do that twice (as said) for top left (x,y) and bottom right (x+w, y+h) of your first rectangle.


bye
Ron

Offline therevills

  • Hero Member
  • *****
  • Posts: 537
Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #6 on: April 01, 2019, 09:33:42 AM »
I've just quickly knocked this up:

RectOverlap
Code: [Select]
function RectsOverlap(x1, y1, w1, h1, x2, y2, w2, h2)
value = true
If x1 >= (x2 + w2) Or (x1 + w1) <= x2 Then value = false
If y1 >= (y2 + h2) Or (y1 + h1) <= y2 Then value = false
endfunction value

And PointInRect
Code: [Select]
function PointInRect(x, y, w, h, otherX, otherY)
endfunction ( otherX >= x And otherX < x+w And otherY >= y And otherY < y+h )

Example:

Code: [Select]
// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle("Test")
SetWindowSize(1024, 768, 0)
SetWindowAllowResize(1) // allow the user to resize the window

// set display properties
SetVirtualResolution(1024, 768) // doesn't have to match the window
SetOrientationAllowed(1, 1, 1, 1) // allow both portrait and landscape on mobile devices
SetSyncRate(30, 0) // 30fps instead of 60 to save battery
SetScissor(0, 0, 0, 0) // use the maximum available screen space, no black borders
UseNewDefaultFonts(1)

global false = 0
global true = 1

red = MakeColor( 255, 0, 0 )
green = MakeColor( 0, 255, 0 )
blue = MakeColor( 0, 0, 255 )

box_x = Random(0, 1000)
box_y = Random(0, 700)
box_w = Random(10, 100)
box_h = Random(10, 100)

mouse_w = 50
mouse_h = 50

do
Print(ScreenFPS())

mx = GetRawMouseX()
my = GetRawMouseY()

Print(mx)
Print(my)

if (RectsOverlap(mx, my, mouse_w, mouse_h, box_x, box_y, box_w, box_h))
color = red
else
color = green
endif

DrawBox(mx, my, mx + mouse_w, my + mouse_h, blue, blue, blue, blue, true)

DrawBox(box_x, box_y, box_x + box_w, box_y + box_h, color, color, color, color, true)
Sync()
loop

function RectsOverlap(x1, y1, w1, h1, x2, y2, w2, h2)
value = true
If x1 >= (x2 + w2) Or (x1 + w1) <= x2 Then value = false
If y1 >= (y2 + h2) Or (y1 + h1) <= y2 Then value = false
endfunction value

function PointInRect(x, y, w, h, otherX, otherY)
endfunction ( otherX >= x And otherX < x+w And otherY >= y And otherY < y+h )

Offline MikeHart

  • Hero Member
  • *****
  • Posts: 571
  • Cerberus-X Dev Team
    • Cerberus X
Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #7 on: April 01, 2019, 10:32:50 AM »
Amon, is the buildin command GetSpriteCollision not suitable for you? you can even define collision shapes if my memory doesn't mislead me here.

Offline Amon.

  • Jr. Member
  • **
  • Posts: 97
  • What? There's no ceiling outside?
    • Amon.Pro
Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #8 on: April 01, 2019, 11:05:26 AM »
Thanks, therevills. You are da dude. :)

@MikeHart
Like you said, I need to RTFM. :) 
Windows 10 Pro - 32GB DDR4 RAM - GEFORCE RTX 2070 8GB - AMD RYZEN 7 8 CORE - WATERCOOLING.

Offline MikeHart

  • Hero Member
  • *****
  • Posts: 571
  • Cerberus-X Dev Team
    • Cerberus X
Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #9 on: April 01, 2019, 12:33:51 PM »
Thanks, therevills. You are da dude. :)

@MikeHart
Like you said, I need to RTFM. :)


Btw. that command will also help when the sprites are rotated.

Online Steve Elliott

  • Hero Member
  • *****
  • Posts: 1771
Windows 10, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
MacOS Mojave, 64-bit, 8Gb RAM, CPU Intel i5, 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB.
Linux Mint 19.1, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).

Offline DaiHard

  • Jr. Member
  • **
  • Posts: 29
Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #11 on: April 02, 2019, 09:14:50 AM »
Just out of interest, why won't Ron's method fail if a tall thin rectangle crosses a wide thin one (think of a fence post in the middle of a fence railing? All corners of both rectangles lie outside the other?

If TR and BL of the test rectangle both lie to the left/right/top bottom of the object, you can be confident, but otherwise you need to look more closely?

Best wishes,

D

Online Derron

  • Hero Member
  • *****
  • Posts: 2083
Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #12 on: April 02, 2019, 09:34:29 AM »
It does a "line check". Will try to draw something during lunch break - maybe it is explanable visually.

I might have mixed up "contains" with "intersects". My algorithm was for "rect a contains rect b".
So I stand corrected: algorithm in my first reply is NOT the one you need for "RectsOverlap".


My (BlitzMax) code for "intersects" is:
Code: BlitzMax
  1.         'returns if the rect overlaps with the given one
  2.         Method Intersects:int(rect:TRectangle) {_exposeToLua}
  3.                 'checking if topleft or bottomright of the rect
  4.                 'is contained in our rect via "containsXY" also returns true
  5.                 'for rects next to each other:
  6.                 'rectA = 0,0 - 10,10
  7.                 'rectB = 10,10 - 20,10
  8.                 '-> rectA contains point "10,10"
  9.                 'return ( containsXY( rect.GetX(), rect.GetY() ) ..
  10.                 '         OR containsXY( rect.GetX() + rect.GetW(),  rect.GetY() + rect.GetH() ) ..
  11.                 '       )
  12.  
  13.                 'to avoid this, we use "exclusive" ranges (> instead of >=)
  14.                 Return ( GetX() < rect.GetX2() AND GetY() < rect.GetY2() ) AND ..
  15.                        ( GetX2() > rect.GetX() AND GetY2() > rect.GetY() )
  16.         End Method
  17.  

bye
Ron