Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中检测与数组的冲突_Java_Arrays_Collision - Fatal编程技术网

在Java中检测与数组的冲突

在Java中检测与数组的冲突,java,arrays,collision,Java,Arrays,Collision,好的,我想建立一个方法来检测两个物体是否发生碰撞。它们的Hitbox存储在阵列中。对于这两个对象中的每一个,都像这样[topLeftX,topLeftY,bottomRightX,bottomRightY]。我无法找出正确的if语句来使用这两个数组来检测这一点 public class Physics { public static boolean isColliding(int ob1Hitbox[], int ob2Hitbox[]) { } } 如果发生冲突,则该方法必

好的,我想建立一个方法来检测两个物体是否发生碰撞。它们的Hitbox存储在阵列中。对于这两个对象中的每一个,都像这样[topLeftX,topLeftY,bottomRightX,bottomRightY]。我无法找出正确的if语句来使用这两个数组来检测这一点

public class Physics {
    public static boolean isColliding(int ob1Hitbox[], int ob2Hitbox[]) {

    }
}
如果发生冲突,则该方法必须返回true。

您可以使用以下命令来完成计算:

import java.awt.Rectangle;

public class Physics {
    public static boolean isColliding(int[] ob1Hitbox, int[] ob2Hitbox) {
        return toRectangle(ob1Hitbox).intersects(toRectangle(ob2Hitbox));
    }

    private static Rectangle toRectangle(int[] hitbox) {
        int x = hitbox[0];
        int y = hitbox[1];
        int width = hitbox[2] - x;
        int height = y - hitbox[3];
        return new Rectangle(x, y, width, height);
    }
}

欢迎来到堆栈溢出!看起来你在请求家庭作业帮助。虽然我们对此本身没有问题,但请注意这些,并相应地编辑您的问题。
对象在这里是什么意思?为什么使用
静态
?平行宇宙中可能存在不同的物理学;)