Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Can't figure out applying multiple rotations without issues

Discussion in 'Scripting' started by TheMaximL, Jun 23, 2020.

  1. TheMaximL

    TheMaximL

    Joined:
    Jul 27, 2019
    Posts:
    50
    At my wits end trying to get a custom VR system working. It use grab points that are created based on the position and rotation of the players hand in the space of joint creator when the grab the object. When on just one axis rotations work, however when I enable two or more the thing very often break and cause weird rotations. Sometimes it works with two rotational axis enabled, most of the time it doesn't. I'm relativley new to C# so I only have a limited quess with what's going on and I'm think it something to do with gimble lock. Below is the jank-fest that is my rotational code.

    Code (CSharp):
    1.     void RotateGrabPoint(XRJointHandInteractor interactor)
    2.             {
    3.                 Transform grabRootHandPoint = m_currentGrabPoint.handPoseTemplate.transform;
    4.                 Quaternion grabRootRot = Quaternion.Inverse(transform.rotation) * grabRootHandPoint.rotation;
    5.  
    6.                 Transform handPoint = interactor.ReturnPhysicsHand().transform;
    7.                 Quaternion handRot = Quaternion.Inverse(transform.rotation) * handPoint.rotation;
    8.                 Quaternion rotDistance = grabRootRot * Quaternion.Inverse(handRot);
    9.  
    10.                 Vector3 eularRot = m_currentGrabPoint.handOffset.transform.localRotation.eulerAngles;
    11.                 Quaternion finalRot = m_currentGrabPoint.handOffset.transform.localRotation;
    12.  
    13.                 float xRot = eularRot.x;
    14.                 float yRot = eularRot.y;
    15.                 float zRot = eularRot.z;
    16.  
    17.                 if (rotZ)
    18.                 {
    19.                     zRot  -= rotDistance.eulerAngles.z;
    20.                 }
    21.                 if (rotX)
    22.                 {
    23.                     eularRot.x -= rotDistance.eulerAngles.x;
    24.                 }
    25.                 if (rotY)
    26.                 {
    27.                     yRot -= rotDistance.eulerAngles.y;
    28.                 }
    29.  
    30.                 Debug.Log(m_currentGrabPoint.handOffset.transform.localRotation.eulerAngles);
    31.                 m_currentGrabPoint.handOffset.transform.localRotation = Quaternion.Euler(xRot, yRot, zRot);
    32.             }
    33.  
    Any help would be truly appriciated as I had no idea where to go with this, or even what I'm doing wrong.

    Cheers
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    248
    Hi,

    Have you tried to use quaternions to apply the inverse rotation instead of taking the path over euler?
    like:
    m_currentGrabPoint.handOffset.transform.localRotation = m_currentGrabPoint.handOffset.transform.localRotation *
    Quaternion.Inverse(rotDistance);
     
  3. TheMaximL

    TheMaximL

    Joined:
    Jul 27, 2019
    Posts:
    50
    I haven’t tried that no, how would I go about separating that into separate axis? As dependent on the object I need to be able to enable and disable rotations on certain axis.
     
  4. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    248
    Think it would be easy to compare the result, to confirm, or rule out the gimbal lock theory.
    If it make sense to modify all three of the axis at the same time that is.
     
unityunity