User:Dora Gustafson/Harmonic Oscillator motion

From Second Life Wiki
< User:Dora Gustafson
Revision as of 13:49, 22 January 2015 by Dora Gustafson (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Harmonic oscillator motion in 12 Key Frames

Will oscillate a prim along a straight line in space
Only the first half period is computed, the second is the first in reverse
If the prim is moved or scaled the script must be reset to update the motion

// Harmonic oscillator motion by Dora Gustafson, Studio Dora 2012
// will oscillate a prim along a straight line in space
// only the first half period is computed, the second is the first in reverse

float phase=PI;
vector amplitude=< 0.0, 0.0, 2.0>; // amplitude and direction for oscillation
float Hperiode=2.0; // half periode time S
float steps=12; // number of Key Frames for a half periode
float step=0.0;
list KFMlist=[];
vector U;
vector V;
integer osc=TRUE;
vector basePos;

default
{
    state_entry()
    {
        llSetMemoryLimit( llGetUsedMemory()+0x1000);
        llSetPrimitiveParams([PRIM_PHYSICS_SHAPE_TYPE, PRIM_PHYSICS_SHAPE_CONVEX]);
        basePos = llGetPos();
        float dT = Hperiode/steps;
        dT = llRound(45.0*dT)/45.0;
        if ( dT < 0.11111111 ) dT = 0.11111111;
        U = amplitude*llCos( phase);
        while ( step < steps )
        {
            step += 1.0;
            V = amplitude*llCos( PI*step/steps + phase);
            KFMlist += [V-U, dT];
            U = V;
        }
    }
    touch_start( integer n)
    {
        llSetKeyframedMotion( [], []);
        llSleep(0.2);
        llSetRegionPos( basePos);
        llSetPos( basePos);
        if ( osc ) llSetKeyframedMotion( KFMlist, [KFM_DATA, KFM_TRANSLATION, KFM_MODE, KFM_PING_PONG]);
        osc = !osc;
    }
    on_rez( integer n) { llResetScript(); }
}