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

#### The Gunships

The gunships were modeled in TS4, as shown in Figures 14.12 and 14.13. They were very detailed and looked great until I shrank them down and converted them to the 256-color palette. But that’s life.

Figure 14.12 The 3D model for a gunship.

Figure 14.13 A rendered gunship.

The AI for the gunships is simple. They travel on the x-axis at a constant velocity. If they get within a specified distance to the player, they adjust their y-axis position to track the player, but at a slow rate. Hence, the player can always make a quick directional change to get away. The power of the gunships is in their heavy weapons. Each gunship is equipped with three laser cannons that can be fired independently, as shown in Figure 14.14.

Figure 14.14 The turret-tracking algorithm for the gunships.

The tracking algorithm for the cannons is rather cool. It works by projecting a vector in the direction that the turret is currently pointing, and another vector from the turret to the player’s ship. Then the algorithm wants to minimize the distance from the head of the turret to the player’s ship, so it tests both clockwise and counterclockwise rotations to see which rotation minimizes the distance and then performs the rotation that minimizes the distance.

The algorithm was written without trig or any complex vector calculations, just using a distance calculation and a minimization algorithm. I came up with it by taking into consideration how people’s heads track an object. We start turning in the direction of the object, and when we feel that we’re looking in the right direction, we start slowing our head’s rotation rate and come to a stop. But sometimes we may overshoot and have to readjust. This was the inspiration for the algorithm. Take a look here to see the source for the tracking:

    // first create a vector point in the direction of the turret

                // compute current turret vector

                int tdir1 = gunships[index].varsI[INDEX_GUNSHIP_TURRET];

                float d1x = gunships[index].varsI[INDEX_WORLD_X] +

                            cos_look16[tdir1]*32;

                float d1y = gunships[index].varsI[INDEX_WORLD_Y] +

                            sin_look16[tdir1]*32;

                // compute turret vector plus one

                int tdir2 = gunships[index].varsI[INDEX_GUNSHIP_TURRET]+1;

                if (tdir2 > 15)

                    tdir2 = 0;

                float d2x = gunships[index].varsI[INDEX_WORLD_X] +

                            cos_look16[tdir2]*32;

                float d2y = gunships[index].varsI[INDEX_WORLD_Y] +

                            sin_look16[tdir2]*32;

                // compute turret vector minus one

                int tdir0 = gunships[index].varsI[INDEX_GUNSHIP_TURRET]-1;

                if (tdir0 < 0)

                    tdir0=15;

                float d0x = gunships[index].varsI[INDEX_WORLD_X] +

                            cos_look16[tdir0]*32;

                float d0y = gunships[index].varsI[INDEX_WORLD_Y] +

                            sin_look16[tdir0]*32;

                // now find the min dist

                float dist0 = Fast_Distance_2D(player_x - d0x,

                                               player_y - d0y);

                float dist1 = Fast_Distance_2D(player_x - d1x,

                                               player_y - d1y);

                float dist2 = Fast_Distance_2D(player_x - d2x,

                                               player_y - d2y);

                if (dist0 < dist2 && dist0 < dist1)

                    {

                    // the negative direction is best

                    gunships[index].varsI[INDEX_GUNSHIP_TURRET] = tdir0;

                    } // end if

                else

                if (dist2 < dist0 && dist2 < dist1)

                    {

                    // the positive direction is best

                    gunships[index].varsI[INDEX_GUNSHIP_TURRET] = tdir2;

                    } // end if

> Tip: You’ll notice that I am using a lot of distance calculations. However, they’re based on the `Fast_Distance2D()` function, so they’re very fast and amount to nothing more than a couple of shifts and adds.

### The Power-Ups

The game felt a little “strategy-less” to me with infinite ammo and shields, so I thought, “Why not have some power-ups?” With that in mind, I sat down with TS4 and started modeling power-ups. Then I realized I didn’t have a good idea of what to model!

Power-ups usually aren’t very realistic. I mean, they say “AMMO” on them, float with some kind of anti-gravity drive, and so on, but still they have to look right. In the end, I decided on simple transparent spheres with the words “AMMO” and “SHLD” glowing inside. The 3D model renderings are shown in Figure 14.15.

Figure 14.15 The rendered power-ups.

Once I had the 3D models down, I rendered the power-ups and got ready to put them in the game. I wanted the power-ups to appear as a result of destroying either an asteroid or an enemy, and I decided that it made more sense to put the power-ups inside the asteroids. My argument was that when you destroy an asteroid, debris and precious minerals (such as dilithium crystals) might be thrown out during the explosion. Sounds reasonable. :)

When an asteroid is destroyed, it’s as if a 10-sided die is rolled. If the die lands on the side labeled `TRUE`, a power-up is created (ammo or shields) and it moves away from the explosion site at a low velocity. When you run through the power-up, you absorb the material and either your shields or ammo increase.

At first, I just created the power-up and let it float off. But I soon realized that space is vast, and the second I lost sight of the power-up, it was really hard to find. Sure, I could have put a blip on the scanner, but it made more sense to give the power-up a lifespan. Hence, when a power-up is spawned, it lives for 3-9 seconds and then dies. This way there are an infinite number of power-ups, and if you lose one, it’s sure to get recycled and not just lost in space.

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.