FrontWheelTracker.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using UnityEngine;
  3. namespace Tracking
  4. {
  5. public class FrontWheelTracker : MonoBehaviour
  6. {
  7. private const string KEY_POS_X = "vive_tracker_pos_x";
  8. private const string KEY_POS_Y = "vive_tracker_pos_y";
  9. private const string KEY_POS_Z = "vive_tracker_pos_z";
  10. private const string KEY_ROT_X = "vive_tracker_rot_x";
  11. private const string KEY_ROT_Y = "vive_tracker_rot_y";
  12. private const string KEY_ROT_Z = "vive_tracker_rot_z";
  13. private Transform trackerTransform;
  14. private Vector3 zeroRot;
  15. private Vector3 zeroPos;
  16. public Transform bicycleTransform;
  17. public float trackerDistanceToWheelCenterPoint;
  18. public Vector3 Rotation
  19. {
  20. get
  21. {
  22. var newVal = trackerTransform.localRotation.eulerAngles - zeroRot;
  23. var x = newVal.x;
  24. var y = newVal.y;
  25. var z = newVal.z;
  26. if (x > 180) x = -(360 - x);
  27. if (y > 180) y = -(360 - y);
  28. if (z > 180) z = -(360 - z);
  29. if (x < -180) x = -(-360 - x);
  30. if (y < -180) y = -(-360 - y);
  31. if (z < -180) z = -(-360 - z);
  32. return new Vector3(x, y, z);
  33. }
  34. }
  35. public float CalculatedSteerRotation
  36. {
  37. get
  38. {
  39. var posOnBikePlane = Vector3.ProjectOnPlane(Position, bicycleTransform.up);
  40. var theta = Mathf.Acos(Mathf.Clamp(posOnBikePlane.x / trackerDistanceToWheelCenterPoint, -1f, 1f));
  41. return 90f - theta * Mathf.Rad2Deg;
  42. }
  43. }
  44. public Vector3 Position => trackerTransform.localPosition - zeroPos;
  45. public Vector3 ZeroRot => zeroRot;
  46. public Vector3 ZeroPos => zeroPos;
  47. public void Init()
  48. {
  49. trackerTransform = transform;
  50. if (PlayerPrefs.HasKey(KEY_POS_X) && PlayerPrefs.HasKey(KEY_POS_Y) && PlayerPrefs.HasKey(KEY_POS_Z))
  51. {
  52. var x = PlayerPrefs.GetFloat(KEY_POS_X);
  53. var y = PlayerPrefs.GetFloat(KEY_POS_Y);
  54. var z = PlayerPrefs.GetFloat(KEY_POS_Z);
  55. zeroPos = new Vector3(x, y, z);
  56. }
  57. if (PlayerPrefs.HasKey(KEY_ROT_X) && PlayerPrefs.HasKey(KEY_ROT_Y) && PlayerPrefs.HasKey(KEY_ROT_Z))
  58. {
  59. var x = PlayerPrefs.GetFloat(KEY_ROT_X);
  60. var y = PlayerPrefs.GetFloat(KEY_ROT_Y);
  61. var z = PlayerPrefs.GetFloat(KEY_ROT_Z);
  62. zeroRot = new Vector3(x, y, z);
  63. }
  64. }
  65. //sets current position as zero -> straighten handlebar and call this function
  66. public void Calibrate()
  67. {
  68. zeroRot = trackerTransform.localRotation.eulerAngles;
  69. zeroPos = trackerTransform.localPosition;
  70. PlayerPrefs.SetFloat(KEY_ROT_X, zeroRot.x);
  71. PlayerPrefs.SetFloat(KEY_ROT_Y, zeroRot.y);
  72. PlayerPrefs.SetFloat(KEY_ROT_Z, zeroRot.z);
  73. PlayerPrefs.SetFloat(KEY_POS_X, zeroPos.x);
  74. PlayerPrefs.SetFloat(KEY_POS_Y, zeroPos.y);
  75. PlayerPrefs.SetFloat(KEY_POS_Z, zeroPos.z);
  76. PlayerPrefs.Save();
  77. }
  78. }
  79. }