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. protected abstract string KeyPrefix { get; }
  8. private string KeyPosX => $"{KeyPrefix}_vive_tracker_pos_x";
  9. private string KeyPosY => $"{KeyPrefix}_vive_tracker_pos_y";
  10. private string KeyPosZ => $"{KeyPrefix}_vive_tracker_pos_z";
  11. private string KeyRotX => $"{KeyPrefix}_vive_tracker_rot_x";
  12. private string KeyRotY => $"{KeyPrefix}_vive_tracker_rot_y";
  13. private string KeyRotZ => $"{KeyPrefix}_vive_tracker_rot_z";
  14. private Transform trackerTransform;
  15. private Vector3 zeroRot;
  16. private Vector3 zeroPos;
  17. public Transform bicycleTransform;
  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 => trackerTransform == null ? Vector3.zero : trackerTransform.localPosition - zeroPos;
  37. public Vector3 ZeroRot => zeroRot;
  38. public Vector3 ZeroPos => zeroPos;
  39. protected virtual void Start()
  40. {
  41. trackerTransform = transform;
  42. if (PlayerPrefs.HasKey(KeyPosX) && PlayerPrefs.HasKey(KeyPosY) && PlayerPrefs.HasKey(KeyPosZ))
  43. {
  44. var x = PlayerPrefs.GetFloat(KeyPosX);
  45. var y = PlayerPrefs.GetFloat(KeyPosY);
  46. var z = PlayerPrefs.GetFloat(KeyPosZ);
  47. zeroPos = new Vector3(x, y, z);
  48. }
  49. if (PlayerPrefs.HasKey(KeyRotX) && PlayerPrefs.HasKey(KeyRotY) && PlayerPrefs.HasKey(KeyRotZ))
  50. {
  51. var x = PlayerPrefs.GetFloat(KeyRotX);
  52. var y = PlayerPrefs.GetFloat(KeyRotY);
  53. var z = PlayerPrefs.GetFloat(KeyRotZ);
  54. zeroRot = new Vector3(x, y, z);
  55. }
  56. }
  57. //sets current position as zero -> straighten handlebar and call this function
  58. public void Calibrate()
  59. {
  60. zeroRot = trackerTransform.localRotation.eulerAngles;
  61. zeroPos = trackerTransform.localPosition;
  62. PlayerPrefs.SetFloat(KeyRotX, zeroRot.x);
  63. PlayerPrefs.SetFloat(KeyRotY, zeroRot.y);
  64. PlayerPrefs.SetFloat(KeyRotZ, zeroRot.z);
  65. PlayerPrefs.SetFloat(KeyPosX, zeroPos.x);
  66. PlayerPrefs.SetFloat(KeyPosY, zeroPos.y);
  67. PlayerPrefs.SetFloat(KeyPosZ, zeroPos.z);
  68. PlayerPrefs.Save();
  69. }
  70. public void OnConnectedChanged(SteamVR_Action_Pose pose, SteamVR_Input_ActionScopes sources, bool connected)
  71. {
  72. }
  73. public void OnConnectedChanged()
  74. {
  75. Debug.Log("connected changed");
  76. }
  77. }
  78. }