User:Dora Gustafson/point tracker

From Second Life Wiki
Jump to navigation Jump to search

KFM Key framed Motion forward on a closed track

<lsl> // 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

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; integer i; integer j; integer k; integer L; vector U; vector V; vector W; vector basePos; rotation baseRot; 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()
   {
       llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_PHYSICS_SHAPE_TYPE, PRIM_PHYSICS_SHAPE_CONVEX]); // make client mesh-aware
       basePos=llGetPos();
       baseRot=llGetRot();
       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)
   {
       llSetKeyframedMotion( [], [KFM_COMMAND, KFM_CMD_STOP]);
       V = basePos-llGetPos();
       llSetKeyframedMotion( [ V, refFrame*baseRot/llGetRot(), 0.1+llVecMag(V)/speed]+KFMlist, []);
   }

} </lsl>