Java:帮助确定两个矩形之间的碰撞

发布于 2024-10-31 10:27:34 字数 1436 浏览 1 评论 0原文

我正在创建一个游戏,pacman,专门作为我课程作业的一部分,但在使用矩形进行碰撞检测时遇到了问题。

我遇到的问题是,即使在屏幕上可以清楚地看到角色没有碰撞,检查交集实际上总是返回 true。下面的输出解释了我的意思:

吃豆人详细信息:x 17.0 y 16.0 墨色细节:x 22.0 y 13.0 调用 intersects() 后发生碰撞:true 吃豆人详细信息:x 18.0 y 16.0 墨色细节:x 23.0 y 13.0 调用 intersects() 后发生碰撞:true

我将矩形设置如下:

public Rectangle pacmanBounds(){
    return new Rectangle(pacRow, pacColumn, 22, 22);
}
public Rectangle ghostBounds(){
    return new Rectangle(ghostRow, ghostColumn, 22, 22);
}

出于测试目的,高度和宽度已被硬编码,但这些是实际的图像大小。

每次重新绘制 JPanel 时,我都会按如下方式检查碰撞:

public boolean checkCollision(){
    Rectangle pacmanBounds = pacman.pacmanBounds();
    //currently commented out for testing
    /*if(pacmanBounds.intersects(inky.ghostBounds()) || pacmanBounds.intersects(blinky.ghostBounds())
            || pacmanBounds.intersects(pinky.ghostBounds()) || pacmanBounds.intersects(clyde.ghostBounds())){
        System.out.println("Oh no!");
        return true;
    }*/

    System.out.println("Pacman details: x " + pacmanBounds.getX() + " y " + pacmanBounds.getY() + " ");
    System.out.println("Inky details: x " + inky.ghostBounds().getX() + " y " + inky.ghostBounds().getY());
    System.out.println("Collision after calling intersects(): " + pacmanBounds.intersects(inky.ghostBounds()));
    return false;
}

此时,我几乎没有关于如何解决此问题的想法,因此,你们可以提供的任何建议将不胜感激!

I'm creating a game, pacman specifically as part of my coursework and I'm having problems when it comes to collision-detection using Rectangles.

The problem I'm experiencing is that even though on screen it is clear to see that the characters aren't colliding checking the intersection is practically always returning true. The output below explains what I mean:

Pacman details: x 17.0 y 16.0
Inky details: x 22.0 y 13.0
Collision after calling intersects(): true
Pacman details: x 18.0 y 16.0
Inky details: x 23.0 y 13.0
Collision after calling intersects(): true

I have the Rectangles set up as follows:

public Rectangle pacmanBounds(){
    return new Rectangle(pacRow, pacColumn, 22, 22);
}
public Rectangle ghostBounds(){
    return new Rectangle(ghostRow, ghostColumn, 22, 22);
}

The height and width have been hardcoded for testing purposes, but these are the actual image size.

I'm checking for collision as follows everytime the JPanel is repainted:

public boolean checkCollision(){
    Rectangle pacmanBounds = pacman.pacmanBounds();
    //currently commented out for testing
    /*if(pacmanBounds.intersects(inky.ghostBounds()) || pacmanBounds.intersects(blinky.ghostBounds())
            || pacmanBounds.intersects(pinky.ghostBounds()) || pacmanBounds.intersects(clyde.ghostBounds())){
        System.out.println("Oh no!");
        return true;
    }*/

    System.out.println("Pacman details: x " + pacmanBounds.getX() + " y " + pacmanBounds.getY() + " ");
    System.out.println("Inky details: x " + inky.ghostBounds().getX() + " y " + inky.ghostBounds().getY());
    System.out.println("Collision after calling intersects(): " + pacmanBounds.intersects(inky.ghostBounds()));
    return false;
}

At this point in time I've pretty much ran out of ideas on how to solve this problem so any advice you guys can give would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东京女 2024-11-07 10:27:34

假设 getX() 和 getY() 返回矩形的左上角坐标点,那么这些将是每次调用的矩形边界:

Pacman 详细信息:x 17.0 y 16.0 Inky 详细信息:x 22.0 y 13.0 调用 intersects() 后发生碰撞:true

Pacman 矩形将是其左上角坐标加上每个方向的 22,为您提供一个带有右下角的矩形 (39.0, 38.0),如果 Inky 的右上角是 (22.0, 13.0) 且左下角是 (44.0, 35.0)<,那么它肯定与 Inky 相交。 /code>

这就是我的样子。您的意思是 22.0 是 Pacman 和 Inky 矩形的边界(以像素为单位还是以正方形为单位)?如果这些假设正确,那么 Inky 的左下角 (22.0, 35.0) 实际上完全位于 Pacman 内部,这可能不是您想要的。问题可能是多种原因造成的,如果不了解更多代码并了解矩形的确切含义,很难说出问题所在。 :D

Assuming getX() and getY() return the point of the upper-left coordinate of the rectangle, then these will be the bounds of the rectangles for each call:

Pacman details: x 17.0 y 16.0 Inky details: x 22.0 y 13.0 Collision after calling intersects(): true

The Pacman rectangle would be its upper-left coordinates plus 22 in each direction, giving you a rectangle with lower-right corner (39.0, 38.0), which most definitely intersects with Inky's if his upper-right corner is (22.0, 13.0) and lower-left corner is (44.0, 35.0)

That's what it looks like to me. Do you mean the 22.0's to be the bounds of the Pacman and Inky rectangles in pixels or in squares? If those assumptions are correct, then Inky's lower-left corner (22.0, 35.0) is literally located completely inside Pacman, which probably isn't what you want. The problem could be a number of things, and its hard to say what it might be without knowing seeing some more of the code and knowing what exactly the rectangles mean. :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文