User:Dora Gustafson/point tracker

From Second Life Wiki
< User:Dora Gustafson
Revision as of 10:08, 17 December 2011 by Dora Gustafson (talk | contribs) (Move object on a closed track given by points)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Make an object move forward on a closed track given by points

<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;

rotation Vec2Rot( vector V ) {

   V = llVecNorm( V );
   if ( V.z == 1.0 ) return < 0.0, -.7071068, 0.0, .7071068 >;
   if ( V.z == -1.0 ) return < 0.0, .7071068, 0.0, .7071068 >;
   vector UP = < 0.0, 0.0, 1.0 >;
   vector LEFT = llVecNorm(UP%V);
   if (horz) V = llVecNorm(LEFT%UP);
   else UP = llVecNorm(V%LEFT);
   return llAxes2Rot(V, 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>