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
#### Game Controller Input Game controller input programming can be both fun and complex. The fun comes from the fact that game controllers can be of almost any shape and size, and tend to resemble non-computer input devices such as steering wheels, rudder pedals, flight yokes, etc. This incredible variety is also the cause of the complexity. Since game controllers can have almost any number of axes, buttons, sliders, etc., the developer never knows the exact configuration of an attached controller without deducing what is available programmatically. This leads to a level of complexity not found in similar code for interfacing with keyboards and mice. Game controllers can return a wide range of information. Data for up to three axes can be returned, including their rotation, velocity, acceleration, and force (with the existence of force feedback). Additionally, the state of two sliders, four POV hats, and 128 buttons can also be retrieved. By default, all of the axis information returned from game controllers is absolute, as most axes can move only so far in either direction. However, these values are arbitrary and not based on screen coordinates, etc. Like the mouse, axis data corresponds to the physical movement of the device. However, since game controllers can be almost any shape, the sign of these values and their relation to the direction of physical movement on the device is dependent upon the device itself (such as rotation of an axis). Also unlike the mouse, game controllers do take into account control panel settings, such as calibration. Therefore, you do not have to worry about writing specific code for calibrating a game controller before retrieving its state.
| Tip: To run the control panel application for game controllers to begin calibration (or any control panel associated with any DirectInputDevice, for that matter), simply call IDirectInputDevice’s RunControlPanel method, passing zero in its two parameters. This allows users to easily calibrate game controllers from within your game.
Game controller input programming has a number of additional nuances that one must be aware of. As stated earlier, the predefined data format structure for joysticks and game controllers does not seem to work appropriately. Thus, in the examples below, we will enumerate all objects on an input device (such as axes, buttons, etc.) and create a custom data format. This will provide support for a wider range of game controllers (theoretically, all game controllers) and will result in more robust and stable code. Additional Game Controller Considerations Above and beyond what is required to set up buffered data retrieval, game controllers require that the developer set up additional properties for each axis before input data can be retrieved. Specifically, the range of values for each axis must be set, as well as their deadzone and saturation levels. Range A minimum and maximum range must be set for each axis. These values are completely arbitrary; DirectInput will scale the data returned from the driver to fit within this range, but these values are meaningful only within the context in which they are used. The center position of the axis will be the center of this specified range. Deadzone The deadzone indicates an area around the center of an axis in which the axis can move, yet the position will be reported as the lowest value within the range. This allows a joystick to wander around a little in the center without reporting movement. This can be useful for setting sensitivity levels. This deadzone range is not subtracted from the overall range; DirectInput compresses the range values into a smaller part of the physical range of the axis. The deadzone value is always between 0 and 10,000, indicating a percentage of the overall range of axis travel in hundredths of degrees. For example, a value of 2,000 indicates we wish the first 20 percent of the physical range of axis travel to fall within the deadzone. This range is based on the center position of the axis, with one-half of the range on either side. Saturation Saturation is similar to the deadzone in that it defines an area at the extent of axis travel in which the axis can move but the position will be reported as the highest value within the range. Again, this can be useful for setting sensitivity levels. However, some joysticks may only report the highest range values when the stick is moved to a diagonal position. Like the deadzone, saturation values are always between 0 and 10,000 (hundredths of degrees), and indicate the percentage of physical axis travel at which the highest range values will be reported. For example, a value of 9,000 indicates that the saturation zone begins at 90 percent of the physical range of axis travel (the last 10 percent of axis travel will fall within the saturation zone). Again, this range is based on the center position of the axis, and the range values themselves are compressed into the remaining part of the physical axis range.
| Caution: It is possible to set a saturation and a deadzone that completely obscure discrete axis measurements (i.e., saturation and deadzone can be set such that axis values will only be reported as the minimum or maximum value in the specified range).
| Note: Axes on a game pad do not report discrete values; they only report values at the central and extreme ends of the specified range.
Game Controller Objects Game controllers come with a variety of objects with which the user can provide input data. These objects include axes, POVs, and buttons. Although most game controllers exhibit input objects that are not available on the keyboard and mouse, data retrieved from these objects is not unlike that retrieved from more conventional input devices. Lateral and Rotational Axes Game controller axes come in two flavors: lateral and rotational. A lateral axis moves in a straight line, such as left and right or forward and backward. Conversely, a rotational axis moves in a circular fashion, rotating about the x-, y-, or z-axis. The twisting motion on some joysticks is an example of a rotational axis. The values returned from each axis type is similar, however, and will be within the range specified by the developer. POV Hats A point-of-view hat is neither a button nor an axis (it has more than two positions but reports values in a fixed range). The data reported by a POV is in polar coordinates, with 0 to the north (or top) and subsequent values increasing in a clockwise direction. These values are reported in hundredths of degrees (i.e., 9000 = 90 degrees). When the POV is centered, the low word of the reported value will be $FFFF. Most POVs have a granularity of 45 or 90 degrees.
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.