- Home /
 
 
               Question by 
               torrpathian · Aug 05, 2021 at 12:58 PM · 
                c#errorquaternioneuler  
              
 
              Why the calculated quaternions not the same as recieved from transform.localRotation?
 Cant understand what is wrong with my code that calculates euler or quaternions from two vectors. I have a rigged model with bones hierarchy, so try to find the angles between 14 and 15 bones. For that reason i calculate 2 vectors Hand and Elb using there positions (transform.position). Then I try to compare my calculated vector for normal and with current localRotation of bone 15 but it is definitely not the same. Why is there such a huge error or where is the mistake in such method of bones rotation calculate? 
 private Vector3 calcEulersYawPitchRoll(Vector3 N, float angle){
     Vector3 eulers = new Vector3();
     eulers.x = Mathf.Atan2(N.y * Mathf.Sin(angle)- N.x * N.z * (1 - Mathf.Cos(angle)) , 1 - (N.y * N.y + N.z * N.z) * (1 - Mathf.Cos(angle))) * Mathf.Rad2Deg;
     eulers.y = Mathf.Asin(N.x * N.y * (1 - Mathf.Cos(angle)) + N.z * Mathf.Sin(angle)) * Mathf.Rad2Deg;
     eulers.z = Mathf.Atan2(N.x * Mathf.Sin(angle) - N.y * N.z * (1 - Mathf.Cos(angle)) , 1 - (N.x * N.x + N.z * N.z) * (1 - Mathf.Cos(angle))) * Mathf.Rad2Deg;
     return eulers;
 }
 private void DisplayQuaternAndNormal(){
     Quaternion hand = m_BoneMapping[15].transform.localRotation;
     float angle = 2 *  Mathf.Acos(hand.w) * Mathf.Rad2Deg;
     float qw = Mathf.Sqrt(1 - hand.w * hand.w);
     Vector3 NfromQuat = new Vector3(hand.x / qw, hand.y / qw, hand.z / qw);
     Vector3 ShldP = m_BoneMapping[14].transform.position;
     Vector3 ArmP = m_BoneMapping[15].transform.position;
     Vector3 ElbP = m_BoneMapping[16].transform.position;
     Vector3 Hand = ElbP - ArmP; Vector3.Normalize(Hand);
     Vector3 Elb = ArmP - ShldP; Vector3.Normalize(Elb);
     float angle2 = Vector3.Angle(Hand, Elb);
     Vector3 N = Vector3.Cross(Hand, Elb).normalized;
     Vector3 Ncalc = calcEulersYawPitchRoll(N, angle2 * Mathf.Deg2Rad);
     Vector3 Neulers = m_BoneMapping[15].transform.localRotation.eulerAngles;
     if(Ncalc.x <= 0){
         Ncalc.x += 360;
     }
     if(Ncalc.y <= 0){
         Ncalc.y += 360;
     }
     if(Ncalc.z <= 0){
         Ncalc.z += 360;
     }
     Debug.Log("Comapare Angle Quat is " + angle  + " Calculated is " + angle2);
     Debug.Log("Comapare Eulers is " + Neulers.x + " " +  Neulers.y + " " + Neulers.z + " " + " Calculated is " + + Ncalc.x + " " +  Ncalc.y + " " + Ncalc.z);
     Debug.Log("Comapare Quaternions is " + NfromQuat.x + " " +  NfromQuat.y + " " + NfromQuat.z + " " + " Calculated is " + + N.x + " " +  N.y + " " + N.z);
 }
 
               
                 
                ezgifcom-gif-maker.gif 
                (439.8 kB) 
               
 
              
               Comment
              
 
               
              Your answer