mikelplhts
mikelplhts

Reputation: 1211

Collision detection between two rectangles in java

I have two rectangles, the red rectangle (can move) and the blue rectangle. Both have: x, y, width, height.

How can I say in a programming language such as Java when there is a collision between the blue and the red rectangle?

Example of collision

Upvotes: 10

Views: 30397

Answers (5)

Denis Petrov
Denis Petrov

Reputation: 871

bool isIntersect(
  int Ax, int Ay, int Aw, int Ah,
  int Bx, int By, int Bw, int Bh)
{
  return
    Bx + Bw > Ax &&
    By + Bh > Ay &&
    Ax + Aw > Bx &&
    Ay + Ah > By;
}

Upvotes: 4

BufBills
BufBills

Reputation: 8103

if (RectA.X1 < RectB.X2 && RectA.X2 > RectB.X1 &&
    RectA.Y1 < RectB.Y2 && RectA.Y2 > RectB.Y1) 

Say you have Rect A, and Rect B. Proof is by contradiction. Any one of four conditions guarantees that no overlap can exist:

Cond1. If A's left edge is to the right of the B's right edge, - then A is Totally to right Of B
Cond2. If A's right edge is to the left of the B's left edge, - then A is Totally to left Of B
Cond3. If A's top edge is below B's bottom edge, - then A is Totally below B
Cond4. If A's bottom edge is above B's top edge, - then A is Totally above B
So condition for Non-Overlap is

Cond1 Or Cond2 Or Cond3 Or Cond4

Therefore, a sufficient condition for Overlap is the opposite (De Morgan)

Not Cond1 And Not Cond2 And Not Cond3 And Not Cond4 This is equivalent to:

A's Left Edge to left of B's right edge, and
A's right edge to right of B's left edge, and
A's top above B's bottom, and
A's bottom below B's Top

Note 1: It is fairly obvious this same principle can be extended to any number of dimensions. Note 2: It should also be fairly obvious to count overlaps of just one pixel, change the < and/or the > on that boundary to a <= or a >=.

If you are having a hard time visualizing why it works, I made an example page at silentmatt.com/intersection.html where you can drag rectangles around and see the comparisons.

Upvotes: 26

Pruthvi Raj
Pruthvi Raj

Reputation: 3036

In java , to detect if two when two rectangles collide, you can use intersects() method

Sample Code:

Rectangle r1 = new Rectangle(x1,y1,x2,y2);
Rectangle r2 = new Rectangle(x1,y1,x2,y2);
if(r1.intersects(r2))
{
    //what to happen when collision occurs goes here
}

Upvotes: 7

BufBills
BufBills

Reputation: 8103

  1. Calculate 4 points of first rectangle. Note them as pointA, pointB, pointC, pointD.
  2. calculate 4 points of second rectangle. Note them as pointE, F, G H.

  3. Iterate point E,F,G,H (1) if any of these is within area that A,B,C,D enclosed. return collision. (2) otherwise no collision.

For 3.(1) algorithm. you need something like this.

Xe >= Xa && Xc <= Xb. && Yc >= Yc && Ye <=Yc

Upvotes: -1

Miljen Mikic
Miljen Mikic

Reputation: 15241

You have to check both the intersection along the x-axis and along the y-axis. If any of them is missing, there is no collision between rectangles.

Code for 1-D:

boolean overlaps(double point1, double length1, double point2, double length2)
{
    double highestStartPoint = Math.max(point1, point2);
    double lowestEndPoint = Math.min(point1 + length1, point2 + length2);

    return highestStartPoint < lowestEndPoint;
}

You have to call it for both x and y:

boolean collision(double x1, double x2, double y1, double y2, double width1, double width2, double height1, double height2)
{
    return overlaps(x1, width1, x2, width2) && overlaps (y1, height1, y2, height2);
}

Upvotes: 1

Related Questions