Here in part five, we'll dive headlong into collisions. A collision occurs when a part of one image overlaps with part of another. In games, this can be used to detect whether an object has been hit by another object. In theory this is easy - it's a matter of just detecting for the overlapping images. The problems occur, however, when you actually try to implement this.
Images usually have irregular shapes, but are housed in rectangles.
Detecting for overlapping rectangles is too crude for most games, and using small rectangles that are inside the required image is also too unsophisticated. If we're not careful, this could result in the creation of an unwieldy collision detection routine. Fortunately, Java provides a very handy solution through the PixelGrabber class. This can be used to read the pixels of an image, and create an array of the color values of each pixel. We can use this to create pixel-perfect collision detection routines, as we shall now see.