Collision detection between two rectangles in java

前端 未结 5 2058
粉色の甜心
粉色の甜心 2020-12-10 17:59

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

5条回答
  •  粉色の甜心
    2020-12-10 18:27

    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

提交回复
热议问题