I am currently working on making a game in which the player's ship is destructible. Collisions between bullets and the ship are per-pixel. When bullets collide with the ship, they take a circular segment out of the ship's alpha mask, like so:
The ship's health is reduced by the number of pixels removed in the blast divided by the total number of pixels in the ship, to get the percentage. The problems arise when a section of the ship is isolated from the rest and appears to be floating with the rest of the ship, like this:
What I'd like to do to handle this is to first detect each section of the ship and make a PImage or PGraphics object to contain each segment. Then I want to find which one is the largest based on the number of pixels within it and leave that part in the ship's image mask. I'd then like to mask out the smaller, separated part(s) from the ship image and create an object with that part as its image. This would make it so that parts that are disconnected would break off of the ship. The step I'm having problems with is detecting if and how many sections the ship has been separated into. I've so far tried putting a random seed on the ship's alpha mask and then extending the seed in each direction until it hits a transparent area, and continuing until convergence, and then copying that section into another buffer and masking that section out of the original image ad nauseum or until no segments are left. It hasn't worked well, though. Does anybody know of a way to isolate different sections of an image into separate buffers?