MotionInput.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Ryujinx.Input.Motion;
  2. using System;
  3. using System.Numerics;
  4. namespace Ryujinx.Input
  5. {
  6. public class MotionInput
  7. {
  8. public ulong TimeStamp { get; set; }
  9. public Vector3 Accelerometer { get; set; }
  10. public Vector3 Gyroscrope { get; set; }
  11. public Vector3 Rotation { get; set; }
  12. private readonly MotionSensorFilter _filter;
  13. public MotionInput()
  14. {
  15. TimeStamp = 0;
  16. Accelerometer = new Vector3();
  17. Gyroscrope = new Vector3();
  18. Rotation = new Vector3();
  19. // TODO: RE the correct filter.
  20. _filter = new MotionSensorFilter(0f);
  21. }
  22. public void Update(Vector3 accel, Vector3 gyro, ulong timestamp, int sensitivity, float deadzone)
  23. {
  24. if (TimeStamp != 0)
  25. {
  26. Accelerometer = -accel;
  27. if (gyro.Length() < deadzone)
  28. {
  29. gyro = Vector3.Zero;
  30. }
  31. gyro *= (sensitivity / 100f);
  32. Gyroscrope = gyro;
  33. float deltaTime = MathF.Abs((long)(timestamp - TimeStamp) / 1000000f);
  34. Vector3 deltaGyro = gyro * deltaTime;
  35. Rotation += deltaGyro;
  36. _filter.SamplePeriod = deltaTime;
  37. _filter.Update(accel, DegreeToRad(gyro));
  38. }
  39. TimeStamp = timestamp;
  40. }
  41. public Matrix4x4 GetOrientation()
  42. {
  43. return Matrix4x4.CreateFromQuaternion(_filter.Quaternion);
  44. }
  45. private static Vector3 DegreeToRad(Vector3 degree)
  46. {
  47. return degree * (MathF.PI / 180);
  48. }
  49. }
  50. }