在Java中检测与数组的冲突
好的,我想建立一个方法来检测两个物体是否发生碰撞。它们的Hitbox存储在阵列中。对于这两个对象中的每一个,都像这样[topLeftX,topLeftY,bottomRightX,bottomRightY]。我无法找出正确的if语句来使用这两个数组来检测这一点在Java中检测与数组的冲突,java,arrays,collision,Java,Arrays,Collision,好的,我想建立一个方法来检测两个物体是否发生碰撞。它们的Hitbox存储在阵列中。对于这两个对象中的每一个,都像这样[topLeftX,topLeftY,bottomRightX,bottomRightY]。我无法找出正确的if语句来使用这两个数组来检测这一点 public class Physics { public static boolean isColliding(int ob1Hitbox[], int ob2Hitbox[]) { } } 如果发生冲突,则该方法必
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);
}
}
欢迎来到堆栈溢出!看起来你在请求家庭作业帮助。虽然我们对此本身没有问题,但请注意这些,并相应地编辑您的问题。
对象在这里是什么意思?为什么使用静态
?平行宇宙中可能存在不同的物理学;)