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

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?)

1 Like

Most of these have been moved to the “math.” library.

In terms of multiplying a quaternion by a vector:

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

math.lerp(float3A, float3B, float3W)

**Edit: Just saw the edit. Glad you found it all :smile:

4 Likes

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?

degrees vs radians

1 Like

Oh wow. Oh very wow. I have joined the stupid club of the OP.

Sorry…

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.

3 Likes

And what’s Vector3.Angle’s equivalent now?

Nope.
Here

        using static math;
        /// <summary>
        /// Returns the angle in radian  between /from/ and /to/. This is always the smallest
        /// </summary>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static float Radian(float3 from, float3 to)
        {
            // sqrt(a) * sqrt(b) = sqrt(a * b) -- valid for real numbers
            var denominator = sqrt(lengthsq(from) * lengthsq(to));
            if (denominator < UnityEpsilonNormalSqrt) return 0F;
            var d = clamp(dot(from, to) / denominator, -1F, 1F);
            return acos(d);
        }

        /// <summary>
        /// Returns the angle in degrees between /from/ and /to/. This is always the smallest
        /// </summary>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static float Angle(float3 from, float3 to) => degrees(Radian(from, to));

        /// <summary>
        // 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.
        // 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.
        // The measured angle between the two vectors would be positive in a clockwise direction and negative in an anti-clockwise direction.
        /// </summary>
        /// <returns></returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static float SignedRadian(in float3 from, in float3 to, in float3 axis)
        {
            var unsignedRadian = Radian(from, to);
            if (unsignedRadian == 0) return 0F;
            var s = sign(dot(cross(from, to), axis));
            return s > 0 ? unsignedRadian : -unsignedRadian;
        }

        /// <summary>
        // 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.
        // 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.
        // The measured angle between the two vectors would be positive in a clockwise direction and negative in an anti-clockwise direction.
        /// </summary>
        /// <returns></returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static float SignedAngle(in float3 from, in float3 to, in float3 axis) => degrees(SignedRadian(from, to, axis));
3 Likes

How about that quaternion.inverse, then…?

[edit] Nevermind, found it here:

Although not in my package.

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 :wink:

1 Like

math.lerp for quaternion would be nice!

1 Like

You can use nlerp for normalized linear interpolation between two quaternions and slerp for
spherical interpolation between two quaternions

4 Likes

fyi Slerp demystified (and AngleAxis and FromToRotation )