使用SlimDX强制反馈?

时间:2012-08-28 17:54:01

标签: slimdx

我还没有找到任何文章或教程,是否可能? 它试图调用一个Effect类,但我得到的只是

E_INVALIDARG:将无效参数传递给返回函数(-2147024809)

代码

[TestClass]
public class ForceFeedbackTest
{
    [TestMethod]
    public void Test()
    {
        using (var form = new Form())
        {
            var directInput = new DirectInput();

            foreach (var device in directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                var controller = new Joystick(directInput, device.InstanceGuid);
                controller.SetCooperativeLevel(form.Handle, CooperativeLevel.Exclusive | CooperativeLevel.Background);
                controller.Acquire();

                int xAxisOffset = 0, yAxisOffset = 0;
                int nextOffset = 0;
                foreach (DeviceObjectInstance d in controller.GetObjects())
                {
                    if ((d.ObjectType & ObjectDeviceType.ForceFeedbackActuator)
                        != 0)
                    {
                        if (nextOffset == 0)
                            xAxisOffset = d.Offset;
                        else
                            yAxisOffset = d.Offset;
                        nextOffset++;
                    }
                }

                int[] offsets = new int[2];
                offsets[0] = xAxisOffset;
                offsets[1] = yAxisOffset;
                int[] coords = { 0, 0 };

                var info = controller.GetEffects(EffectType.All).First();

                var args = new EffectParameters();
                args.Flags = EffectFlags.ObjectOffsets | EffectFlags.Cartesian;
                var typeSpec = new ConstantForce();


                args.Duration = 1000;
                args.SamplePeriod = controller.Capabilities.ForceFeedbackSamplePeriod;
                args.Parameters = typeSpec;


                args.Gain = 5000;

                args.SetAxes(offsets, coords);

                args.StartDelay = 500;

                var effect = new Effect(controller, info.Guid);
                effect.SetParameters(args); //This is the line i get the error on.
                effect.Start();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您无法使用DeviceObjectInstanceSetAxes获得的偏移量。 MSDN提到了这一点,但它有点粗略:

  

设备本机数据格式的偏移量。本机数据格式对应于原始设备数据。 dwOfs成员与此对象的设备常量(例如DIJOFS_BUTTON0)不对应。

http://msdn.microsoft.com/en-us/library/windows/desktop/microsoft.directx_sdk.reference.dideviceobjectinstance(v=vs.85).aspx

您可以使用EffectFlags.ObjectIds代替EffectFlags.ObjectOffsets并传递ObjectType中的DeviceObjectInstance来识别轴。

据我记忆,如果你知道哪些轴有执行器,你也可以使用JoystickObjects类的一个静态成员作为SetAxes的偏移量。

答案 1 :(得分:0)

如果您使用的是Xbox 360控制器或任何支持XInput的控制器,我会使用它而不是DirectInput。使用IMO更容易,并且强制反馈是微不足道的。看看SlimDX.XInput。过去几年中大多数较新的游戏手柄都支持XInput。

VB.net代码:

Dim player As Integer = 1 'Player can be 1-4
Dim gamePad As SlimDX.XInput.Controller = New SlimDX.XInput.Controller(player)

Public Sub VibrateController(ByVal rSpeed As Short, ByVal lSpeed As Short)
   Dim vibSettings As New SlimDX.XInput.Vibration()
   vibSettings.LeftMotorSpeed = lSpeed
   vibSettings.RightMotorSpeed = rSpeed
   gamePad.SetVibration(vibSettings)
End Sub