Search Unity

Unity.Mathematics : lerp, multiplying vector3 by a quaternion, quaternion.Inverse, etc...

Discussion in 'Entity Component System' started by PhilSA, Apr 9, 2018.

  1. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    EDIT: Nevermind, it's all in Unity.Mathematics.math. Please delete thread if you want to

    __________________________

    I'm just beginning to mess around with the ECS and I'm wondering where to find common math operations in the new math library. Things like...
    • An equivalent of Vector3.Lerp
    • Multiplying a float3 by a quaternion (it gives me an error)
    • quaternion.Inverse
    • etc...
    Are these purposefully not available, or are they just coming eventually? (or did I just miss them completely?)
     
    Last edited: Jan 17, 2021
    jashan likes this.
  2. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    328
    Most of these have been moved to the "math." library.

    In terms of multiplying a quaternion by a vector:
    Code (CSharp):
    1. math.mul(someQuaternion, someFloat3)
    You were unable to multiply a Vector3 by a Quaternion before, but you CAN multiply a Quaternion by a Vector3

    For lerping
    Code (CSharp):
    1. math.lerp(float3A, float3B, float3W)
    **Edit: Just saw the edit. Glad you found it all :D
     
  3. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    Extreme necrobump, but I just found that:
    math.mul(quaternion.AxisAngle(), float3) 
    does not equal:
    math.mul(Quaternion.AngleAxis(), float3)

    Quaternion's AngleAxis() gives the correct result whereas quaternion's.AxisAngle() gives the wrong result, where both receive the same input. Why? Did I miss something?
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,266
    degrees vs radians
     
    Nyanpas likes this.
  5. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    Oh wow. Oh very wow. I have joined the stupid club of the OP.

    Sorry...
     
  6. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    Welcome to our club. We are all friendly here, and no one needs to be ashamed. Coffee and soft drinks are free and everyone get's their own yellow rubber duck on their first evening.
     
    BigRookGames, apkdev and Hanoke like this.
  7. StuwuStudio

    StuwuStudio

    Joined:
    Feb 4, 2015
    Posts:
    165
    And what's Vector3.Angle's equivalent now?
     
  8. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    Nope.
    Here
    Code (CSharp):
    1.  
    2.         using static math;
    3.         /// <summary>
    4.         /// Returns the angle in radian  between /from/ and /to/. This is always the smallest
    5.         /// </summary>
    6.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
    7.         public static float Radian(float3 from, float3 to)
    8.         {
    9.             // sqrt(a) * sqrt(b) = sqrt(a * b) -- valid for real numbers
    10.             var denominator = sqrt(lengthsq(from) * lengthsq(to));
    11.             if (denominator < UnityEpsilonNormalSqrt) return 0F;
    12.             var d = clamp(dot(from, to) / denominator, -1F, 1F);
    13.             return acos(d);
    14.         }
    15.  
    16.         /// <summary>
    17.         /// Returns the angle in degrees between /from/ and /to/. This is always the smallest
    18.         /// </summary>
    19.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
    20.         public static float Angle(float3 from, float3 to) => degrees(Radian(from, to));
    21.  
    22.         /// <summary>
    23.         // The smaller of the two possible radian between the two vectors is returned, therefore the result will never be greater than 180 degrees or smaller than -180 degrees.
    24.         // If you imagine the from and to vectors as lines on a piece of paper, both originating from the same point, then the /axis/ vector would point up out of the paper.
    25.         // The measured angle between the two vectors would be positive in a clockwise direction and negative in an anti-clockwise direction.
    26.         /// </summary>
    27.         /// <returns></returns>
    28.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
    29.         public static float SignedRadian(in float3 from, in float3 to, in float3 axis)
    30.         {
    31.             var unsignedRadian = Radian(from, to);
    32.             if (unsignedRadian == 0) return 0F;
    33.             var s = sign(dot(cross(from, to), axis));
    34.             return s > 0 ? unsignedRadian : -unsignedRadian;
    35.         }
    36.  
    37.         /// <summary>
    38.         // The smaller of the two possible angles between the two vectors is returned, therefore the result will never be greater than 180 degrees or smaller than -180 degrees.
    39.         // If you imagine the from and to vectors as lines on a piece of paper, both originating from the same point, then the /axis/ vector would point up out of the paper.
    40.         // The measured angle between the two vectors would be positive in a clockwise direction and negative in an anti-clockwise direction.
    41.         /// </summary>
    42.         /// <returns></returns>
    43.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
    44.         public static float SignedAngle(in float3 from, in float3 to, in float3 axis) => degrees(SignedRadian(from, to, axis));
    45.  
     
    Last edited: Oct 17, 2020
    Sarkahn, BigRookGames and Onigiri like this.
  9. nyanpath

    nyanpath

    Joined:
    Feb 9, 2018
    Posts:
    77
  10. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    Yeah, that's a little weird. This is on Unity 2019.4.18, Unity Mathematics 1.2.1.

    Fortunately, in my case, I can do the quaternion.Inverse before going into jobs ... which is better in terms of performance, anyways. But turns out, in 1.2.1, math has it, so it's math.inverse(quaternion). But 1.2.1 also seems kind of old, now ;-)
     
    Nyanpas likes this.
  11. MidnightCow

    MidnightCow

    Joined:
    Jun 2, 2017
    Posts:
    30
    math.lerp for quaternion would be nice!
     
  12. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    You can use
    nlerp
    for normalized linear interpolation between two quaternions and
    slerp
    for
    spherical interpolation between two quaternions
     
  13. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108