Difference between revisions of "Slerp"

From Second Life Wiki
Jump to navigation Jump to search
(should maaaybe check for division by zero though, for slightly less speed advantage)
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Wikipedia|Slerp}}
'''Slerp''' is shorthand for '''spherical linear interpolation''', introduced by Ken Shoemake in the context of quaternion interpolation for the purpose of animating 3D rotation. It refers to constant speed motion along a unit radius great circle arc, given the ends and an interpolation parameter between 0 and 1.
<dl><dd>
<div id="box">
Slerp is shorthand for spherical linear interpolation, introduced by Ken Shoemake in the context of quaternion interpolation for the purpose of animating 3D rotation. It refers to constant speed motion along a unit radius great circle arc, given the ends and an interpolation parameter between 0 and 1.
</div>
</dl>


<lsl>rotation slerp( rotation a, rotation b, float f ) {
More detail: {{Wikipedia|Slerp}}.
     float angleBetween = llAngleBetween(a, b);
 
     if ( angleBetween > PI )
The following slerp algorithm uses '''a''' and '''b''' for ends and '''t''' for the interpolation parameter.<br/>
         angleBetween = angleBetween - TWO_PI;
To continue the rotation past the ends use a value for '''t''' outside the range '''{{interval|gte=0|lte=1|center=t}}'''.
    return a*llAxisAngle2Rot(llRot2Axis(b/a)*a, angleBetween*f);
 
}//Written by Francis Chung, Taken from http://forums.secondlife.com/showthread.php?p=536622</lsl>
<syntaxhighlight lang="lsl2">
rotation slerp( rotation a, rotation b, float t ) {
  return llAxisAngle2Rot( llRot2Axis(b /= a), t * llRot2Angle(b)) * a;
}//Written collectively, Taken from http://forums-archive.secondlife.com/54/3b/50692/1.html
</syntaxhighlight>
Source: http://forums-archive.secondlife.com/54/3b/50692/1.html
 
A slightly faster, but less concise implementation:
<syntaxhighlight lang="lsl2">
// formula: slerp = (qa*sin((1-t)*theta)+qb*sin(t*theta))/sin(theta)
// where qa, qb are the rotations and theta is half the angle between them
rotation slerp(rotation a, rotation b, float t) {
     float theta = llAngleBetween(a, b)*0.5;
     if(theta) {
        float is = 1/llSin(theta);
         float st = llSin(t*theta)*is;
        float sti = llSin((1-t)*theta)*is;
        return <a.x*sti, a.y*sti, a.z*sti, a.s*sti>+<b.x*st, b.y*st, b.z*st, b.s*st>;
    } else return a;
}
</syntaxhighlight>
 
See also: [[Nlerp]]

Latest revision as of 06:58, 28 March 2024

Slerp is shorthand for spherical linear interpolation, introduced by Ken Shoemake in the context of quaternion interpolation for the purpose of animating 3D rotation. It refers to constant speed motion along a unit radius great circle arc, given the ends and an interpolation parameter between 0 and 1.

More detail: "Wikipedia logo"Slerp.

The following slerp algorithm uses a and b for ends and t for the interpolation parameter.
To continue the rotation past the ends use a value for t outside the range [0, 1].

rotation slerp( rotation a, rotation b, float t ) {
   return llAxisAngle2Rot( llRot2Axis(b /= a), t * llRot2Angle(b)) * a;
}//Written collectively, Taken from http://forums-archive.secondlife.com/54/3b/50692/1.html

Source: http://forums-archive.secondlife.com/54/3b/50692/1.html

A slightly faster, but less concise implementation:

// formula: slerp = (qa*sin((1-t)*theta)+qb*sin(t*theta))/sin(theta)
// where qa, qb are the rotations and theta is half the angle between them
rotation slerp(rotation a, rotation b, float t) {
    float theta = llAngleBetween(a, b)*0.5;
    if(theta) {
        float is = 1/llSin(theta);
        float st = llSin(t*theta)*is;
        float sti = llSin((1-t)*theta)*is;
        return <a.x*sti, a.y*sti, a.z*sti, a.s*sti>+<b.x*st, b.y*st, b.z*st, b.s*st>;
    } else return a;
}

See also: Nlerp