Nlerp

From Second Life Wiki
Revision as of 07:58, 29 October 2023 by Frionil Fang (talk | contribs) (considerably faster than slerp, can be neat for a visual effect since it isn't constant speed, AND is a valid use for rotation addition)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 = 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