Tricks of the Windows Game Programming Gurus:Putting It All Together: You God Game!

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

Lastly, the Wraith has a shield and a vapor trail. The shield is nothing more than a bitmap that I overlay on the Wraith when something hits it, and the vapor trail is just particles that I emit out randomly when the thrusters are on. The thruster effect is achieved with two bitmaps for each direction of the Wraith: one with thrusters on and one with thrusters off. When the thrusters are on, I randomly select the on/off image and it looks like a Klingon impulse drive.

### 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. Figures 14.5 shows the asteroids in the modeler, and Figure 14.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.

Figure 14.5 The 3D models for the asteroids.

Figure 14.6 The rendered asteroids.

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!

> Tip: 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?

### The Enemies

The enemies in the game aren’t the smartest guys in the universe, but they get the job done. For the most part they use the more introductory AI methods, such as deterministic logic and FSMs (finite state machines). However, there are a couple of cool techniques that I used for tracking algorithms. You’ll see this later in the chapter when I show you the code that makes the predator mines home in on the player’s position. Anyway, take a look at how each enemy was created and implemented.

#### The Outposts

The model I used for the outposts was probably the most complex 3D model in the whole game. It took me hours to build the model. The bummer is that the 3D model has a ton of detail, as shown in Figures 14.7 and 14.8, but I lost all that detail when I rendered the model and shrank it down.

Figure 14.7 The 3D model for an outpost.

Figure 14.8 A rendered outpost.

Anyway, the outposts don’t do much except sit there and rotate. They have no weapons, no AI, nothing. However, they can detect damage, and when the player fires on them they will start to explode. Particles and secondary explosions will occur until the damage level of the outpost is so great that it explodes!

#### The Predator Mines

The predator mines are the protectors of the outposts. They hold position nearby until you get within a specified range, and then they turn on and track you. The predator mines were rendered with TS4 and are shown in Figures 14.9 and 14.10.

Figure 14.9 The 3D model for a predator mine.

Figure 14.10 A rendered predator mine.

I wasn’t very happy with the final 3D model for them. Actually, I created another 3D model, shown in Figure 14.11, but it looked more like a stationary mine than something that could attack you.

Figure 14.11 Another predator mine concept.

In any case, the AI for the predator mine is simple. It’s a finite state machine that starts off in an idle or sleep state. It’s activated when you get within a specified range, based on the following `define`:

    #define MIN_MINE_ACTIVATION_DIST 250

If the player is within this range of a predator mine, it will activate and track using the vector-tracking algorithm that I described and demonstrated when I covered artificial intelligence and tracking algorithms in Chapter 12, “Making Silicon Think with Artificial Intelligence.”

The predator mines don’t have any weapons; they simply try to track you and detonate near you, causing damage to your ship.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.