CalibratableTracker.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using UnityEngine;
  2. using Valve.VR;
  3. namespace Tracking
  4. {
  5. public abstract class CalibratableTracker : MonoBehaviour
  6. {
  7. public Transform bicycleTransform;
  8. private Transform trackerTransform;
  9. private Vector3 zeroPos;
  10. private Vector3 zeroRot;
  11. protected abstract string KeyPrefix { get; }
  12. private string KeyPosX => $"{KeyPrefix}_vive_tracker_pos_x";
  13. private string KeyPosY => $"{KeyPrefix}_vive_tracker_pos_y";
  14. private string KeyPosZ => $"{KeyPrefix}_vive_tracker_pos_z";
  15. private string KeyRotX => $"{KeyPrefix}_vive_tracker_rot_x";
  16. private string KeyRotY => $"{KeyPrefix}_vive_tracker_rot_y";
  17. private string KeyRotZ => $"{KeyPrefix}_vive_tracker_rot_z";
  18. public Vector3 RelativeRotation
  19. {
  20. get
  21. {
  22. if (trackerTransform == null) return Vector3.zero; //TODO: something smarter
  23. var newVal = trackerTransform.localRotation.eulerAngles - zeroRot;
  24. var x = newVal.x;
  25. var y = newVal.y;
  26. var z = newVal.z;
  27. if (x > 180) x = -(360 - x);
  28. if (y > 180) y = -(360 - y);
  29. if (z > 180) z = -(360 - z);
  30. if (x < -180) x = -(-360 - x);
  31. if (y < -180) y = -(-360 - y);
  32. if (z < -180) z = -(-360 - z);
  33. return new Vector3(x, y, z);
  34. }
  35. }
  36. public Vector3 RelativePosition =>
  37. trackerTransform == null ? Vector3.zero : trackerTransform.localPosition - zeroPos;
  38. public Vector3 ZeroRot => zeroRot;
  39. public Vector3 ZeroPos => zeroPos;
  40. protected virtual void Start()
  41. {
  42. trackerTransform = transform;
  43. if (PlayerPrefs.HasKey(KeyPosX) && PlayerPrefs.HasKey(KeyPosY) && PlayerPrefs.HasKey(KeyPosZ))
  44. {
  45. var x = PlayerPrefs.GetFloat(KeyPosX);
  46. var y = PlayerPrefs.GetFloat(KeyPosY);
  47. var z = PlayerPrefs.GetFloat(KeyPosZ);
  48. zeroPos = new Vector3(x, y, z);
  49. }
  50. if (PlayerPrefs.HasKey(KeyRotX) && PlayerPrefs.HasKey(KeyRotY) && PlayerPrefs.HasKey(KeyRotZ))
  51. {
  52. var x = PlayerPrefs.GetFloat(KeyRotX);
  53. var y = PlayerPrefs.GetFloat(KeyRotY);
  54. var z = PlayerPrefs.GetFloat(KeyRotZ);
  55. zeroRot = new Vector3(x, y, z);
  56. }
  57. }
  58. //sets current position as zero -> straighten handlebar and call this function
  59. public void Calibrate()
  60. {
  61. zeroRot = trackerTransform.localRotation.eulerAngles;
  62. zeroPos = trackerTransform.localPosition;
  63. PlayerPrefs.SetFloat(KeyRotX, zeroRot.x);
  64. PlayerPrefs.SetFloat(KeyRotY, zeroRot.y);
  65. PlayerPrefs.SetFloat(KeyRotZ, zeroRot.z);
  66. PlayerPrefs.SetFloat(KeyPosX, zeroPos.x);
  67. PlayerPrefs.SetFloat(KeyPosY, zeroPos.y);
  68. PlayerPrefs.SetFloat(KeyPosZ, zeroPos.z);
  69. PlayerPrefs.Save();
  70. }
  71. public void OnConnectedChanged(SteamVR_Action_Pose pose, SteamVR_Input_ActionScopes sources, bool connected)
  72. {
  73. }
  74. public void OnConnectedChanged()
  75. {
  76. Debug.Log("connected changed");
  77. }
  78. }
  79. }