User:Rolig Loon/Elliptical Orbit

From Second Life Wiki
Jump to navigation Jump to search

Features

Moves an object along a smooth, continuous elliptical path.

Script

<lsl> float a; //Semi-minor axis float b; // Semi-major axis vector orig; //Center of ellipse integer count; vector NewPos; integer targetID;

vector calcPos (integer num){

   float t = num * 2 * PI/120;
   vector where = orig + <a * llCos(t), b* llSin(t),0>;
   return where;

}

default {

   on_rez(integer start)
   {
       llResetScript();
   }
   
   state_entry()
   {
       a = 2.0; // Set the semi-minor axis length here
       b = 3.0; // Set the semi-major axis length here
       orig = llGetPos(); 
       llListen(9998,"","","");
   }
   
   listen(integer channel, string name , key id, string msg)
   {
       if (llToLower(msg) == "stop")
       {
           llStopMoveToTarget();
           llTargetRemove(targetID);
           llSetStatus(STATUS_PHYSICS | STATUS_PHANTOM, FALSE);
           llOwnerSay("I am at " + (string)llGetPos() + " in " + llGetRegionName());
       }
       else if (llToLower(msg) == "go")
       {
           if (count == 0)
           {
               NewPos = calcPos(count); 
               llSetPos(NewPos); // Object's first move is from the center of the ellipse to a point on the path
           }
           llSetStatus(STATUS_PHYSICS | STATUS_PHANTOM, TRUE);
           llSetBuoyancy (1.0);
           ++count;
           NewPos = calcPos(count);
           targetID = llTarget(NewPos,0.5);
           llMoveToTarget(NewPos,0.5);
       }
       else
       {
           llOwnerSay("What?");
       }
   }
   at_target(integer tnum, vector targetpos, vector ourpos)
   {
           llStopMoveToTarget();
           llTargetRemove(targetID);
           ++count;
           NewPos = calcPos(count);
           targetID = llTarget(NewPos, 0.5);
           llMoveToTarget(NewPos,0.5);
   }
}

</lsl>