Java 图片像素碰撞检测
在 2D 游戏或图形应用中,检测两个对象是否“碰撞”是常见需求。大多数情况下,开发者会用矩形边界(bounding box)来判断是否接触,但这会带来误判——两个透明区域的图片可能会被判定为相交。
今天分享的 ImageIconCrack
类,就通过像素级碰撞检测来解决这个问题。
功能介绍
ImageIconCrack.detection()
方法接收两张图片(File a
、File b
)及它们在画布上的位置 (a_X, a_Y)
和 (b_X, b_Y)
,返回一个布尔值:
true
→ 图片上非透明像素实际重叠false
→ 没有像素重叠
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;public class ImageIconCrack {public static boolean detection(File a, File b, int a_X, int a_Y, int b_X, int b_Y) throws IOException {BufferedImage a_BufferImage = ImageIO.read(a);BufferedImage b_BufferImage = ImageIO.read(b);int a_Width = a_BufferImage.getWidth();int a_Height = a_BufferImage.getHeight();int b_Width = b_BufferImage.getWidth();int b_Height = b_BufferImage.getHeight();int a_X_right = a_X + a_Width;int a_Y_downside = a_Y + a_Height;int b_X_right = b_X + b_Width;int b_Y_downside = b_Y + b_Height;if (!findIfMeets(a_X, a_Y, b_X, b_Y, a_X_right, a_Y_downside, b_X_right, b_Y_downside)) {return false;}int[] x1_y1_x2_y2 = findMiddles(a_X, a_Y, b_X, b_Y, a_X_right, a_Y_downside, b_X_right, b_Y_downside);int a_ShouldSearch_x1 = Math.max(0, x1_y1_x2_y2[0] - a_X);int a_ShouldSearch_y1 = Math.max(0, x1_y1_x2_y2[1] - a_Y);int a_ShouldSearch_x2 = Math.min(a_Width - 1, x1_y1_x2_y2[2] - a_X);int a_ShouldSearch_y2 = Math.min(a_Height - 1, x1_y1_x2_y2[3] - a_Y);int b_ShouldSearch_x1 = Math.max(0, x1_y1_x2_y2[0] - b_X);int b_ShouldSearch_y1 = Math.max(0, x1_y1_x2_y2[1] - b_Y);int b_ShouldSearch_x2 = Math.min(b_Width - 1, x1_y1_x2_y2[2] - b_X);int b_ShouldSearch_y2 = Math.min(b_Height - 1, x1_y1_x2_y2[3] - b_Y);if (a_ShouldSearch_x1 > a_ShouldSearch_x2 || a_ShouldSearch_y1 > a_ShouldSearch_y2 ||b_ShouldSearch_x1 > b_ShouldSearch_x2 || b_ShouldSearch_y1 > b_ShouldSearch_y2) {return false;}int searchTimesX = a_ShouldSearch_x2 - a_ShouldSearch_x1 + 1;int searchTimesY = a_ShouldSearch_y2 - a_ShouldSearch_y1 + 1;for (int x = 0; x < searchTimesX; x++) {for (int y = 0; y < searchTimesY; y++) {int a_x = a_ShouldSearch_x1 + x;int a_y = a_ShouldSearch_y1 + y;int b_x = b_ShouldSearch_x1 + x;int b_y = b_ShouldSearch_y1 + y;if (a_x >= a_Width || a_y >= a_Height || b_x >= b_Width || b_y >= b_Height) {continue;}int pixel_a = a_BufferImage.getRGB(a_x, a_y);int pixel_b = b_BufferImage.getRGB(b_x, b_y);if (!isAnEmptyPixel(pixel_a) && !isAnEmptyPixel(pixel_b)) {return true;}}}return false;}private static boolean isAnEmptyPixel(int pixel) {int alpha = (pixel >> 24) & 0xFF;return alpha == 0;}private static boolean findIfMeets(int a_X, int a_Y, int b_X, int b_Y, int a_X_right, int a_Y_downside, int b_X_right, int b_Y_downside) {return a_X < b_X_right && a_X_right > b_X && a_Y < b_Y_downside && a_Y_downside > b_Y;}private static int[] findMiddles(int a_X, int a_Y, int b_X, int b_Y, int a_X_right, int a_Y_downside, int b_X_right, int b_Y_downside) {int[] x_four = {a_X, b_X, a_X_right, b_X_right};int[] y_four = {a_Y, b_Y, a_Y_downside, b_Y_downside};fourNumbers(x_four);fourNumbers(y_four);return new int[]{x_four[1], y_four[1], x_four[2], y_four[2]};}private static void fourNumbers(int[] numbers) {for (int i = 0; i < 3; i++) {for (int j = 0; j < 3 - i; j++) {if (numbers[j] > numbers[j + 1]) {int temp = numbers[j];numbers[j] = numbers[j + 1];numbers[j + 1] = temp;}}}}}