일반적으로 두 개의 회전 된 사각형이 충돌하는지 확인하려면 Separating Axis Theorem을 사용해야합니다. 그러나 불필요한 처리 작업을 수행 할 필요가 없도록 SAT를 사용하기 전에 직사각형이 충돌하는지 여부를 알 수있는 간단한 방법이 있습니다. 경계 원이 교차하지 않는다는 것을 증명함으로써 직사각형이 교차하지 않는다는 것을 증명하는 경계 원 검사를 할 수 있습니다.
사각형의 경계 원은 중심을 공유하며 직사각형의 대각선 길이와 동일한 지름을가집니다. 본질적으로 원이 교차하지 않으면 직사각형이 교차 할 수 없습니다.
회전을 수행하는 방법을 잘 모르겠지만 도형/영역 유형 개체를 사용하는 경우 AffineTransform
을 사용하여 회전을 수행 한 다음 각 회전 된 개체의 영역에 intersects()
을 사용하여 충돌이 발생하면 직접 구현하지 않아도됩니다. 다음 예제를 고려하십시오.
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
public class Main {
public static void main(String args[]) {
//Create rectangle
Rectangle rect = new Rectangle(10, 10, 10, 10);
//Create transformation object
AffineTransform af = new AffineTransform();
//Rotate the rectangle by 45 degrees
af.rotate(Math.PI/4, rect.x, rect.y);
Rectangle rect2 = new Rectangle(20, 20, 20, 20);
AffineTransform bf = new AffineTransform();
bf.rotate(Math.PI/4, rect2.x, rect2.y);
//Create Area objects based off of the Rectangle objects
Area areaA = new Area(rect);
//Set the Area object to be the same as the Rectangle object
areaA = areaA.createTransformedArea(af);
Area areaB = new Area(rect2);
areaB = areaB.createTransformedArea(bf);
//Check if the objects collide by using their Area equivalent
if (areaA.intersects(areaB.getBounds())) {
System.out.println("Collision!");
}
}
}
분명히 이것을 구현에 맞게 수정할 수 있습니다. 올바른 방향으로 사용자를 유도하기를 바랍니다.
이 답변을 주셔서 대단히 감사합니다. 그러나 unfortunatels 패키지 중 하나를 가져올 수 없습니다. (항상 "가져 오기 java.awt.geom을 확인할 수 없습니다"라는 말은 모든 가져 오기에 해당합니다.) android? – user1059863
http://google.com/codesearch/p?hl=ko#uX1GffpyOZk/awt/java/awt/geom/AffineTransform.java는 awt 패키지가 Android (afaik)에서 사용할 수 있음을 보여줍니다. Rectangle를 Rectangle2D로 바꿔야 만합니다. – Deco
java.awt 중에 나에게 맞는 유일한 패키지는 java.awt.font입니다 ... 2.1 용으로 개발 중이며 지원되지 않았기 때문일 수 있습니다. – user1059863