Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to compare two quaternions?

Discussion in 'Scripting' started by Harter, Feb 21, 2013.

  1. Harter

    Harter

    Joined:
    Feb 28, 2012
    Posts:
    119
    Hey guys,

    Seems like I found the worst way to do it:
    Code (csharp):
    1.         bool RotationEquals(Quaternion r1, Quaternion r2)
    2.         {
    3.             float abs = Mathf.Abs(Quaternion.Dot(r1, r2));
    4.             if (abs >= 0.9999999f)
    5.                 return true;
    6.             return false;
    7.         }
    How exactly should I use Quaternion.Angle instead of this crap in my case?

    Thanks
     
  2. Harter

    Harter

    Joined:
    Feb 28, 2012
    Posts:
    119
    Is it possible to do without epsilon at all?
     
    Last edited: Feb 21, 2013
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  4. saadali211

    saadali211

    Joined:
    Feb 17, 2017
    Posts:
    19

    bool compareQuaternion(Quaternion q1,Quaternion q2)
    {
    if (q1.x == q2.x&& q1.y == q2.y && q1.z == q2.z && q1.w == q2.w)
    {
    return true;
    }

    return false;
    }
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Except not because comparing floats like that is a no-no
     
    falkovds and lordofduct like this.
  6. falkovds

    falkovds

    Joined:
    Sep 30, 2017
    Posts:
    3
    I have something like this:
    Code (CSharp):
    1. Quaternion q1= xxxx;
    2. Quaternion q2= yyyy;
    3. while (q1 != q2)
    4.     {
    5.         q1 = Quaternion.Lerp(q1, q2, rotationSpeed * Time.deltaTime);
    6.         Debug.Log(Mathf.Abs(Quaternion.Dot(q1,q2)));
    7.         yield return null;
    8.     }
    And if I see in log:
    Code (CSharp):
    1. Mathf.Abs(Quaternion.Dot(q1,q2))==1
    then the cycle becomes infinite. That is, if quaternions become absolutely equal, then the condition is not fulfilled for some reason. And it happens sometimes.
    But if condition:
    Code (CSharp):
    1. Mathf.Abs(Quaternion.Dot(q1,q2))==0.999999x
    then the cycle breaks correctly.
    So the top option isn’t so bad
     
    Last edited: May 21, 2020
  7. falkovds

    falkovds

    Joined:
    Sep 30, 2017
    Posts:
    3
    I changed code to:
    Code (CSharp):
    1. Quaternion q1= xxxx;
    2. Quaternion q2= yyyy;
    3. while (Mathf.Abs(Quaternion.Dot(q1,q2))<0.999999f)
    4.     {
    5.         q1 = Quaternion.Lerp(q1, q2, rotationSpeed * Time.deltaTime);
    6.         Debug.Log(Mathf.Abs(Quaternion.Dot(q1,q2)));
    7.         yield return null;
    8.     }
    Now it works more stable.