This article may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. To maintain the flow of the article, we've left these URLs in the text, but disabled the links.
|
Push All the Right Buttons with the Force Feedback Pro Elaine Chen and Beth Marcus |
Advanced input devices require advanced programming. When it's a device as cool as the force feedback joystick, the rewards are worth the effort. |
If you've been to a video game arcade recently, you've already seen force feedback in action. Many of the latest games have been enhanced with force feedback: racing games with action seats that tilt, rock, and vibrate as you traverse different terrain and steering that responds to driving conditions; skiing games where your body twists and turns as you glide down a snowy virtual slope; even shoot-em-up games now have active recoil features. Activating seats, handles, and steering wheels adds a whole new dimension to the gaming experience. When properly coordinated with synthesized visuals and sound, control feedback can go a long way toward creating a compelling, interactive virtual experience. Instead of interfacing with a slavish computer, the user is drawn into the simulation and becomes part of the virtual world.
Until recently, force feedback was an esoteric area of academic and military research with applications in virtual reality, space teleoperation, and medical simulation. The mechanical gadgets interacting with the user to provide feedback were dominated by expensive high-performance systems. However, the needs of the entertainment industry are quite different from these high-end applications. What adds realism to a game is not an ability to feel the fine textures on a multifaceted polygonal object, but the ability to feel pulses, vibrations, resistance, and other simple effects. These effects can be provided by lower-end systems with mass-produceable components, and such systems are beginning to appear in the home PC market as force feedback joysticks, steering wheels, and other gaming peripherals. The Microsoft® SideWinder® Force Feedback Pro joystick is one of several force feedback peripherals currently on the market. It has two motorized directions of movement, and can provide force feedback through DirectX® 5.0 calls. The SideWinder Force (SWForce) SDK contains lots of information and many code samples showing you how to create and manipulate various force effects. Instead of presenting a comprehensive tutorial of how to use the SDK, we'll provide a brief overview of important force feedback concepts and walk you through the quickest way to get force effects working on your SideWinder Force Feedback Pro. We will also share some insights on joystick performance, force effect interaction issues, and programming with either DirectX calls or the wrapper functions provided in the SDK.
Force Effects for Games
Time-based Effects
|
Vibrations can have all kinds of different waveforms, from sine waves (very smooth from one end to the other) to triangular waves (similar to sine waves, but with sharper directional changes) to square waves (sharp transitions from one end to another, creating a feedback effect like a machine gun firing) to sawtooth (very jagged). 5 to 10Hz is a good starting point for the waveform frequency. Since people are much better at feeling changes in forces rather than exact magnitudes of a constant force, the same peak force will feel much stronger when applied to a periodic waveform like a sine wave. Therefore using these vibrations really adds a lot of impact to the game.
Space-based and Invariant Effects
Figure 2 shows some representative space-based effects. These effects provide forces that are not a function of time but are updated depending on where the joystick handle is pointing or how it is being moved.
A spring effect is simulated by providing force that restores the joystick's position when its handle departs from a given "spring center." For instance, if the middle of the workspace is the spring center, as the handle is pushed to the right, there will be an increasing force pushing it back to the left. The governing equation is F=Kx, where F is the restoring force, x is the distance of the handle from the spring center, and K is the spring constant. A good physical value for K is 50 ounces per inch in the Cartesian (3D) domain. You can convert your own units when working with joysticks (like the SideWinder Force Feedback Pro) that only allow you to work in percentages, or you can tweak the percentages until it feels right.
A damper (or viscous friction) effect is simulated by providing a resisting force that increases as the speed of handle movement increases. The governing equation is F=Bv, where v is the speed of the joystick and B is the coefficient of viscous friction. If you try to move under water, the resistance you feel is due to this physical phenomenon of viscous friction. A good value for B is something like 2 ounce-seconds per inch.
A friction effect is simulated by providing a constant resisting force exerted by a surface on a moving object, opposing the direction of movement. The governing equation is F=µN, where µ is the coefficient of friction and N is the contact force between the object and the surface. If you try to push a box full of copier paper down a carpeted corridor, that's the effect you feel.
An inertial (gravity or acceleration) effect is simulated by providing a force that goes up as a function of acceleration. This is governed by F=ma, where m is the mass and a is the acceleration. If you are swinging a lasso in a circle, the force that the rope exerts on your hand is due in part to an inertial effect. The feelings of gravity and acceleration are not easily presented by a joystick, since acceleration is really sensed in your inner ear and not as a force on your skin. However, if the joystick presents an inertial effect based on acceleration or deceleration in a racing game, your brain makes the connection and provides an illusion of moving with the car.
A wall effect is very much like the spring effect with a twist: you feel nothing until you impinge on the wall. As you penetrate into the virtual wall, a force pushes you out of the wall along a direction perpendicular to the wall and the force increases as a function of increasing penetration into the wall. This approach models a wall as a very stiff sponge: it gives a little as you push in, but it attempts to push you out nonetheless. This is a standard trick commonly employed in virtual reality applications to simulate the behavior of rigid objects bouncing off each other. The governing equation is F=Kd, where d is the perpendicular penetration of the joystick handle into the virtual wall, and F is the force pushing the stick back out along the perpendicular. K describes the wall constant. On high-end systems you can push K as high as 200 ounce-inches. This is difficult to achieve in home PC joysticks, so to get a crisp hard wall you typically would want to maximize K.
An invariant effect is independent of time and space. It is simply a constant force which has a given magnitude and direction.
Space-based, time-based, and invariant effects form the basic building blocks upon which more complicated force effects can be built. For instance, if one vibration of a given frequency is applied on top of a second vibration with a different frequency, the resulting vibration will feel very random. If a wall and a damper are added together, you get the feeling of tapping a wall under water. Depending on when you turn on different effects, you could even concatenate effects so that they happen one after another. For instance, if your flight simulation ended up in an emergency crash landing, you could have a huge impact effect followed by an uneven vibration as the plane skids to a stopand you could even have a fast and violent vibration as the plane explodes. You could also merge an effect with a temporal envelope. For instance, a sine wave could have a rise time, a starting magnitude, a fade time, and an ending magnitude.
SideWinder Force Feedback Pro
With this basic understanding of force effects,
we can start programming force effects into applications. But first, let's take a look at the physical object that will generate all of these effects.
Figure 3: SideWinder Force Feedback Pro |
Figure 4: Force Feedback System Architecture |
To use the joystick, you need to have the newest version of DirectX 5.0 installed on your computer. Once the system is set up, you can go to the Control Panel, select SideWinder Force Feedback Pro as your joystick device, and test out the functionality of the stick. To program the joystick, you need to install the SWForce SDK as well. (A copy of the SDK can be obtained by request from swinddev@microsoft.com.)
Creating Force Effects
Using Wrapper Functions
|
|
If the operation is successful, you can move on to creating a square wave. You need to supply the address of a pointer to a direct input effect. This is the handle with which you can control your new square wave effect. As you can see, you need to supply a lot of parameters. The square wave can be oriented in any direction; 0 degrees results in a square wave that goes in and out, and 90 degrees results in a square wave that goes left and right. The magnitude is expressed as a percentage, where 10000 equals 100 percent, the peak force that can be exerted by the joystick. The offset shifts the waveform up and down. The envelope governs the rise time, the starting magnitude, the fade time, and the ending magnitude. For simplicity, the following code demonstrates a simple centered square wave that turns on and stays on. It moves in and out at 50 percent maximum force (5000) at a frequency of 10Hz (100000): |
|
Now the effect is created and downloaded to the joystick, where it is stored in RAM and will not execute until you explicitly start it. This is because you may want to define a lot of effects and set them up as part of your game initialization routine. You can then have software handles to all the effects that are already set up in the joystick, and you can start them inside the game without having to wait for the parameters to be downloaded. |
|
You can stop the effect at any time; the effect still resides in memory in the joystick, and can be started again at will. |
|
This effect hogs joystick memory (all 2KB of it) until you explicitly destroy it either by using its handle or by destroying everything that you have downloaded to the stick. After an effect has been destroyed, you must go through the creation and download procedure to start it again. |
|
Since the joystick can only hold a finite number of force effects in memory, you may need to do a little memory management and cache your effects for different sections of the game. In other words, you may download a lot of effects, then destroy them to make room for new effects appropriate for a new page in your game.
Creating Effects With DirectX
|
|
To initialize, you need to create a direct input object, enumerate the force feedback joystick (with your callback), and obtain a pointer to the DirectInputDevice interface. Next, obtain a pointer to the DirectInputDevice2 interface (which contains the force feedback features), get rid of the now superfluous DirectInputDevice interface, and use the DirectInputDevice2 interface to continue the initialization. (This interface stepping is, of course, standard in COM.) It takes a few more lines of code than using the wrapper functions (see Figure 5). Now you need to set up the square wave effect and start it. Note that this effect is identical to the one we created with the wrapper functions (see Figure 6). Once you have created the effect using DirectX, you can try to modify the parameters directly, which is one of the things not supported by the wrapper functions. The code in Figure 7 will change the current 50 percent, 10Hz square wave to 100 percent strength running at 5Hz. To destroy all effects, you need to supply a callback function that will dump them as they are enumerated by the device interface. |
|
After all the downloaded effects are found and destroyed automatically, the joystick is unacquired and the DirectInput object and device interface are released. |
|
Demo Program
Figure 8 shows a program that demonstrates how you might create, keep track of, and manipulate force feedback effects. Both the SWFF wrapper and DirectX approaches are shown. Representative members of each type of force feedback effect (standard spatial, standard temporal, and OEM-specific canned, walled, and file download effects) are included. Most of the effects turn on when the corresponding buttons are pressed, with one exception: we set up the sawtooth demo to fire when the trigger button is depressed. This is a nice feature in DirectX that allows you to tie a given effect with a given button state. The response is much more immediate; it almost feels like a reflex action, and would be appropriate in situations like administering a vibration whenever a machine gun is in action. The code shows how simple it is to create force effects using the SideWinder wrapper functions. It also shows how lengthy the code can get when using the DirectX interface. Again, the choice is up to you. This demo program exercises many of the joystick capabilities and showcases the ability of the joystick to add effects on top of each other. One important observation is the effect of saturation. When many effects are on at the same time, they start to drown each other out. The forces are added according to currently running effects, but there is a peak force dictated by the specifications of the joystick, and the output force is truncated to that force. It is therefore possible to add vibrations to springs with various offsets and end up with something that feels like a constant force in parts of the workspace. The joystick also has a natural bandwidth. We noted that 10Hz is a comfortable frequency for vibrationsthat is approximately the bandwidth for voluntary movement of our hands (people really can't move much faster than that). Higher frequencies can simulate different effects like the vibration in an idling chainsaw, but after a point the force magnitude starts to become seriously attenuated. This is simply because the joystick, being a physical device, has a dynamic response that cannot follow very high-frequency motions. We have found that at 50Hz the handle hums rather than moves around as it would at 10Hz. The joystick would not even perform correctly when commanded to vibrate at 100Hz. Therefore, 50Hz seems to be a good ballpark cut-off point for vibration frequency. The SideWinder Force Feedback Joystick has relatively low friction, and the peak force is adequate for most purposes. It does feel a little weak at a 1 pound (16 ounce) peak when operated in constant force mode, but the vibration and spring effects all feel decent. There is no doubt that it adds a lot of spice to any home PC game.
Conclusion
|
From the June 1998 issue of Microsoft Interactive Developer.