User:Dora Gustafson/point tracker

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

KFM Key framed Motion forward on a closed track

// point tracker script by Dora Gustafson, Studio Dora 2011
// The objct follows a path given by a number of points beginning with the object's position == <0,0,0>
// The path is closed so it ends where it begins
// Points prim's X-axis forward along the path and it's Y-axis is always horizontal
// v1.01 simplified for focus
// v1.02 with 'refFrame' like in vehicles, to change what is forward
// Translation divided in a straight one and one including a rotation
// Object forward can either be horizontal or follow path forward up and down
// v1.02.1 For WIKI. RefFrame inverted. Track is relative to where script is reset

float speed=2.0; // m/S
float straight=0.75; // part to travel without turning. 0.0 <= straight < 1.0
integer horz=FALSE; // TRUE object forward horizontal no matter if the track goes up or down
list track=[<0,0,0>,<4,0,0>,<4,5,2>,<0,5,2>]; // list of points to track; must have at least 2 elements
rotation refFrame=<0.0, 0.0, 0.0, 1.0>;
list KFMlist;
vector basePos;
integer i;
integer j;
integer k;
integer L;
vector U;
vector V;
vector W;
vector LEFT=< 0.0, 1.0, 0.0 >;

rotation Vec2Rot( vector FWD )
{
    FWD = llVecNorm( FWD );
    vector UP = < 0.0, 0.0, 1.0 >;
    if ( llFabs(FWD.z) < 1.0 ) LEFT = llVecNorm(UP%FWD);
    if (horz) FWD = llVecNorm(LEFT%UP);
    else UP = llVecNorm(FWD%LEFT);
    return llAxes2Rot(FWD, LEFT, UP);
}

default
{
    state_entry()
    {
        llSetPrimitiveParams([PRIM_PHYSICS_SHAPE_TYPE, PRIM_PHYSICS_SHAPE_CONVEX]); // make client mesh-aware
        refFrame.s = -refFrame.s;
        basePos = llGetPos();
        L = llGetListLength(track);
        KFMlist = [];
        for ( i=0; i<L; i++)
        {
            j = (i+1) % L;
            V = llList2Vector( track, j)-llList2Vector( track, i);
            U = straight*V;
            KFMlist += [U, ZERO_ROTATION, 0.1+llVecMag(U)/speed];
            k = (j+1) % L;
            W = llList2Vector( track, k)-llList2Vector( track, j);
            KFMlist += [V-U, refFrame*Vec2Rot(W)/(refFrame*Vec2Rot(V)), 0.1+llVecMag(V-U)/speed];
        }
    }
    touch_end(integer n)
    {
        llSetPrimitiveParams([PRIM_POSITION, basePos, PRIM_ROTATION, refFrame*Vec2Rot(llList2Vector( track, 1)-llList2Vector( track, 0))]);
        llSetKeyframedMotion( KFMlist, []);
    }
}