Difference between revisions of "Interpolation"
(Added new section. New function 'rCosAim', does an rCos animation loop, includes speeds setting) |
m (Maybe this makes more sense. Hmm.) |
||
Line 283: | Line 283: | ||
<lsl> | <lsl> | ||
vector vCub(vector v0,vector v1,vector v2,vector v3,float t){ | vector vCub(vector v0,vector v1,vector v2,vector v3,float t){ | ||
vector P = (v3-v2)-(v0 | vector P = (v3-v2)-(v1-v0);vector Q = (v1-v0)-P;vector R = v2-v1;vector S = v0; | ||
return P*llPow(t,3) + Q*llPow(t,2) + R*t + S;} | return P*llPow(t,3) + Q*llPow(t,2) + R*t + S;} | ||
</lsl> | </lsl> | ||
Line 294: | Line 294: | ||
|- | |- | ||
| vector v0 | | vector v0 | ||
| | | Start Point | ||
|- | |- | ||
| vector v1 | | vector v1 | ||
| Start | | Start Tangent | ||
|- | |- | ||
| vector v2 | | vector v2 | ||
| End | | End Point | ||
|- | |- | ||
| vector v3 | | vector v3 | ||
| | | End Tangent | ||
|- | |- | ||
| float t | | float t |
Revision as of 02:21, 3 February 2010
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
Interpolation Library
Float Functions
Float Linear | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Linear interpolation of f0 and f1 with fraction t. <lsl> float fLin(float f0, float f1,float t){ return f0*(1-t) + f1*t;} </lsl>
By Nexii Malthus
|
Float Cosine | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Cosine interpolation of f0 and f1 with fraction t. <lsl> float fCos(float v0,float v1,float t){ float F = (1 - llCos(t*PI))/2; return v0*(1-F)+v1*F;} </lsl>
By Nexii Malthus
|
Float Cubic | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Cubic interpolation of f0, f1, f2 and f3 with fraction t. <lsl> float fCub(float f0,float f1,float f2,float f3,float t){ float P = (f3-f2)-(f0-f1);float Q = (f0-f1)-P;float R = f2-f0;float S = f1; return P*llPow(t,3) + Q*llPow(t,2) + R*t + S;} </lsl>
By Nexii Malthus
|
Float Hermite | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Hermite interpolation of f0, f1, f2 and f3 with fraction t, tension and bias. <lsl> float fHem(float f0,float f1,float f2,float f3,float t,float tens,float bias){ float t2 = t*t;float t3 = t2*t; float a0 = (f1-f0)*(1+bias)*(1-tens)/2; a0 += (f2-f1)*(1-bias)*(1-tens)/2; float a1 = (f2-f1)*(1+bias)*(1-tens)/2; a1 += (f3-f2)*(1-bias)*(1-tens)/2; float b0 = 2*t3 - 3*t2 + 1; float b1 = t3 - 2*t2 + t; float b2 = t3 - t2; float b3 = -2*t3 + 3*t2; return ( b0 * f1+b1 * a0+b2 * a1+b3 * f2 );} </lsl>
By Nexii Malthus
|
Vector Functions
Vector Linear | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Linear interpolation of v0 and v1 with fraction t. <lsl> vector vLin(vector v0, vector v1,float t){ return v0*(1-t) + v1*t;} </lsl>
By Nexii Malthus
|
Vector Cosine | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Cosine interpolation of v0 and v1 with fraction t. <lsl> vector vCos(vector v0,vector v1,float t){ float F = (1 - llCos(t*PI))/2; return v0*(1-F)+v1*F;} </lsl>
By Nexii Malthus
|
Vector Cubic | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Cubic interpolation of v0, v1, v2 and v3 with fraction t. <lsl> vector vCub(vector v0,vector v1,vector v2,vector v3,float t){ vector P = (v3-v2)-(v1-v0);vector Q = (v1-v0)-P;vector R = v2-v1;vector S = v0; return P*llPow(t,3) + Q*llPow(t,2) + R*t + S;} </lsl>
By Nexii Malthus
|
Vector Hermite | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Hermite interpolation of v0, v1, v2 and v3 with fraction t, tension and bias. <lsl> vector vHem(vector v0,vector v1,vector v2,vector v3,float t,float tens,float bias){ float t2 = t*t;float t3 = t2*t; vector a0 = (v1-v0)*(1+bias)*(1-tens)/2; a0 += (v2-v1)*(1-bias)*(1-tens)/2; vector a1 = (v2-v1)*(1+bias)*(1-tens)/2; a1 += (v3-v2)*(1-bias)*(1-tens)/2; float b0 = 2*t3 - 3*t2 + 1; float b1 = t3 - 2*t2 + t; float b2 = t3 - t2; float b3 = -2*t3 + 3*t2; return ( b0 * v1+b1 * a0+b2 * a1+b3 * v2 );} </lsl>
By Nexii Malthus
|
Rotation Functions
Rotation Linear | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Spherical Linear interpolation of r0 and r1 with fraction t. Also known as SLERP <lsl> rotation rLin(rotation r0,rotation r1,float t){ // Spherical-Linear Interpolation float ang = llAngleBetween(r0, r1); if( ang > PI) ang -= TWO_PI; return r0 * llAxisAngle2Rot( llRot2Axis(r1/r0)*r0, ang*t);} </lsl>
By Nexii Malthus
|
Rotation Cosine | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Spherical Cosine interpolation of r0 and r1 with fraction t. I liken to call it as SCORP <lsl> rotation rCos(rotation r0,rotation r1,float t){ // Spherical-Cosine Interpolation float f = (1 - llCos(t*PI))/2; float ang = llAngleBetween(r0, r1); if( ang > PI) ang -= TWO_PI; return r0 * llAxisAngle2Rot( llRot2Axis(r1/r0)*r0, ang*f);} </lsl>
By Nexii Malthus
|
Rotation Cubic | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Spherical Cubic interpolation of r0 and r1 with fraction t. I liken to call it as SCURP <lsl> rotation rCub(rotation r0,rotation r1,rotation r2,rotation r3,float t){ // Spherical-Cubic Interpolation // r0 = Start, r1 = End, r2 and r3 affect path of curve! return rLin( rLin(r0,r1,t), rLin(r2,r3,t), 2*t*(1-t) );} </lsl>
By Nexii Malthus
|
Speed Controlled
Rotation Cosine Aim | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Spherical Cosine interpolation of r0 and r1 with speed regulation. Does the entire animation loop to rotate between r0 to r1 with a specific speed, with the cosine interpolation it makes it appear to accelerate and deccelerate realistically. <lsl> rCosAim( rotation r0, rotation r1, float speed ){ float ang = llAngleBetween(r0, r1) * RAD_TO_DEG; if( ang > PI) ang -= TWO_PI; float x; float y = (ang/speed)/0.2; for( x = 0.0; x < y; x += 1.0 ) llSetRot( rCos( r0, r1, x/y ) ); } </lsl>
By Nexii Malthus
|
Old non-documented Library
Changes/ 1.0-1.1 - Added rotation types 1.1-1.2 - Added Hermite for float and vector
<lsl> //===================================================// // Interpolation Library 1.2 // // "May 12 2008", "6:16:20 GMT-0" // // Copyright (C) 2008, Nexii Malthus (cc-by) // // http://creativecommons.org/licenses/by/3.0/ // //===================================================//
float fLin(float v0, float v1,float t){
return v0*(1-t) + v1*t;}
float fCos(float v0,float v1,float t){
float F = (1 - llCos(t*PI))/2; return v0*(1-F)+v1*F;}
float fCub(float v0,float v1,float v2,float v3,float t){
float P = (v3-v2)-(v0-v1);float Q = (v0-v1)-P;float R = v2-v0;float S = v1; return P*llPow(t,3) + Q*llPow(t,2) + R*t + S;}
float fHem(float v0,float v1,float v2,float v3,float t,float tens,float bias){
float t2 = t*t;float t3 = t2*t; float a0 = (v1-v0)*(1+bias)*(1-tens)/2; a0 += (v2-v1)*(1-bias)*(1-tens)/2; float a1 = (v2-v1)*(1+bias)*(1-tens)/2; a1 += (v3-v2)*(1-bias)*(1-tens)/2; float b0 = 2*t3 - 3*t2 + 1; float b1 = t3 - 2*t2 + t; float b2 = t3 - t2; float b3 = -2*t3 + 3*t2; return ( b0 * v1+b1 * a0+b2 * a1+b3 * v2 );}
vector vLin(vector v0, vector v1,float t){
return v0*(1-t) + v1*t;}
vector vCos(vector v0,vector v1,float t){
float F = (1 - llCos(t*PI))/2; return v0*(1-F)+v1*F;}
vector vCub(vector v0,vector v1,vector v2,vector v3,float t){
vector P = (v3-v2)-(v0-v1);vector Q = (v0-v1)-P;vector R = v2-v0;vector S = v1; return P*llPow(t,3) + Q*llPow(t,2) + R*t + S;}
vector vHem(vector v0,vector v1,vector v2,vector v3,float t,float tens,float bias){
float t2 = t*t;float t3 = t2*t; vector a0 = (v1-v0)*(1+bias)*(1-tens)/2; a0 += (v2-v1)*(1-bias)*(1-tens)/2; vector a1 = (v2-v1)*(1+bias)*(1-tens)/2; a1 += (v3-v2)*(1-bias)*(1-tens)/2; float b0 = 2*t3 - 3*t2 + 1; float b1 = t3 - 2*t2 + t; float b2 = t3 - t2; float b3 = -2*t3 + 3*t2; return ( b0 * v1+b1 * a0+b2 * a1+b3 * v2 );}
rotation rLin(rotation r0,rotation r1,float t){
// Spherical-Linear Interpolation float ang = llAngleBetween(r0, r1); if( ang > PI) ang -= TWO_PI; return r0 * llAxisAngle2Rot( llRot2Axis(r1/r0)*r0, ang*t);}
rotation rCos(rotation r0,rotation r1,float t){
// Spherical-Cosine Interpolation float f = (1 - llCos(t*PI))/2; float ang = llAngleBetween(r0, r1); if( ang > PI) ang -= TWO_PI; return r0 * llAxisAngle2Rot( llRot2Axis(r1/r0)*r0, ang*f);}
rotation rCub(rotation r0,rotation r1,rotation r2,rotation r3,float t){
// Spherical-Cubic Interpolation // r0 = Start, r1 = End, r2 and r3 affect path of curve! return rLin( rLin(r0,r1,t), rLin(r2,r3,t), 2*t*(1-t) );}
default{state_entry(){}}
</lsl>