Search Unity

Quaternion substraction issue

Discussion in 'Scripting' started by entirelydoomed, Jul 24, 2021.

  1. entirelydoomed

    entirelydoomed

    Joined:
    Mar 26, 2021
    Posts:
    67
    I'm trying to get the difference between two quaternions but as it often happens with quaternions something is going wrong and i don't understand what.
    Code (CSharp):
    1.  
    2. private void Carry()
    3.     {
    4.         if (pickedUp)
    5.         {
    6.             Vector3 currPos = rb.position;
    7.             Vector3 targetPos = pointRb.position;
    8.             rb.velocity = (targetPos - currPos) * step;
    9.            
    10.             if (Input.GetMouseButtonDown(0))
    11.             {
    12.                 iniRot = rb.rotation;
    13.                 print("iniRot   " + iniRot.eulerAngles);
    14.                
    15.                 FPSController.instance.lockCamera = true;
    16.                 rb.angularDrag = 10;
    17.             }
    18.             if (Input.GetMouseButtonUp(0))
    19.             {
    20.                 FPSController.instance.lockCamera = false;
    21.                 rb.angularDrag = 1;
    22.                 rotDiff = rb.rotation * Quaternion.Inverse(iniRot);
    23.                
    24.                 print("currRot   " + rb.rotation.eulerAngles);
    25.                 print("rotDiff   " + rotDiff.eulerAngles);
    26.             }
    27.             if (Input.GetMouseButton(0))
    28.             {  
    29.                 float x = Input.GetAxis("Mouse X");
    30.                 float y = Input.GetAxis("Mouse Y");
    31.                
    32.                 Vector3 mouseDir = new Vector3(x, y, 0);
    33.                 mouseDir = cam.TransformDirection(mouseDir);
    34.                
    35.                
    36.                 Vector3 axis = Vector3.Cross(mouseDir, cam.forward).normalized;
    37.                
    38.                 Debug.DrawRay(transform.position, axis * 10f, Color.magenta);
    39.  
    40.                 float angle = step * rotSpeed;
    41.  
    42.  
    43.                 Vector3 velocity = angle * axis * Mathf.Rad2Deg;
    44.  
    45.                 rb.angularVelocity += velocity;
    46.             }
    47.             else
    48.             {
    49.                 Quaternion currRot = rb.rotation;
    50.                 Quaternion targetRot = pointRb.rotation;
    51.                 targetRot = pointRb.rotation * rotDiff;
    52.        
    53.                 Quaternion diff = targetRot * Quaternion.Inverse(currRot);
    54.                
    55.                 Vector3 angularDisplacement = PhysicsHelper.QuaternionToAngularVelocity(diff);
    56.                 Vector3 angularSpeed = angularDisplacement * step;
    57.                
    58.                 rb.angularVelocity = angularSpeed;
    59.             }
    60.         }
    61.     }
    In this code i'm trying to store rigidbody rotation in the moment when i press LMB and substract it from rotation in the moment when i release LMB. The output is pretty strange and i don't get whether it is the problem of quaternion representation in eulerangles or just my fault somewhere.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    I think maybe you want:
    Code (CSharp):
    1. Quaternion diff = Quaternion.FromToRotation(targetRot, currRot);
     
  3. entirelydoomed

    entirelydoomed

    Joined:
    Mar 26, 2021
    Posts:
    67
    I tried that but sadly the result wasn't right. Does it do the same as manually substracting quaternions? If so, then maybe substraction wasn't a problem...