Nlerp

From Second Life Wiki
Jump to navigation Jump to search

Nlerp is shorthand for normalized linear interpolation. It is a faster, less mathematically accurate alternative to spherical interpolation for the purpose of animating 3D rotation. It travels the "least twisting" path between the two rotations, but does not move at a constant speed; it will accelerate and decelerate at the start and end, which may be interesting for a visual effect.

The following nlerp algorithm uses a and b for ends and t for the interpolation parameter.
t is in the range [0, 1].

rotation nlerp(rotation a, rotation b, float t) {
    float ti = 1-t;
    rotation r = <a.x*ti, a.y*ti, a.z*ti, a.s*ti>+<b.x*t, b.y*t, b.z*t, b.s*t>;
    float m = 1/llSqrt(r.x*r.x+r.y*r.y+r.z*r.z+r.s*r.s); // normalize
    return <r.x*m, r.y*m, r.z*m, r.s*m>;
}

See also: Slerp