MatchPlayerPositionAndCalibrate.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using Display;
  3. using UnityEngine;
  4. using Valve.VR;
  5. namespace Tracking
  6. {
  7. public enum CalibrationMode
  8. {
  9. None,
  10. MatchOnly,
  11. AutoImmediate,
  12. AutoCountdown
  13. }
  14. public class MatchPlayerPositionAndCalibrate : MonoBehaviour
  15. {
  16. public Transform player;
  17. public CalibrationMode calibrationMode;
  18. public CountdownDisplay countdownHud;
  19. [Header("Front Wheel")] public Transform frontWheelDesiredPosition;
  20. public FrontWheelTracker frontWheelTracker;
  21. [Header("Trainer Leg")] public Transform legDesiredPosition;
  22. public KineticLegTracker legTracker;
  23. private Transform fwtTransform;
  24. private Transform legTransform;
  25. private Transform playerTransform;
  26. private SteamVR_TrackedObject fwTrackedObject;
  27. private SteamVR_TrackedObject legTrackedObject;
  28. private int matching;
  29. private bool autoSet;
  30. private void Start()
  31. {
  32. fwtTransform = frontWheelTracker.transform;
  33. legTransform = legTracker.transform;
  34. playerTransform = player.transform;
  35. fwTrackedObject = legTracker.GetComponent<SteamVR_TrackedObject>();
  36. legTrackedObject = frontWheelTracker.GetComponent<SteamVR_TrackedObject>();
  37. }
  38. public void Match()
  39. {
  40. matching = 1;
  41. }
  42. public void setDesiredToReal()
  43. {
  44. //TODO
  45. }
  46. private void MatchFrontWheel()
  47. {
  48. var dif = frontWheelDesiredPosition.position - fwtTransform.position;
  49. playerTransform.position += dif;
  50. }
  51. private void MatchLeg()
  52. {
  53. //The only way to match it now is by rotating
  54. var fwtPosition = fwtTransform.position;
  55. var fwtToDesiredLeg = legDesiredPosition.position - fwtPosition;
  56. var fwtToRealLeg = legTransform.position - fwtPosition;
  57. var dl2d = new Vector2(fwtToDesiredLeg.x, fwtToDesiredLeg.z);
  58. var dr2d = new Vector2(fwtToRealLeg.x, fwtToRealLeg.z);
  59. var angle = Vector2.Angle(dl2d, dr2d);
  60. playerTransform.RotateAround(fwtPosition, playerTransform.up, -angle);
  61. }
  62. private void Update()
  63. {
  64. AutoCalibration();
  65. DoMatching();
  66. }
  67. private void AutoCalibration()
  68. {
  69. if (calibrationMode == CalibrationMode.None || autoSet || !fwTrackedObject.isValid ||
  70. !legTrackedObject.isValid) return;
  71. if (calibrationMode == CalibrationMode.AutoCountdown)
  72. {
  73. var display = Instantiate(countdownHud);
  74. display.OnCountdownDone = () =>
  75. {
  76. Match();
  77. return "Calibrated!";
  78. };
  79. autoSet = true;
  80. }
  81. else
  82. {
  83. Match();
  84. autoSet = true;
  85. }
  86. }
  87. private void DoMatching()
  88. {
  89. if (matching == 1)
  90. {
  91. MatchFrontWheel();
  92. matching++;
  93. }
  94. else if (matching == 2)
  95. {
  96. MatchLeg();
  97. if (calibrationMode == CalibrationMode.MatchOnly || calibrationMode == CalibrationMode.None)
  98. {
  99. matching = 0;
  100. }
  101. else
  102. {
  103. matching++;
  104. }
  105. }
  106. else if (matching == 3)
  107. {
  108. legTracker.Calibrate();
  109. frontWheelTracker.Calibrate();
  110. matching = 0;
  111. }
  112. }
  113. }
  114. }