Search code examples
collision-detection2d-games

Space invaders base deterioration


In space invaders, when an alien bullet hits your defense bases, they deteriorate slowly. Can anybody explain how games do that?

I guess they are not using images for the bases and also not bounding rectangle collision detection?


Solution

  • Dialing in the wayback machine. The original space invaders used a simple bit mask to add damage to the bases. The images were just basic 1 bit images, from memory 1 byte wide and 8 bytes down (8 by 8 pixels)

    To apply the damage there is a mask [0b11111001,0b11110000] that is shifted left (with carry placed in the lowest bit) then masked with the base bytes. pixelByte &= maskByte. Then cut out the pattern of damage. Bombs would continue hitting the base if any pixels were remaining and in the path of the bomb.

    To detect a base hit a mask was also used. bombMask = 0b00000011 shifted left (or could have been looked up) to match x pos. To see if there is a hit you just AND the mask with the base bits pixelByte & bombMask if the result is not zero then the base has been hit.

    It was all written in assembly, and for its time was a programing master piece. I still remember thinking, "How can they move so many pixels so quickly."

    Today you can use the same method, but you seldom see that much effort put into detail like that. I find that modern games tend to take short cuts and just have a set lot of images to show damage.

    So just take the damage count, based on a hit box, or circle, show image to illustrate damage state, healthy, minor damage, major damage, and destroyed. Easy to implement! good for the game? well that is another matter altogether.