Skip to content
Advertisement

How to check if two rectangles collide over the axis

I think I’ve got some kind of duplicate question, but I just can’t figure it out.

So I got a Region class. This class contains four attributes. aX, aY and bX, bY. Now I want to produce a method doesCollide(Region other). I’ve tried many things, but can’t get it to work. I think it is, because the regions refer to a region on a minecraft world, where the to points a and b can be negative. Can someone help me out?

For better understanding I did a little drawing of a scenario:

What I’ve tried:

Due to a hint of a user I tried it using the java.awt.Rectangle class:

        Rectangle thisRectangle = new Rectangle((int) aX, (int) aY, Math.abs((int) bX) - Math.abs((int) aX), Math.abs((int) bY) - Math.abs((int) aY));
        Rectangle otherRectangle = new Rectangle((int) other.aX, (int) other.aY, Math.abs((int) other.bX) - Math.abs((int) other.aX), Math.abs((int) other.bY) - Math.abs((int) other.aY));
        return thisRectangle.intersects(otherRectangle);

(I absolutely don’t know if I’ve done it right)

I tried something that I would say is the “standard” thing used here:

return aX < other.bX && bX > other.aX & aY < other.bY && bY > other.aY

Well, didn’t work either.

Advertisement

Answer

Okay, some things to point out. First, if you have double values you can use Rectangle2D.Double. Second, it handles negative numbers fine.

I would suggest creating a method to convert a region to a Rectangle2D.

public Rectangle2D getBounds(Region r){
    double x, w;
    if(r.aX < r.bX ){
      x = r.aX;
      w = r.bX - r.aX;
    } else{
      x = r.bX;
      w = r.aX - r.bX;
    }
    double y, h;
    if(r.aY < r.bY){
      y = r.aY;
      h = r.bY - r.aY;
    } else{
      y = r.bY;
      h = r.aY - r.bY;
    } 
    return new Rectangle2D.Double(x, y, w, h);
}

What this is doing, checking to make sure aX, aY and bX, bY are in the correct order, so that x,y is the top left corner of the rectangle and the width and height are positive (or zero) values. I’ve written it as a method on a region, but you can make it as a method of Region. Such that:

public boolean intersects(Region other){
    return getBounds().intersects(other.getBounds());
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement