MotionDevice.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using Ryujinx.Common.Configuration.Hid;
  2. using Ryujinx.Configuration;
  3. using System;
  4. using System.Numerics;
  5. namespace Ryujinx.Motion
  6. {
  7. public class MotionDevice
  8. {
  9. public Vector3 Gyroscope { get; private set; }
  10. public Vector3 Accelerometer { get; private set; }
  11. public Vector3 Rotation { get; private set; }
  12. public float[] Orientation { get; private set; }
  13. private Client _motionSource;
  14. public MotionDevice(Client motionSource)
  15. {
  16. _motionSource = motionSource;
  17. }
  18. public void RegisterController(PlayerIndex player)
  19. {
  20. InputConfig config = ConfigurationState.Instance.Hid.InputConfig.Value.Find(x => x.PlayerIndex == player);
  21. if (config != null && config.EnableMotion)
  22. {
  23. string host = config.DsuServerHost;
  24. int port = config.DsuServerPort;
  25. _motionSource.RegisterClient((int)player, host, port);
  26. _motionSource.RequestData((int)player, config.Slot);
  27. if (config.ControllerType == ControllerType.JoyconPair && !config.MirrorInput)
  28. {
  29. _motionSource.RequestData((int)player, config.AltSlot);
  30. }
  31. }
  32. }
  33. public void Poll(PlayerIndex player, int slot)
  34. {
  35. InputConfig config = ConfigurationState.Instance.Hid.InputConfig.Value.Find(x => x.PlayerIndex == player);
  36. Orientation = new float[9];
  37. if (!config.EnableMotion || !_motionSource.TryGetData((int)player, slot, out MotionInput input))
  38. {
  39. Accelerometer = new Vector3();
  40. Gyroscope = new Vector3();
  41. return;
  42. }
  43. Gyroscope = Truncate(input.Gyroscrope * 0.0027f, 3);
  44. Accelerometer = Truncate(input.Accelerometer, 3);
  45. Rotation = Truncate(input.Rotation * 0.0027f, 3);
  46. Matrix4x4 orientation = input.GetOrientation();
  47. Orientation[0] = Math.Clamp(orientation.M11, -1f, 1f);
  48. Orientation[1] = Math.Clamp(orientation.M12, -1f, 1f);
  49. Orientation[2] = Math.Clamp(orientation.M13, -1f, 1f);
  50. Orientation[3] = Math.Clamp(orientation.M21, -1f, 1f);
  51. Orientation[4] = Math.Clamp(orientation.M22, -1f, 1f);
  52. Orientation[5] = Math.Clamp(orientation.M23, -1f, 1f);
  53. Orientation[6] = Math.Clamp(orientation.M31, -1f, 1f);
  54. Orientation[7] = Math.Clamp(orientation.M32, -1f, 1f);
  55. Orientation[8] = Math.Clamp(orientation.M33, -1f, 1f);
  56. }
  57. private static Vector3 Truncate(Vector3 value, int decimals)
  58. {
  59. float power = MathF.Pow(10, decimals);
  60. value.X = float.IsNegative(value.X) ? MathF.Ceiling(value.X * power) / power : MathF.Floor(value.X * power) / power;
  61. value.Y = float.IsNegative(value.Y) ? MathF.Ceiling(value.Y * power) / power : MathF.Floor(value.Y * power) / power;
  62. value.Z = float.IsNegative(value.Z) ? MathF.Ceiling(value.Z * power) / power : MathF.Floor(value.Z * power) / power;
  63. return value;
  64. }
  65. }
  66. }