The method I’m currently thinking using is to check for basic bounding box collisions (using the Rectangle class and the intersects() method). If it detects a collision, I plan on having it check for pixel-perfect collision. Sometimes I render more than a thousand different bullets, so there won’t be a need for pixel-perfect collision detection every update.
The basic idea behind my perfect-pixel collision detection method is to iterate though every pixel in one sprite and check it for every pixel in another sprite. The code then checks the contents to see if both locations contain one opaque pixel. One way to simplify this loop is to return true as soon as a collision is detected. Another method to speed it up would be to only consider the area that the two sprites overlap each other (but I haven’t had time to figure that out yet). Another point is that you also have to factor in the offset between the two sprites. Handle any ArrayIndexOutOfBoundsExceptions and throw them away, that just means that the pixels you’re checking against don’t exist.
Example code using 2 byte[][] arrays. 1 means opaque pixel, 0 means transparent pixel.
public static boolean intersectsByte(byte[][] a1, byte[][] a2, int x, int y) {
for (int i = 0; i < a1.length; i++) {
for (int j = 0; j < a1[0].length; j++) {
if (a1[i][j] == 1) {
for (int k = 0; k < a1.length; k++) {
for (int l = 0; l < a1[0].length; l++) {
try {
if (a2[k - x][l - y] == 1) { return true; }
} catch (Exception ex) { } } } } } }
return false; }
(Ignore the terrible formatting, I’m just trying to keep my post length sane.)