Search Tips Advanced Search |
| Delphi Graphics and Game Programming Exposed with DirectX 7.0 by John Ayres Wordware Publishing, Inc. ISBN: 1556226373 Pub Date: 12/01/99
Search this book:
Previous | Table of Contents | Next
Thus, most games use a polling technique for retrieving user input. Polling involves directly accessing the device at a specific point within the game loop and reading its data. Theoretically, this polling could take place at any time, but it usually occurs at the beginning of the game loop so that the most recent movement can be reflected in the next frame of animation. Direct polling gives the application developer precise control over the processing of input, and can even allow the detection of multiple input events simultaneously (i.e., check if two keys are pressed at the same time).
| polling: A method for retrieving input or checking some data value manually (as opposed to letting a separate thread check it automatically), usually repeated often at regular intervals.
The primary drawback to the direct polling technique is that user input events can get lost if the polling happens infrequently. This is generally a symptom of a slow game loop, where a large amount of processing takes place before the input device is again polled. Figure 2-3: Missing a user input event One method to remedy this situation is to conduct device polling at several different places throughout the game loop, and then process the sum of the movement data when it is time to update game world objects. However, another more accurate technique is to buffer user input events. To perform this technique efficiently, a separate thread must be utilized to perform the device polling and event queuing. Fortunately, DirectX provides several methods that make this technique viable, and we’ll examine a demonstration of this in the chapter on input techniques. Once the input has been retrieved, it is translated into events that are meaningful in the context of the game. For example, if the joystick button was pressed, the function for starting a missile or a bullet volley would be called. How the input is translated is, of course, dependent on the type of game itself. The result of such user actions will be apparent after the sprite updating segment has been called and the next frame of animation has been drawn to the screen. #### Perform Antagonist AI and Other Game Logic In this segment, algorithms that control the behavior of enemy units or other non-player units are performed, with the results being used to update the appropriate sprite positions or to execute any other appropriate actions. This may include deciding if an enemy spacecraft pursues the player or flees, but it also includes other, perhaps more mundane, decision making processes that affect other parts of the game. For example, background sprites that are never interacted with during the game may contain a little logic for changing their animation so that it does not appear repetitive. This segment may run some very complex code depending on the type of game and its objectives. Some games, such as chess, may have a very advanced thinking engine that examines the current configuration of the board and makes a movement decision based upon the prediction of the next move that the opponent may make. Other games, such as real-time strategy war games, may have several smaller decision engines that determine the most direct route a unit may take to its destination. In general, this segment executes the artificial intelligence code that determines how the game objects will react to the actions of the player.
| artificial intelligence (AI): The concept of cognitive software that thinks and reacts to situations in a sentient manner. In relation to game programming, this is the section of code responsible for determining the actions and reactions of game protagonists in relation to the actions and reactions of the player.
Artificial intelligence is a specialty area in its own right, and some game companies hire programmers specifically for their AI expertise. Some AI techniques are well documented and are easy to implement, such as chasing or evading the player or converging on a specific position. Others, such as natural speech generation or pattern recognition, are very difficult to implement and are either closely guarded trade secrets or the province of advanced academic research. #### Update Sprite Positions The positions of all game objects are updated in this segment. Their velocities are added to their current positions, which are then usually checked to make sure that they are still within the boundaries of the game world. This segment is where the results of user input and artificial intelligence logic are realized. A ship is accelerated or turned according to how the user moved the joystick, and enemy soldiers and cannons are advanced forward or animated per the decisions of the artificial intelligence segment. Since all objects in the game world need to be updated, the game loop typically iterates through each object, one at a time, applying the position translations as required. The game loop itself needs to run as quickly as possible, so other actions that are performed for each game object are typically executed in this segment as well. The most common action performed at the same time as position updating is probably drawing the sprite image into an offscreen buffer. However, other actions may include collision detection, artificial intelligence processing, or other functions. The process of actually drawing the sprite, as well as other sprite specific procedures, will be examined in Chapter 6, Sprite Techniques. #### Check for Collisions The collision detection segment is necessary for determining if two sprites or game objects have collided, and the action that is a result thereof. Most games are driven off of events where two sprites come into contact. For example, when a bullet that was fired several frames ago finally touches the enemy ship at which it was targeted, something should happen. There are hundreds of different methods by which to detect if two or more objects have collided. Some games may require that every object be checked against every other object; others may be interested in the collision between only certain types of objects. Whatever the case, collision detection algorithms vary in complexity according to the task they must perform and the number of objects involved. Accuracy is also an area of great variation. Some may detect only a close collision, and may not be very accurate at all. Others may go as far as comparing the pixels of each bitmap at a certain frame of animation and at a certain position within the game world to see if any pixels actually overlap (a highly accurate method indeed). Speed also becomes a very important issue with the collision detection scheme put into production, as the more accurate the collision detection, typically the slower it will run. Collision detection algorithms will be covered in the chapter on sprite techniques.
Previous | Table of Contents | Next

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 whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.