java - 检查矩形是否相交仅在第一次检查时相交时才起作用

标签 java android game-engine intersection rectangles

public void update() {
    speedY = bg.getSpeedY() / 2;
    tileY += speedY;
    r.set(tileX, tileY, 50, 48);
    // Collision with cycle here:
    if (tileY > 480) {
        r = null;
    }
    if (tileY < 480) {
        checkCollision();
    }
}

private void checkCollision() {
    if (type != 0) {
        boolean val = Rect.intersects(Cycle.rect, r);   
        if (val) {
            test = true;
        }
    }
}

使用调试器,我确定所有矩形都正确更新,并且 Cycle.rect 返回正确的矩形位置。如果矩形在第一次检查时相交,则效果很好。但是,当更新运行时,相交永远不会返回 true。为什么会发生这种情况?我使用 boolean 值只是为了帮助调试。

我记录了两个值,我认为矩形应该相交,但事实并非如此:

r: 02-24 23:32:50.762: V/矩形(16624): 矩形(50, 314 - 50, 48) Cycle.rect: 02-24 23:32:50.762: V/矩形(16624): 矩形(49, 329 - 100, 100)

最佳答案

问题在于 Java 中的坐标系,其中左上角被视为 0。因此,如果 top 坐标为 ,则 Rect 被“定义” >小于底部。否则,它被认为是空的。

来自Rect API reference ,

Note: most methods do not check to see that the coordinates are sorted correctly (i.e. left <= right and top <= bottom).

也适用于set()(注意:强调我的),

Set the rectangle's coordinates to the specified values. Note: no range checking is performed, so it is up to the caller to ensure that left <= right and top <= bottom.

isEmpty()

Returns true if the rectangle is empty (left >= right or top >= bottom)

关于java - 检查矩形是否相交仅在第一次检查时相交时才起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22004260/

相关文章:

android,在 NanoHTTPD 项目中强制关闭

android - 如何播放 DCIM 文件夹中的 mp4 文件?

java - 游戏循环 - 时间计算

ruby - 是否可以在 RPG Maker XP 中更新 Ruby 版本

java - 使用 Scanner 对用户输入进行 junit 测试

Java 泛型类型参数

android - 在用户之间发送图像并在 Android 中将其保存在服务器中一段时间​​的最佳方式是什么?

C++控制台游戏输入&&刷新函数

java - JList 自定义选择行为 - 包括从单击的项目到列表末尾的所有内容

java - 如何重新启动 GUI 应用程序游戏,例如再次玩