Using Force Feedback

Using force feedback in your application adds to the immersion a player experiences while using your application. Driving a car around at over 100 miles per hour, suddenly ramming into a tree, and having the joystick you're using jerk out of your hand from the force is an experience you just can't match. So, how can you add this to your applications?

One of the first things that needs mentioning is that force feedback effects will require you to have exclusive access to the device. Luckily, these joysticks can be acquired with exclusive mode and in the background. You will use a similar method to create the device for this example as you did for the joystick. Replace the existing InitializeInput method with the one found in Listing 15.5.

Listing 15.5 Initializing a Device for Force Feedback
 private ArrayList effectList = new ArrayList(); public bool InitializeInput() {     // Create our joystick device     foreach(DeviceInstance di in Manager.GetDevices(DeviceClass.GameControl,         EnumDevicesFlags.AttachedOnly | EnumDevicesFlags.ForceFeeback))     {         // Pick the first attached joystick we see         device = new Device(di.InstanceGuid);         break;     }     if (device == null) // We couldn't find a joystick         return false;     device.SetDataFormat(DeviceDataFormat.Joystick);     device.SetCooperativeLevel(this, CooperativeLevelFlags.Background |         CooperativeLevelFlags.Exclusive);     device.Properties.AxisModeAbsolute = true;     device.Acquire();     // Enumerate any axes     foreach(DeviceObjectInstance doi in device.Objects)     {         if ((doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0)         {             // We found an axis, set the range to a max of 10,000             device.Properties.SetRange(ParameterHow.ById,                 doi.ObjectId, new InputRange(-5000, 5000));         }     }     // Load our feedback file     EffectList effects = null;     effects = device.GetEffects(@"..\..\idling.ffe",         FileEffectsFlags.ModifyIfNeeded);     foreach(FileEffect fe in effects)     {         EffectObject myEffect = new EffectObject(fe.EffectGuid, fe.EffectStruct,               device);         myEffect.Download();         effectList.Add(myEffect);     }     while(running)     {         UpdateInputState();         Application.DoEvents();     }     return true; } 

There are a few changes that have been made here. You've switched to exclusive access, since it's required for use with force feedback. You've also updated our enumeration to only return force feedback devices. You do the range setting once more, and finally get into the force feedback effects.

You can create the force feedback effect from a file. In actuality, each effect from a file can be an array of separate feedback effects. The code included on the CD (and in this text) will use the idling.ffe file that ships with the DirectX SDK. You will also find a copy with the source on the CD. After you've loaded the effect list from the file, you scan through each individual force feedback effect and create your effect object. You download this effect to the actual device so that it can use it and check its status, and finally add it to the local array list.

Now that you have the set of effects loaded, you will want to actually use them on the device. You should add a method that checks to see whether the effect is currently playing, and if it isn't, to start playing it. This will ensure that as long as our application is running, the effect will be playing:

 private void PlayEffects() {     // See if our effects are playing.     foreach(EffectObject myEffect in effectList)     {         if (!myEffect.EffectStatus.Playing)         {             // If not, play them             myEffect.Start(1, EffectStartFlags.NoDownload);         }     } } 

You should add a call to PlayEffects into our UpdateInputState method as well so that it actually gets called.

USING THE FORCE EDITOR

The Force Editor utility that ships with the DirectX SDK can be used to create any force feedback effect that you want. While it is possible to create these effects manually in your code, why hard code them into your application when you can just save them into a file, and use them from there?



Managed DirectX 9 Graphics and Game Programming, Kick Start
Managed DirectX 9 Kick Start: Graphics and Game Programming
ISBN: B003D7JUW6
EAN: N/A
Year: 2002
Pages: 180
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net