The Asteroid Field
The asteroid field consists of a large number of randomly moving asteroids of three different sizes: small, medium, and large. The asteroids were rendered using TS4 with real-time rendering enabled. Figure 15.5 shows the asteroids in the modeler, and Figure 15.6 shows their rendered forms. Once I had the asteroids looking like asteroids and lit properly, I created a rotation animation and then a Targa (.TGA) movie of each asteroid rotating, converting the .TGA movie files, image0000.tga, image0001.tga, and so on, into bitmaps (.bmp) and importing them directly into the game without templating them.
The physics model of the asteroids is rather simple. They just move in the same direction at a constant velocity until they're fired upon. In addition, the asteroids rotate at varying speeds to give a feel of mass. When an asteroid is fired upon, its size and hardness determine how it's blown up. You can hit a large asteroid so hard with a blaster pulse that it's completely obliterated, but sometimes it only splits. Splitting is dictated by two factors: probability and availability. There are only so many medium and small asteroids, so you can't always split all the large ones.
In addition, I didn't like the way that a large asteroid always split into two medium ones, and a medium asteroid always split into two small ones. Therefore, sometimes a large asteroid might split into one medium and two small ones, or two medium and two small ones, and so on. I threw in some extra permutations based on probability to make things more interesting. If you want to add more asteroids, you can change the following define:
#define MAX_ROCKS 300
Try jacking it up to 1,000, or 10,000 if you have a Pentium III 550 MHz!
I have this love for the game Asteroids. I guess it was that $100 I won in college on a bet. Some other computer science students bet me that I couldn't write an Asteroids game right in front of them in Pascal on an IBM XT. They had seen other games I'd written and said I'd copied them. Of course, these were your typical comp-sci students who couldn't do anything unless there was an API call for it.
I sat down and wrote Asteroids in about eight hours flat—an exact copy of the vector version by Atari (no sound, though). I won the 100 bucks, and then I slapped them all with a backhand and took their pocket protectors.
The point is that I have memorized the code for Asteroids after doing it so many times, and I always like using it as an example. It's like my "Hello World" for game programming. :)
Finally, when an asteroid hits the edge of the game universe, it just wraps around by resetting the x or y position variable. But what if you want to make it bounce off the edge by reflecting the velocity vector's sign?