MoveLogic.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using SC.XR.Unity.Module_InputSystem;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class MoveLogic
  6. {
  7. private float pointerRefDistance;
  8. private bool pointerPosIndependentOfHead = true;
  9. private Vector3 pointerLocalGrabPoint;
  10. private Vector3 objectLocalGrabPoint;
  11. private Vector3 grabToObject;
  12. /// <summary>
  13. /// Setup function
  14. /// </summary>
  15. public void Setup(SCPose pointerCentroidPose, Vector3 grabCentroid, Transform objectPose, Vector3 objectScale)
  16. {
  17. pointerRefDistance = GetDistanceToBody(pointerCentroidPose);
  18. pointerPosIndependentOfHead = pointerRefDistance != 0;
  19. Quaternion worldToPointerRotation = Quaternion.Inverse(pointerCentroidPose.rotation);
  20. pointerLocalGrabPoint = worldToPointerRotation * (grabCentroid - pointerCentroidPose.position);
  21. objectLocalGrabPoint = Quaternion.Inverse(objectPose.rotation) * (grabCentroid - objectPose.position);
  22. objectLocalGrabPoint = Vector3.Scale(objectLocalGrabPoint, new Vector3(1f / objectScale.x, 1f / objectScale.y, 1f / objectScale.z)) ;
  23. grabToObject = objectPose.position - grabCentroid;
  24. }
  25. /// <summary>
  26. /// Update the position based on input.
  27. /// </summary>
  28. /// <returns>A Vector3 describing the desired position</returns>
  29. public Vector3 Update(SCPose pointerCentroidPose, Quaternion objectRotation, Vector3 objectScale, bool usePointerRotation)
  30. {
  31. float distanceRatio = 1.0f;
  32. if (pointerPosIndependentOfHead)
  33. {
  34. // Compute how far away the object should be based on the ratio of the current to original hand distance
  35. float currentHandDistance = GetDistanceToBody(pointerCentroidPose);
  36. distanceRatio = currentHandDistance / pointerRefDistance;
  37. }
  38. if (usePointerRotation)
  39. {
  40. Vector3 scaledGrabToObject = Vector3.Scale(objectLocalGrabPoint, objectScale);
  41. Vector3 adjustedPointerToGrab = (pointerLocalGrabPoint * distanceRatio);
  42. adjustedPointerToGrab = pointerCentroidPose.rotation * adjustedPointerToGrab;
  43. return adjustedPointerToGrab - objectRotation * scaledGrabToObject + pointerCentroidPose.position;
  44. }
  45. else
  46. {
  47. return pointerCentroidPose.position + (pointerCentroidPose.rotation * pointerLocalGrabPoint + grabToObject) * distanceRatio;
  48. }
  49. }
  50. private float GetDistanceToBody(SCPose pointerCentroidPose)
  51. {
  52. // The body is treated as a ray, parallel to the y-axis, where the start is head position.
  53. // This means that moving your hand down such that is the same distance from the body will
  54. // not cause the manipulated object to move further away from your hand. However, when you
  55. // move your hand upward, away from your head, the manipulated object will be pushed away.
  56. if (pointerCentroidPose.position.y > API_GSXR_Slam.SlamManager.head.transform.position.y)
  57. {
  58. return Vector3.Distance(pointerCentroidPose.position, API_GSXR_Slam.SlamManager.head.transform.position);
  59. }
  60. else
  61. {
  62. Vector2 headPosXZ = new Vector2(API_GSXR_Slam.SlamManager.head.transform.position.x, API_GSXR_Slam.SlamManager.head.transform.position.z);
  63. Vector2 pointerPosXZ = new Vector2(pointerCentroidPose.position.x, pointerCentroidPose.position.z);
  64. return Vector2.Distance(pointerPosXZ, headPosXZ);
  65. }
  66. }
  67. }