Understanding of spherical linear interpolation

【Preface】

Linear interpolation is the uniform change of the distance between point A and point B, and spherical linear interpolation is the uniform change of the angle between point A and point B. The former has a wide range of applications, and the latter mainly applies smooth rotation

[Spherical linear interpolation solution]

Spherical linear interpolation (Slerp) can also be written as follows:

Y = a(t)Y0 + b(t)Y1 

What needs to be solved is a(t), b(t)

Take Vector3 as an example: At this point, the point is regarded as a vector, and there are already two vectors A and B (the direction and length of the vector are known), and the direction and length of the new vector C are found, where the angle formed by C and A changes linearly of.

Find the direction first : first unitize the vector

 In the figure, v0 and v1 represent AB vectors, v2 is perpendicular to v0, r is the direction of the vector to be obtained, and θ is the rotation angle of r, which can be obtained as follows:

r = v0cosθ + v2sinθ.

Here only v2 is unknown, let the projection vector of v1 on v0 be v3, then v3 = (v1*v0)*v0, v2 = Normalize(v1 - (v0 v1)v0)

Then find the length:

The length of the vector obtained above is 1, what is the length of the new vector? You can set it according to your own needs. There are two commonly used methods

One is: the point where the vector C is located is on the straight line where AB is located

The second is: the length of the vector C is the linear interpolation of the lengths of the vectors A and B

The latter is Vector3.Slerp in Unity

The code to find the length using the latter is:

Vector3 Slerp(Vector3 start, Vector3 end, float percent)
    {
        Vector3 startNormal = start.normalized;
        Vector3 endNormal = end.normalized;
        float dot = Vector3.Dot(startNormal, endNormal);
        Mathf.Clamp(dot, -1.0f, 1.0f);
        float theta = Mathf.Acos(dot) * percent;
        Vector3 RelativeVec = endNormal - startNormal * dot;
        RelativeVec.Normalize();
        Vector3 result = (startNormal * Mathf.Cos(theta) + RelativeVec * Mathf.Sin(theta));
        float length = start.magnitude * (1- percent) + end.magnitude *  percent;
        return length * result;
    }

 [Spherical linear interpolation of quaternions]

We know that quaternions can also represent directions, that is, rotation angles. It is also possible to use quaternions for spherical linear interpolation, so that the angle changes uniformly. The result is:

Push to process see link.

【Nlerp】

The calculation of Vector3.Slerp involves trigonometric functions and finding the length, which takes a long time. When the length of the vector is 1, an approximate method can be used to realize spherical linear interpolation. This is Nlerp, which is multi-application and animation.

Vector3 NLerp(Vector3 start, Vector3 end, float percent)
    {
        return Vector3.Lerp(start, end, percent).normalized;
    }

【reference】

Math Magician – Lerp, Slerp, and Nlerp – Keith M. Programming

Spherical linear interpolation (slerp) of quaternions bzdww

https://www.cnblogs.com/21207-iHome/p/6952004.html

Guess you like

Origin blog.csdn.net/enternalstar/article/details/131085237