Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Checking overlap area of rectangles?

Does anyone know how to check if two rectangles are overlapping?

Like, let's say i have two instances of Rect. the first is @bob and the
second is @joe.

Now if @joe is touching @bob, it prints "Quit touching me, joe!"

How would i do that?
 
It all depends. If you want to test if they are touching:

Code:
class Rect

  def touching?(rect)

    # Gets 4 points of secondary rect

    rect_x1 = rect.x

    rect_x2 = rect.x + rect.width

    rect_y1 = rect.y

    rect_y2 = rect.y + rect.height

 

    for i in rect_x1..rect_x2

      if i.between?(@x, @x + @width)

        for j in rect_y1..rect_y2

          if j.between?(@y, @y + @height)

            return true

          end

        end

      end

    end

 

    return false

  end

end

Haven't tested it, but I think that should work.

It barfs on "@x +", attr_accessor doesn't seem to make difference...?? - Brew
 
Simple, just look at it the other way...

for 2 rectangles, r1 & r2

If the left side of r2 is greater then the right side of r1 or
If the right side of r2 is less than the left side of r1 or
if the top or r2 is greater than the bottom of r1 or
if the bottom of r2 is less than the top or r1

then the rectangles do not intersect, so to return 'true' if they do intersect, return the opposite '!'

Code:
    def intersect(r1, r2)

      return ! (r2.x > (r1.x + r1.width) or \

               (r2.x + r2.width) < r1.x  or \

                r2.y > (r1.y + r1.height) or \

               (r2.y + r2.height) < r1.y)

    end

 
 
Thanks. And can I check what direction rectangle1
overlaps with rectangle 2 at? This is important for
a game i am making, and i am making every scene
my self, which means I have to do the movement
and collision from scratch.
 
Neither of these will indicate the direction as they are written. But if you understand the logic, you should be able to modify either to suit your needs. Give it a try. Choose whichever algorithm you like, and we'll help you with the mod if necessary.

Be Well
 
okay, i'm officially stumped...

I know that I need to check that for it to
be touching from the left or right the y has
has to be relatively close. But what I don't
get is how I check it. @x > x1 is the same as
@x < x2 basically, so if @x < x2 would return
true, so would @x > x1 and vice versa. Can
you show me how I would prevent that?
 
No, those are not the same. (although I applaud you for trying it yourself.) :scruff:

If @x = 5, x1 = 10, x2 = 15, then
@x > x1 => false
@x < x2 => true

You defined the problem as 2 arbitrary rectangles, existing in any position on a plane.
Both scripts provided will tell you if the 2 rectangles intersect. (or touch)
But by the sounds of your 2nd post, the rectangles will never intersect, only touch (or not touch).
Let's make a couple more assumptions...
You are using pixel movement
The rectangles can be any size
You are using 8 directional movement.

Seph's algorithm compares every point (integer / pixel) on rect1 to the boundaries of rect2. If any point falls within those boundaries, the rectangles intersect.

My algorithm determines if rect2 is either Above, Below, Left, or Right of rect1, then they do not intersect. I think this algorithm lends itself to determining direction a bit easier.

Since this is "Script Support" where we learn to script, I'll just give you the algorithm, and see if you can code it...

8 directional movement is indicated by the numbers on the number pad...

7 8 9
4 5 6
1 2 3

So, if rect2 is above, the direction is '8', below is '2', Left is '4', Right is '6'
Both Above & Left would be '7'.....

If we initialize 'direction' to 5 (in the middle) then we can work out from there...

Code:
 

@direction = 5   #initialize direction

# set the direction of approach before they touch

If <the bottom of rect2> is less than <the top of rect1>

  @direction = 8   #above

end

If <the top of rect2> is greater than <the bottom of rect1>

  @direction = 2    #below

end

If <the left side of rect2> is greater than <the right side of rect1>

  @direction += 1   #1 right of either above, center, or bottom

end

If <the right side of rect2> is less than <the left side of rect1>

  @direction -= 1    # 1 left of either above, center, or bottom

end

# If the direction == 5, return true. Otherwise return false

return @direction == 5

 

If you check direction after a 'true' response, it should be set to the proper approach direction.

Be Well
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top