Saturday, September 13, 2014

Use Scratch's Move Functions in Your Own Script (Level III)

A function in LSL is essentially a series of directions specified by the user and given a name. Functions are placed at the beginning of the script and can be easily called when needed.


The movement functions in the Scratch program can turn and move a prim easily. All you do is call a function and tell it how far to move.


Below the fold you'll find the script with the various Scratch functions. Your prim will move in a variety of ways as it illustrates what the functions do.

//===============

// Cut and paste code from next line to end

// The code below is adapted from code generated by
// SCRATCH FOR SECOND LIFE (S4SL)
// alpha release October 19, 2007
// by Eric Rosenbaum (ericr@media.mit.edu)
// MIT Media Lab
// Lifelong Kindergarten group

//These are Scratch's internal variables. They're not all needed for movement, but you might as well list them all in case you wish to use resize commands and turn to face in the future.
//Place the variables at the beginning of the script box, before the functions that follow. The script itself begins when all the functions have been defined.

// INTERNAL VARIABLES

vector home;
float color;
vector originalScale;
float sizePercent;
integer numAvatarsNearby;
vector nearestAvPosition;
key ownerKey;
vector ownerPosition;

// These are the movement functions

// levelOut()

// removes the x and y rotation components, so the object is

// level with respect to the ground

levelOut()

{

vector myVec = llRot2Euler(llGetRot());

vector newVec = <0, 0, myVec.z>;

rotation newRot = llEuler2Rot(newVec);

llSetRot(newRot);

}

//...

// goHome()

// move the object back to its home position

// home is set the the position of the object when it is created,

// and can be set to a new position using setHomeHere()

// effective to 10 meters. For longer distances use go(home) several times or, better yet, use a long distance teleport script like WarpPos

goHome()

{

llSetPos(home);

//levelOut();

}


//...

// setHomeHere()

// set the home position to the current position

// you can set home whenever you wish-- when the object with the script inside rezzes, for instance, or when the script starts, or when the object moves to a new position.

setHomeHere()

{

home = llGetPos();

}


//...

// move(steps)

// move object a number of steps (meters) along its current heading

// forward is along the positive x axis

move(float steps)

{

vector fwd = llRot2Fwd(llGetRot()) * steps;

llSetPos(llGetPos() + fwd);

}

//...

// turnRight(float angle)

// turn angle degrees clockwise around the local z axis

turnRight(float angle)

{

angle *= -1;

rotation newRot = llEuler2Rot(<0,0,angle> * DEG_TO_RAD);

llSetRot(newRot*llGetRot());

}

//...

// turnLeft(float angle)

// turn angle degrees counterclockwise around the local z axis

turnLeft(float angle)

{

rotation newRot = llEuler2Rot(<0,0,angle> * DEG_TO_RAD);

llSetRot(newRot*llGetRot());

}

//...

//up(float steps)

//move up along the global z axis by steps meters

up(float steps)

{

llSetPos(llGetPos()+<0,0,steps>);

}

//...

//down(float steps)

//move down along the global z axis by steps meters

down(float steps)

{

llSetPos(llGetPos()+<0,0,-1*steps>);

}

//...

// turnPitch(float angle)

// turn angle degrees upward around the local y axis

turnPitch(float angle)

{

angle *= -1;

rotation newRot = llEuler2Rot(<0,angle,0> * DEG_TO_RAD);

llSetRot(newRot*llGetRot());

}

//...

// turnRoll(float angle)

// turn angle degrees clockwise around the local x axis

turnRoll(float angle)

{

rotation newRot = llEuler2Rot(<angle,0,0> * DEG_TO_RAD);

llSetRot(newRot*llGetRot());

}

//...

// Script starts here

default
{

     state_entry()
     {
          setHomeHere();
// sets home postion when script starts

    }

    touch_start(integer total_number)
    {
         llInstantMessage(llDetectedKey(0), "Start Sequence");
         llSleep(1);
         llInstantMessage(llDetectedKey(0), "Move forward 1 meter");
        move (1);
        llSleep(2); // two second pause
        llInstantMessage(llDetectedKey(0), "Turn right 90 degrees, then move forward 1 meter");
        turnRight(45);
        turnRight(45);
        move (1);
        llSleep(2);
        llInstantMessage(llDetectedKey(0), "Repeat");
        turnRight(45);
        turnRight(45);
        move (1);
        llSleep(2);
        llInstantMessage(llDetectedKey(0), "Repeat");
        turnRight(45);
        turnRight(45);
        move(1);
        llSleep(2);
        llInstantMessage(llDetectedKey(0), "Turn Right 90 degrees");
        turnRight(45);
        turnRight(45);
        llSleep(2);
        llInstantMessage(llDetectedKey(0), "Up 2 meters");
        up(2);
        llSleep(2);
        llInstantMessage(llDetectedKey(0), "Down 1 meter");
        down(1);
        llSleep(2);
        llInstantMessage(llDetectedKey(0), "Turn right 45 degrees");
        turnRight(45);
        llSleep(2);
        llInstantMessage(llDetectedKey(0), "Turn Left 90 degrees");
        turnLeft(90);
        llSleep(2);
        llInstantMessage(llDetectedKey(0), "Turn Right 45 degrees");
        turnRight(45);
        llSleep(2);
        llInstantMessage(llDetectedKey(0), "Roll 45 degrees");
        turnRoll(45);
        llSleep(2);
        llInstantMessage(llDetectedKey(0), "Roll - 90 degrees");
        turnRoll(-90);
        llSleep(2);
        llInstantMessage(llDetectedKey(0), "Roll 45 degrees");
        turnRoll(45);
        llSleep(2);
         llInstantMessage(llDetectedKey(0), "Pitch 45 degrees");
        turnPitch(45);
        llSleep(2);
         llInstantMessage(llDetectedKey(0), "Pitch - 90 degrees");
        turnPitch(-90);
        llSleep(2);
         llInstantMessage(llDetectedKey(0), "Pitch 45 degrees");
        turnPitch(45);
        llSleep(2);
        down(1);
         llInstantMessage(llDetectedKey(0), "Down 1 meter to home position");
        llSleep(2);
         llInstantMessage(llDetectedKey(0), "Up 3 meters and move 1 meter forward");
        up(3);
        move(1);
        llSleep(2);
         llInstantMessage(llDetectedKey(0), "Go Home");
        goHome();
        llSleep(1);
         llInstantMessage(llDetectedKey(0), "End sequence");
        llResetScript();
    }
}

//==============

No comments:

Post a Comment