1

我还没有找到任何文章或教程,可以吗?它试图调用一个 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();
            }
        }
    }
}
4

2 回答 2

1

您不能使用从DeviceObjectInstancefor获得的偏移量SetAxes。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并传入ObjectTypefromDeviceObjectInstance来识别轴。

据我所知,如果您知道哪些轴有执行器,您还可以使用JoystickObjects类的静态成员之一作为 的偏移量。SetAxes

于 2012-09-09T13:22:18.677 回答
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
于 2013-03-04T14:35:40.957 回答