MatchPlayerPositionAndCalibrate.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System.Collections;
  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. [Header("HMD")] public bool calibrateHmd = true;
  24. public LookStraightDisplay lookStraightHud;
  25. public CameraTracker hmdTracker;
  26. public Transform bikeTransform;
  27. private bool autoSet;
  28. private SteamVR_TrackedObject fwTrackedObject;
  29. private Transform fwtTransform;
  30. private SteamVR_TrackedObject legTrackedObject;
  31. private Transform legTransform;
  32. private int matching;
  33. private Transform playerTransform;
  34. private void Start()
  35. {
  36. fwtTransform = frontWheelTracker.transform;
  37. legTransform = legTracker.transform;
  38. playerTransform = player.transform;
  39. fwTrackedObject = legTracker.GetComponent<SteamVR_TrackedObject>();
  40. legTrackedObject = frontWheelTracker.GetComponent<SteamVR_TrackedObject>();
  41. }
  42. private void Update()
  43. {
  44. AutoCalibration();
  45. DoMatching();
  46. }
  47. public void Match()
  48. {
  49. matching = 1;
  50. }
  51. public void setDesiredToReal()
  52. {
  53. //TODO
  54. }
  55. private void MatchFrontWheel()
  56. {
  57. var dif = frontWheelDesiredPosition.position - fwtTransform.position;
  58. playerTransform.position += dif;
  59. StartCoroutine(HideFrontWheelTracker());
  60. }
  61. private void MatchLeg()
  62. {
  63. //The only way to match it now is by rotating
  64. var fwtPosition = fwtTransform.position;
  65. var fwtToDesiredLeg = legDesiredPosition.position - fwtPosition;
  66. var fwtToRealLeg = legTransform.position - fwtPosition;
  67. var dl2d = new Vector2(fwtToDesiredLeg.x, fwtToDesiredLeg.z);
  68. var dr2d = new Vector2(fwtToRealLeg.x, fwtToRealLeg.z);
  69. var angle = Vector2.Angle(dl2d, dr2d);
  70. playerTransform.RotateAround(fwtPosition, playerTransform.up, -angle);
  71. StartCoroutine(HideLegTracker());
  72. }
  73. private IEnumerator HideFrontWheelTracker()
  74. {
  75. yield return new WaitForSeconds(1);
  76. frontWheelDesiredPosition.gameObject.SetActive(false);
  77. frontWheelTracker.gameObject.SetActive(false);
  78. }
  79. private IEnumerator HideLegTracker()
  80. {
  81. yield return new WaitForSeconds(1);
  82. legDesiredPosition.gameObject.SetActive(false);
  83. legTracker.gameObject.SetActive(false);
  84. }
  85. private void AutoCalibration()
  86. {
  87. if (calibrationMode == CalibrationMode.None || autoSet || !fwTrackedObject.isValid ||
  88. !legTrackedObject.isValid) return;
  89. //TODO: show tracker status
  90. if (calibrationMode == CalibrationMode.AutoCountdown)
  91. {
  92. var display = Instantiate(countdownHud);
  93. display.OnCountdownDone = () =>
  94. {
  95. Match();
  96. return "Calibrated!";
  97. };
  98. autoSet = true;
  99. }
  100. else
  101. {
  102. Match();
  103. autoSet = true;
  104. }
  105. }
  106. private void DoMatching()
  107. {
  108. if (matching == 1)
  109. {
  110. MatchFrontWheel();
  111. matching++;
  112. }
  113. else if (matching == 2)
  114. {
  115. MatchLeg();
  116. if (calibrationMode == CalibrationMode.MatchOnly || calibrationMode == CalibrationMode.None)
  117. matching = 0;
  118. else
  119. matching++;
  120. }
  121. else if (matching == 3)
  122. {
  123. legTracker.Calibrate();
  124. frontWheelTracker.Calibrate();
  125. matching = 0;
  126. DoLookStraightIfWanted();
  127. }
  128. }
  129. private void DoLookStraightIfWanted()
  130. {
  131. if (!calibrateHmd) return;
  132. var lookStraightDisplay = Instantiate(lookStraightHud);
  133. lookStraightDisplay.OnDone += OnLookStraightDone;
  134. var lTransform = lookStraightDisplay.transform;
  135. var newPos = bikeTransform.position + bikeTransform.forward * 1.4f;
  136. newPos.y = hmdTracker.transform.position.y;
  137. lTransform.position = newPos;
  138. }
  139. private void OnLookStraightDone()
  140. {
  141. hmdTracker.Calibrate();
  142. }
  143. }
  144. }