you don’t want the saucer to jerk and
you don’t want the saucer to jerk and immediately start moving away from the asteroid. Instead, you gradually alter its velocity so that it appears to steer away from the asteroid. This is a subtle difference, but the effect is dramatic because it looks as if the saucer is truly steering through the asteroid field. The first step in the UpdateSaucer() function is to obtain the position of the flying saucer (line 5). You can then loop through the asteroids and find out the minimum X and Y collision distance (lines 8 and 9), which is the closest distance between an asteroid and the saucer. Inside the loop, the asteroid position is first obtained (line 12), which is critical for determining the collision distance. The minimum XY collision distance is then calculated (lines 15 22) and used as the basis for determining if this asteroid is currently the closest one to the saucer. This is where the function gets a little tricky because you must add the X and Y components of the collision distance to see which asteroid is closer to the saucer (lines 23 30). This technique isn’t flawless, but it helps to eliminate “false alarm” situations in which an asteroid is close to the saucer horizontally but far away vertically. When the asteroid loop is exited, you have two pieces of important information to work with: the X collision distance and the Y collision distance. It’s now possible to check and see if these distances are below a certain minimum distance that is required in order for the saucer to be in danger of colliding with an asteroid. My own trial-and-error testing led to a value of 60, but you might decide on a slightly different value in your own testing. In order to steer the saucer to safety horizontally, the X collision distance is checked to see if it is below the minimum distance of 60; in which case, the saucer’s velocity is adjusted (lines 36 43). The same process is then repeated for the saucer’s Y collision distance (lines 44 51). Finally, the new velocity of the saucer is set by calling the SetVelocity() method on the saucer sprite (line 54). Testing the Finished Product The premise behind the Roids 2 program is to show how a certain degree of AI can be injected into a sprite object so that it can avoid other sprite objects. In this context, the evading sprite object is a flying saucer that is desperately trying to keep from colliding with asteroids in an asteroid field. Although you have to actually run the program yourself to see the flying saucer evade the asteroids, Figure 20.3 shows the saucer in the process of steering away from a close call. Figure 20.3. The flying saucer in the Roids 2 program does its best to dodge the asteroids that are floating around the game screen. This is one of those rare program examples that is actually fun to just sit back and watch because the program appears to have a mind of its own. In other words, you’ve created the logic for the flying saucer, and now you get to see how it responds in different situations. This is the part of the process of developing AI in games that is often frustrating because objects can do strange things that are quite unexpected at times. Even so, the flying saucer does a pretty good job of avoiding asteroids in the Roids 2 program. Summary If you find artificial intelligence to be a fascinating area of computer research, you hopefully enjoyed this hour. You learned about the three fundamental types of game AI (roaming, behavioral, and strategic), along with how they are used in typical gaming scenarios. As a game programmer with at least a passing interest in AI, your AI knowledge will likely grow a great deal as you encounter situations in which you can apply AI techniques. After you get comfortable with implementing the basics, you can move on to more advanced AI solutions based upon prior experience and research on the Web. I hope this hour at least provided you with a roadmap to begin your journey into the world of the computer mind. The next hour applies your newfound AI knowledge to the most ambitious game in the book. The game is called Space Out, and it’s a space shoot-em-up with a quirky cast of characters.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services