I'm trying to utilize force feedback with a Fanatec steering wheel in my unity project. I'm using SharpDX.DirectInput to try and solve the problem. Here's my code that's trying to utilize force feedback, I've just been calling this method in the script's start function for testing purposes.
private void ForceFeedback()
{
// set up direct input
DirectInput di = new DirectInput();
Device wheel = null;
// find wheel
foreach (DeviceInstance instance in di.GetDevices(
DeviceClass.GameControl,
DeviceEnumerationFlags.AttachedOnly | DeviceEnumerationFlags.ForceFeedback))
{
wheel = new Joystick(di, instance.InstanceGuid);
}
// set cooperative level
wheel.SetCooperativeLevel(GetWindowHandle(),
CooperativeLevel.Exclusive | CooperativeLevel.Background);
// set axis mode to absolute
wheel.Properties.AxisMode = DeviceAxisMode.Absolute;
// set buffer size
wheel.Properties.BufferSize = 128;
// acquire wheel for capturing
wheel.Acquire();
//Configure axes
int[] axis = null;
foreach (DeviceObjectInstance doi in wheel.GetObjects())
{
// set axes ranges
if (((int)doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0)
{
wheel.Properties.Range =
new InputRange(-5000, 5000);
}
int[] temp;
// get info about first two ff axes on device
if (doi.ObjectType.CompareTo(ToGuid((int)DeviceObjectTypeFlags.ForceFeedbackActuator)) != 0)
{
if (axis != null)
{
temp = new int[axis.Length + 1];
axis.CopyTo(temp, 0);
axis = temp;
}
else
{
axis = new int[1];
}
// store the offset of each axis.
axis[axis.Length - 1] = doi.Offset;
if (axis.Length == 2)
{
break;
}
}
}
// see if wheel supports ConstantForce and set it
Effect e;
foreach (EffectInfo ei in wheel.GetEffects(EffectType.All))
{
if ((ei.Type & EffectType.ConstantForce) != 0)
{
// fill in some generic values for the effect
EffectParameters param = new EffectParameters();
param.Directions = new int[axis.Length];
param.Axes = new int[axis.Length];
// -> param.conditionStruct ?
// -> param.effectType ?
param.Duration = int.MaxValue;
param.Gain = 10000;
param.SamplePeriod = 0;
param.Flags = EffectFlags.ObjectOffsets | EffectFlags.Cartesian;
param.Axes = axis;
e = new Effect(wheel, wheel.Information.ForceFeedbackDriverGuid, param);
e.Start();
}
}
}
/// <summary>
/// Convert int to guid. To be used in forcefeedback function
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static Guid ToGuid(int value)
{
byte[] bytes = new byte[16];
BitConverter.GetBytes(value).CopyTo(bytes, 0);
return new Guid(bytes);
}
The GetWindowHandle()
funtion is defined at the top of the class:
// function using native dll to get active window handle (for sharpdx cooperation levels)
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern System.IntPtr GetActiveWindow();
public static System.IntPtr GetWindowHandle()
{
return GetActiveWindow();
}
Whenever I run the code I receive the following error: SharpDXException: HRESULT: [0x80040154], Module: [SharpDX.DirectInput], ApiCode: [DIERR_DEVICENOTREG/DeviceNotRegistered], Message: Class not registered
Anyone have any idea what could be causing this and what I could do to fix it? I've done quite a bit of research online to try and find a solution but haven't had any luck so far.
User contributions licensed under CC BY-SA 3.0