HMDTracker.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. namespace Tracking
  5. {
  6. public class HMDTracker : CalibratableTracker
  7. {
  8. public int framesToConsider = 10;
  9. public float SteerRotation => framesToConsider > 1
  10. ? CalculateSteerRotationMultiFrame()
  11. : CalculateSteerRotationSingleFrame();
  12. public float LeanRotation => framesToConsider > 1
  13. ? CalculateLeanRotationMultiFrame()
  14. : CalculateLeanRotationSingleFrame();
  15. protected override string KeyPrefix => "fw";
  16. private Queue<float> previousValues;
  17. private Queue<float> previousLeanValues;
  18. private void OnEnable()
  19. {
  20. if (framesToConsider > 1) previousValues = new Queue<float>(framesToConsider);
  21. if (framesToConsider > 1) previousLeanValues = new Queue<float>(framesToConsider);
  22. }
  23. /*private void OnGUI()
  24. {
  25. GUI.TextArea(new Rect(0, 400, 200, 90),
  26. $"LeanRotation: {LeanRotation}\nRelativeRotation: {RelativeRotation}\nRelativePosition: {RelativePosition}");
  27. }*/
  28. private float CalculateSteerRotationSingleFrame()
  29. {
  30. return RelativeRotation.y;
  31. }
  32. private float CalculateSteerRotationMultiFrame()
  33. {
  34. var angleThisFrame = CalculateSteerRotationSingleFrame();
  35. if (previousValues.Count > framesToConsider) previousValues.Dequeue();
  36. previousValues.Enqueue(angleThisFrame);
  37. //easiest way -> avg; TODO maybe in future, remove outliers
  38. return previousValues.Average();
  39. }
  40. private float CalculateLeanRotationSingleFrame()
  41. {
  42. return RelativeRotation.z;
  43. }
  44. private float CalculateLeanRotationMultiFrame()
  45. {
  46. var angleThisFrame = CalculateLeanRotationSingleFrame();
  47. if (previousLeanValues.Count > framesToConsider) previousLeanValues.Dequeue();
  48. previousLeanValues.Enqueue(angleThisFrame);
  49. //easiest way -> avg; TODO maybe in future, remove outliers
  50. return previousLeanValues.Average();
  51. }
  52. }
  53. }