Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 509
mikelplhts Avatar asked Jun 24 '15 09:06

mikelplhts


People also ask

Which method is used to collision detection between two rectangles?

One of the simpler forms of collision detection is between two rectangles that are axis aligned — meaning no rotation. The algorithm works by ensuring there is no gap between any of the 4 sides of the rectangles. Any gap means a collision does not exist.

How do you find the intersection of two rectangles in Java?

Rectangle rect1 = new Rectangle(100, 100, 200, 240); Rectangle rect2 = new Rectangle(120, 80, 80, 120); Rectangle intersection = rect1. intersection(rect2); To use java. awt.

How do you perform collision detection in Java?

Since Java doesn't have an intersect function (really!?) you can do collision detection by simply comparying the X and Y, Width and Height values of the bounding boxes (rectangle) for each of the objects that could potentially collide.

How do you know if two rectangles overlap?

Two rectangles do not overlap if one of the following conditions is true. 1) One rectangle is above top edge of other rectangle. 2) One rectangle is on left side of left edge of other rectangle. We need to check above cases to find out if given rectangles overlap or not.


4 Answers

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.

like image 135
BufBills Avatar answered Nov 15 '22 07:11

BufBills


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
}
like image 25
Pruthvi Raj Avatar answered Nov 15 '22 07:11

Pruthvi Raj


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;
}
like image 30
Denis Petrov Avatar answered Nov 15 '22 07:11

Denis Petrov


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);
}
like image 41
Miljen Mikic Avatar answered Nov 15 '22 07:11

Miljen Mikic