MatchPlayerPositionAndCalibrate.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. //TODO: show tracker status
  72. if (calibrationMode == CalibrationMode.AutoCountdown)
  73. {
  74. var display = Instantiate(countdownHud);
  75. display.OnCountdownDone = () =>
  76. {
  77. Match();
  78. return "Calibrated!";
  79. };
  80. autoSet = true;
  81. }
  82. else
  83. {
  84. Match();
  85. autoSet = true;
  86. }
  87. }
  88. private void DoMatching()
  89. {
  90. if (matching == 1)
  91. {
  92. MatchFrontWheel();
  93. matching++;
  94. }
  95. else if (matching == 2)
  96. {
  97. MatchLeg();
  98. if (calibrationMode == CalibrationMode.MatchOnly || calibrationMode == CalibrationMode.None)
  99. {
  100. matching = 0;
  101. }
  102. else
  103. {
  104. matching++;
  105. }
  106. }
  107. else if (matching == 3)
  108. {
  109. legTracker.Calibrate();
  110. frontWheelTracker.Calibrate();
  111. matching = 0;
  112. }
  113. }
  114. }
  115. }