java java中两个矩形之间的碰撞检测

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31022269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:57:15  来源:igfitidea点击:

Collision detection between two rectangles in java

javaalgorithmcollision-detection

提问by mikelplhts

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

我有两个矩形,红色矩形(可以移动)和蓝色矩形。两者都有:x,y,宽度,高度。

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

当蓝色和红色矩形之间发生碰撞时,如何在Java等编程语言中说?

Example of collision

碰撞示例

回答by BufBills

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:

假设你有矩形 A 和矩形 B。证明是矛盾的。四个条件中的任何一个都保证不存在重叠:

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:

Not Cond1 And Not Cond2 And Not Cond3 And Not Cond4 这相当于:

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 >=.

注 1:很明显,同样的原则可以扩展到任意数量的维度。注意 2:计算一个像素的重叠也应该是相当明显的,将该边界上的 < 和/或 > 更改为 <= 或 >=。

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

如果您很难想象它的工作原理,我在silentmatt.com/intersection.html制作了一个示例页面,您可以在其中拖动矩形并查看比较。

回答by Pruthvi Raj

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

在java中,要检测两个矩形碰撞时是否两个,可以使用intersects()方法

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
}

回答by Denis Petrov

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;
}

回答by Miljen Mikic

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.

您必须检查沿 x 轴和沿 y 轴的交点。如果缺少其中任何一个,则矩形之间没有碰撞。

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:

你必须为 x 和 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);
}

回答by BufBills

  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.

  1. 计算第一个矩形的 4 个点。将它们记为 pointA、pointB、pointC、pointD。
  2. 计算第二个矩形的 4 个点。将它们记为 pointE、F、G H。

  3. 如果其中任何一个在 A、B、C、D 包围的区域内,则迭代点 E、F、G、H (1)。返回碰撞。(2)否则无碰撞。

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

对于 3.(1) 算法。你需要这样的东西。

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

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