Java 2D 비디오 게임을하고 있으며 아래에서 보는 것보다 빠른 충돌 감지 알고리즘을 찾고 있습니다. 어뢰와 함선 사이의 충돌을 감지하려고합니다. 사용하고있는 알고리즘은 내 메인 그래픽스 2D 디스패치 루프의 성능에 심각한 영향을 미치고 화면 리 페인트 속도를 늦 춥니 다. 누군가가 이것을 처리하는 방법에 대한 더 나은 알고리즘을 추천 할 수 있는지 궁금 해서요, 충돌을 감지하는 것이 더 빠릅니까? 샘플 코드에 대한 포인터는 훌륭합니다!빠른 Java 이미지 충돌 감지 알고리즘
여기 .. 내가 그 픽셀에 의해 픽셀 간다 사용하고 느린 알고리즘
private boolean isPixelCollide(double x1, double y1, VolatileImage image1,
double x2, double y2, VolatileImage image2) {
double width1 = x1 + image1.getWidth() -1,
height1 = y1 + image1.getHeight() -1,
width2 = x2 + image2.getWidth() -1,
height2 = y2 + image2.getHeight() -1;
int xstart = (int) Math.max(x1, x2),
ystart = (int) Math.max(y1, y2),
xend = (int) Math.min(width1, width2),
yend = (int) Math.min(height1, height2);
// intersection rect
int toty = Math.abs(yend - ystart);
int totx = Math.abs(xend - xstart);
for (int y=1;y < toty-1;y++){
int ny = Math.abs(ystart - (int) y1) + y;
int ny1 = Math.abs(ystart - (int) y2) + y;
for (int x=1;x < totx-1;x++) {
int nx = Math.abs(xstart - (int) x1) + x;
int nx1 = Math.abs(xstart - (int) x2) + x;
try {
if (((image1.getSnapshot().getRGB(nx,ny) & 0xFF000000) != 0x00) &&
((image2.getSnapshot().getRGB(nx1,ny1) & 0xFF000000) != 0x00)) {
// collide!!
return true;
}
} catch (Exception e) {
// System.out.println("s1 = "+nx+","+ny+" - s2 = "+nx1+","+ny1);
}
}
}
return false;
} getRGB를 많이입니다
테스트 된 모든 픽셀에 대해서 * image1.getSnapshot()을 호출 할 것입니다! 적어도 이것을 루프 밖으로 빼내십시오. – Marco13