Java 矩形。交叉点工作不正常

Java 矩形。交叉点工作不正常,java,rectangles,intersect,Java,Rectangles,Intersect,我正在做一个游戏,要求我知道两个物体在哪里碰撞。我决定尝试使用矩形,因为我最近了解了Rectangle.intersects()方法。所以我制作了一个2乘2的小矩形网格,像这样映射到游戏中的两个玩家身上 检查两个相交的矩形是在一个不同类的计时器上进行的,这些矩形是在类中为玩家制作的 创建矩形 Rectangle playerHitBoxTL = new Rectangle(0, 0, 60 , 60); Rectangle playerHitBoxTR = new Rectan

我正在做一个游戏,要求我知道两个物体在哪里碰撞。我决定尝试使用矩形,因为我最近了解了Rectangle.intersects()方法。所以我制作了一个2乘2的小矩形网格,像这样映射到游戏中的两个玩家身上

检查两个相交的矩形是在一个不同类的计时器上进行的,这些矩形是在类中为玩家制作的

创建矩形

    Rectangle playerHitBoxTL = new Rectangle(0, 0, 60 , 60);
    Rectangle playerHitBoxTR = new Rectangle(60, 0, 59, 60);
    Rectangle playerHitBoxBL = new Rectangle(0, 60, 60, 59);
    Rectangle playerHitBoxBR = new Rectangle(60, 60, 59, 59);
将它们添加到另一个类中并检查它们是否冲突

    @Override
    public void actionPerformed(ActionEvent arg0) {
        Rectangle playerTL = thePlayer.playerHitBoxTL;
        Rectangle playerTR = thePlayer.playerHitBoxTR;
        Rectangle playerBL = thePlayer.playerHitBoxBL;
        Rectangle playerBR = thePlayer.playerHitBoxBR;
        Rectangle player2TL = testPlayer.playerHitBoxTL;
        Rectangle player2TR = testPlayer.playerHitBoxTR;
        Rectangle player2BL = testPlayer.playerHitBoxBL;
        Rectangle player2BR = testPlayer.playerHitBoxBR;
        if (playerBR.intersects(player2TL)){
           JOptionPane.showMessageDialog(null,"");
        }
    }
很明显,如果播放器1的右下矩形与播放器2的左上矩形相交,则显示JOptionPane的最终目标

计时器已在容纳玩家的班级中启动。希望你们有什么建议

编辑: 有人建议我也将代码放在运动上

    @Override
    public void keyPressed(KeyEvent arg0) {
        int code = arg0.getKeyCode(); //gets the int value of the key that was pressed
        if (code == KeyEvent.VK_W) { //If the "w" key is pressed, move everything up
            thePlayer.setLocation(thePlayer.getLocation().x, thePlayer.getLocation().y - 5);
            bullet1.setLocation(bullet1.getLocation().x, bullet1.getLocation().y - 5);        
        }
        if (code == KeyEvent.VK_A) { //if the "a" key is pressed, move everything down
            thePlayer.setLocation(thePlayer.getLocation().x - 5, thePlayer.getLocation().y);
            bullet1.setLocation(bullet1.getLocation().x - 5, bullet1.getLocation().y);
        }
        if (code == KeyEvent.VK_S) { //if the "s" key is pressed move everything to the left
            thePlayer.setLocation(thePlayer.getLocation().x, thePlayer.getLocation().y + 5);
            bullet1.setLocation(bullet1.getLocation().x, bullet1.getLocation().y + 5);
        }
        if (code == KeyEvent.VK_D) { //if the "d" key is pressed move everything to the right
            thePlayer.setLocation(thePlayer.getLocation().x + 5, thePlayer.getLocation().y);
            bullet1.setLocation(bullet1.getLocation().x + 5, bullet1.getLocation().y);
        }
}

编辑2:找出问题所在,最初矩形被映射到容纳角色的JPanel上,而不是容纳整个游戏的框架上。在frame类中创建新的矩形,为角色设置适当的位置和尺寸,并让它们与关键侦听器一起移动,intersects方法开始按照我希望的方式工作,之后

两个
矩形的相交面积是多少?(单点(60,60))。javadoc for
Rectangle
intersects
方法说“两个矩形相交,如果它们的交集不是空的。”显然,他们认为这意味着“有一个非零区域”而不是“包含任何点”。此外,为什么每个玩家都有一个(我猜)左下/右上
矩形?他们不应该只有一个从左下到右上的
矩形吗?@user2478398你的第一个问题让我有点困惑。前两个坐标表示播放器中每个矩形的左上角,因此它们根据放置位置略有变化。矩形的宽度和高度只是播放器宽度和高度的一半。另外,我的最终目标是让玩家在四个方向中的一个方向上弹回,这取决于与哪个矩形相交,也就是四个矩形。我理解这一点。您的代码没有“BR与TL相交”(有效)。将矩形表示为(左下)->(右上)。您有BR=(60,60)->(119,119)。TL=(0,0)->(60,60)。这两个矩形仅在一个点(60,60)相交。因此,它们相交的面积为0(单个点没有面积)。因此,
矩形
类确定它们不相交。也就是说,它们“接触”,它们不相交。哦,没关系,你没有共享移动(或提及)的代码,所以我假设它们没有从你的建筑中移动过来。。。人们需要更多的信息来提供帮助,因为
Rectangle.intersects
肯定有用。。。