Search Unity

Average quaternion between two quaternions

Discussion in 'Scripting' started by AleVerDes, Jul 29, 2014.

  1. AleVerDes

    AleVerDes

    Joined:
    Jun 16, 2014
    Posts:
    40
    Hello!

    How do I find a quaternion, whose direction is strictly between two given quaternions? In 2D space was simple: (directionA + directionB) / 2, but the conditions were by then what area of the axis is used - left or right (if we talk about the degree measure of the angle). In addition quaternions no function or division.

    For Simplified problem, I can say that one quaternion will always look on the axis X (in the inspector is the angle (0, 0, 0)). The second will change. A quaternion is required to be always in the middle between the two quaternions.



    In the image you want me to quaternion (red) is strictly between quaternions A and B in space.
     
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    If you get 2 vectors A,B from those quaternions.

    i.e.

    Code (CSharp):
    1. Vector3 aVector = Vector3.forward * aRotation;
    2. Vector3 bVector = Vector3.forward * bRotation;
    3.  
    4. Vector3 halfVector = (aVector  +bVector).normalized;
    5.  
    6. Quaternion halfRotation = Quaternion.LookRotation(halfVector);
    Not tested, but it's the first thing which came to mind
     
  3. AleVerDes

    AleVerDes

    Joined:
    Jun 16, 2014
    Posts:
    40
    aRotation and bRotation - what is it?
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Your quaternion, but he made a mistakes, the quaternion must be first in the multiplication;

    Code (csharp):
    1. Vector3 a = quaternionA * Vector3.forward;
    Also, his method will lose part of the quaternion data, such as the up.

    Something easier would be;

    Code (csharp):
    1. Quaternion average = Quaternion.Slerp(a, b, 0.5f);
    Might give unexpected result if from and to are total opposite from each other.
     
  5. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    977
    Can't you just do:

    Code (CSharp):
    1. Vector3 half = Vector3.Lerp(vector1, vector2, .5f);
    2.  
    3. Quaternion look = Quaternion.LookRotation(half);