MatchPlayerPositionAndCalibrate.cs 4.5 KB

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