Collision detection between two rectangles in java

前端 未结 5 2069
粉色の甜心
粉色の甜心 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:45

    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
    }
    

提交回复
热议问题