Managed DirectX is a great way to write DirectX applications in .NET languages, and most of the Managed DX libraries are well documented, robust, and have numerous examples floating around the Internet. I've been coding Managed DX off and on since it first arrived in 2002, but never messed with force feedback until recently.
Imagine my surprise to find that all the public examples for force feedback code in C# error out with my game pad (Logitech 2 Rumble). Many other people have posted on various Internet forums about this issue with Managed DirectX and other game pads, so this appears to be a very long running bug in the Managed DirectInput libraries. Though there are a number of people reporting this issue, I was not able to find a solution that worked for me anywhere -- so here's my own solution.
The attached project provides examples of how to create and trigger several different forces, so if you're new to force feedback in C#, this has everything you need to get started all in one place.
If you'd like to see Microsoft's standard example (which doesn't work for me), visit this link.
The solution I eventually found was unique, but triggered in part by a blog post by Ayucar.
Using the Code
The attached project has the entire code, but here are the (somewhat simplified) highlights in case you are already familiar with the examples but just keep getting that "Value does not fall within expected range" error.
Basic Device Initialization
Device Dev = null;
foreach ( DeviceInstance instance in Manager.GetDevices( DeviceClass.GameControl,
EnumDevicesFlags.AttachedOnly ) )
Dev = new Device( instance.InstanceGuid );
Dev.SetDataFormat( DeviceDataFormat.Joystick );
Dev.SetCooperativeLevel( Parent, CooperativeLevelFlags.Background |
Dev.Properties.AxisModeAbsolute = true;
Dev.Properties.AutoCenter = false;
The above is pretty typical of DirectInput examples. The
AutoCenter = falseline is particularly needed for force feedback. The
Parent variable in the
SetDataFormat line must be a
Finding the Axes
int axis = null;
foreach ( DeviceObjectInstance doi in Dev.Objects )
if ( ( doi.ObjectId & (int)DeviceObjectTypeFlags.Axis ) != 0 )
doi.ObjectId, new InputRange( -5000, 5000 ) );
if ( ( doi.Flags & (int)ObjectInstanceFlags.Actuator ) != 0 )
if ( axis != null )
temp = new int[axis.Length + 1];
axis.CopyTo( temp, 0 );
axis = temp;
axis = new int;
axis[axis.Length - 1] = doi.Offset;
if ( axis.Length == 2 )
Everything until the
inttemp is declared is your standard DirectInput axes initialization -- you have to do those things regardless of whether or not you are using force feedback. The axis array calculation is force-feedback-specific, and is all taken directly from the Microsoft examples (I didn't add anything new there).
Creating a Force
InitializeForce( Dev, EffectType.ConstantForce, axis,
10000, EffectFlags.ObjectOffsets | EffectFlags.Spherical, 250000 ) );
public static EffectObject InitializeForce( Device Dev, EffectType Type,
int Axis, int Magnitude, EffectFlags Flags, int Duration )
EffectObject eo = null;
foreach ( EffectInformation ei in Dev.GetEffects( EffectType.All ) )
if ( DInputHelper.GetTypeCode( ei.EffectType ) == (int)Type )
e = new Effect();
e.SetDirection( new int[Axis.Length] );
e.SetAxes( new int );
e.EffectType = Type;
e.ConditionStruct = new Condition[Axis.Length];
e.Duration = Duration;
e.Gain = 10000;
e.Constant = new ConstantForce();
e.Constant.Magnitude = Magnitude;
e.SamplePeriod = 0;
e.TriggerButton = (int)Microsoft.DirectX.DirectInput.Button.NoTrigger;
e.TriggerRepeatInterval = (int)DI.Infinite;
e.Flags = Flags;
e.UsesEnvelope = false;
eo = new EffectObject( ei.EffectGuid, e, Dev );
Here's where my solution comes in. The line that normally errors out is
e.SetAxes(new int[Axis.Length]); It usually gives an exception "Value does not fall within expected range", which is evidently a bug in Managed DirectX (at least affecting some game pads). Changing that line so that it always initializes an axis array of length 1 instead of length 2 (as the Microsoft example would do) fixes this issue seemingly without any other side effects to performance or function.
I should note that I have not been able to successfully load FFE files in Managed DirectX due to this same problem. Presumably, in the inner initialization code when loading from FFE, it is trying to load two axes there, as well. This problem exists with all versions of Managed DirectX, as far as I can tell, at least up through March 2008 when this article was written.
Points of Interest
Force feedback is underutilized in most PC games, possibly because the technology was not available in low end game pads until relatively recently. Adding force feedback capabilities to your games is a great way to provide a more immersive experience, and a minor way to stand out in the PC games market.
- Version 1: March 16, 2008