Search Unity

Get Quaternion to Translate Between Two Vectors

Discussion in 'Scripting' started by Precursor13, Jun 15, 2012.

  1. Precursor13

    Precursor13

    Joined:
    Jun 22, 2010
    Posts:
    142
    Hey all, recently I've been trying to find a function capable of giving me a quaternion to translate between to Vectors, ie:

    Code (csharp):
    1. Quaternion rot = vectorRotationQ(Vector3.forward, Vector3.up);//would give me a vector that, when
    2.  
    3. Vector3 up = rot * Vector3.forward; //Should give me Vector3.up
    However, it would also need to work with more complex vectors, such as
    Code (csharp):
    1. vectorRotationQ(new Vector3(5,8,0), new Vector3(8,-2,0));
    So far, I've tried the following functions

    #1, doesn't work at all
    Code (csharp):
    1. public Quaternion vectorRotationQ(Vector3 from, Vector3 target) {
    2.     //  Vector3 target = this.m_CurrentTarget.position - this.m_Transform.position;
    3.         target.Normalize ();
    4.         from.Normalize();
    5.         float dot = Vector3.Dot (from, target);
    6.     //  Debug.Log(dot);
    7.         float angle = Mathf.Acos (dot) * 0.5f;
    8.        
    9.         Quaternion rotation = new Quaternion (0.0f, Mathf.Sin (angle), 0.0f, Mathf.Cos (angle));
    10.         rotation = rotation * Quaternion.LookRotation(from);
    11.         return rotation;
    12.         //this.m_Transform.forward = target;
    13.     }
    #2, works for simple vectors only

    Code (csharp):
    1. public Quaternion vectorRotationQ(Vector3 from, Vector3 target) {
    2.         return Quaternion.LookRotation(target) * Quaternion.Inverse(Quaternion.LookRotation(from));
    3.     }
    #3, also works for simple vectors (but not complex ones)

    Code (csharp):
    1. public Quaternion vectorRotationQ(Vector3 from, Vector3 target) {
    2.         return Quaternion.Euler(Quaternion.LookRotation(target).eulerAngles - Quaternion.LookRotation(from).eulerAngles);
    3.     }

    Sadly, none of these functions work for a more complex axis translation, ie Vector3(1,1,0) and Vector3.up would not give a rotation that could translate the former into the latter. I feel as if I must be making some insanely obvious mistake, anyone have an idea what that could be?
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Are all these Vector3s misleading? Everything above suggests that you're working in X and Y only.

    The v should be capitalized in the scripting languages Unity uses.
     
  3. Precursor13

    Precursor13

    Joined:
    Jun 22, 2010
    Posts:
    142
    I wouldn't really call Vector3.forward 2D, but at any rate, I just chose those vectors at random.
    As for the capitalization, if you're talking about the function named vectorRotationQ, I doubt that it would at all affect the returned value of said function (unless, of course, the debug information is somehow interfering with the function, in which case we have far more serious things to worry about).
     
  4. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
  5. Precursor13

    Precursor13

    Joined:
    Jun 22, 2010
    Posts:
    142
    Thanks JohnnyA, that's exactly the function I'm looking for. However, I'm more interested in the method Quaternion.FromToRotation uses to return the correct value. Anyone know what algorithm the function uses?
     
  6. congson19072001

    congson19072001

    Joined:
    Oct 18, 2017
    Posts:
    1
    thank you Johnny, your answer helped me a lot