2012-08-28 106 views
0

我還沒有找到任何文章或教程,這可能嗎? 它試圖調用效果類,但我得到的是用SlimDX強制反饋?

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(); 
      } 
     } 
    } 
} 

回答

0

不能使用您從DeviceObjectInstance獲得的偏移量爲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.ObjectOffsetsDeviceObjectInstanceObjectType傳遞,識別軸。

據我所知,如果您知道哪個軸有執行器,您也可以使用JoystickObjects類的靜態成員之一作爲SetAxes的偏移量。

0

如果您使用Xbox 360控制器或任何支持XInput的控制器,我會使用該控件而不是DirectInput。海事組織使用起來更容易,並且後援力量很大。檢查出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