좋아, 두 물체가 충돌하는지 감지하는 방법을 만들고 싶습니다. 히트 박스는 배열에 저장됩니다. 두 개체 각각에 대해 [topLeftX, topLeftY, bottomRightX, bottomRightY]처럼. 나는 이것을 감지하기 위해 두 배열을 사용하는 올바른 if 문을 알아낼 수 없습니다.
public class Physics {
public static boolean isColliding(int ob1Hitbox[], int ob2Hitbox[]) {
}
}
충돌이 발생한 경우 메서드는 true를 반환해야합니다.
Rectangle#intersects
계산을 수행하기 위해 사용할 수 있습니다.
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);
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다