Difference between revisions of "Smooth Sliding Door"

From Second Life Wiki
Jump to navigation Jump to search
Line 21: Line 21:
// Licensed under the "OpenCollar License"
// Licensed under the "OpenCollar License"
// Ie. Licensed under the GPLv2, with the additional requirement that these scripts remain "full perms" in Second Life.
// Ie. Licensed under the GPLv2, with the additional requirement that these scripts remain "full perms" in Second Life.
//
// Edit: Simplified the script to be just a basic move script .. removed the sound triggers (5/1/2010)
//
//
// ********************************************************************
// ********************************************************************
Line 30: Line 32:


// Movement Constants
// Movement Constants
vector      OFFSET = <-2.0, 0.0, 0.0>;      // Directional offset for moving the door
vector      OFFSET = <-2.0, 0.0, 0.0>;      // Directional offset for moving the door in x,y,z coordinates
float      OPENTIME = 3.5;                // Time taken to open door
float      OPENTIME = 3.5;                // Time taken to open door
float      CLOSETIME = 3.5;                // Time taken to close door
float      CLOSETIME = 3.5;                // Time taken to close door
// Sound/volume to play on open and close, either a UUID or a name in the door contents
string      OPENSOUND = "";
float      OPENVOL = 1.0;
string      CLOSESOUND = "";
float      CLOSEVOL = 1.0;
// Misc. Constants
float      MAX_DISTANCE = 10.00;          // Maximum distance the touching avatar can be away




Line 66: Line 58:
     if(!bOpen)
     if(!bOpen)
     {  // Initial conditions
     {  // Initial conditions
        bOpen = TRUE;               
         rRot = llGetRot();
         rRot = llGetRot();
         vPosition = llGetPos();
         vPosition = llGetPos();
Line 73: Line 66:
         vTargetPos = vPosition+OFFSET*rRot;
         vTargetPos = vPosition+OFFSET*rRot;
          
          
         // Set the timer to cleanup position .. nesessary for laggy sims
         // Set the timer to cleanup position
         llSetTimerEvent(OPENTIME);
         llSetTimerEvent(OPENTIME);      
       
        bOpen = TRUE;               
        if(OPENSOUND != "") llTriggerSound(OPENSOUND, OPENVOL);
     }else
     }else
     {         
     {         
        bOpen = FALSE;
         // Target Position
         // Target Position
         omega=CLOSETIME/llVecDist(<0,0,0>,OFFSET);
         omega=CLOSETIME/llVecDist(<0,0,0>,OFFSET);
         vTargetPos = vPosition;
         vTargetPos = vPosition;
          
          
         // Set the timer to cleanup position .. nesessary for laggy sims
         // Set the timer to cleanup position
         llSetTimerEvent(CLOSETIME);
         llSetTimerEvent(CLOSETIME);
        bOpen = FALSE;
        if(CLOSESOUND != "") llTriggerSound(CLOSESOUND, CLOSEVOL);                     
     }
     }
      
      
Line 109: Line 98:
     touch_start(integer num_detected)
     touch_start(integer num_detected)
     {
     {
         key        keyTouchingAvatar = llDetectedKey(0);
         MoveDoor();
        float      distance = llVecDist(llGetPos(),llDetectedPos(0));
 
        if (distance < MAX_DISTANCE && !bMoving) MoveDoor();
     }  
     }  
          
          
Line 125: Line 111:
     }
     }
}</lsl>
}</lsl>


==See also==
==See also==
*[[ Script_Library | Script Library ]]
*[[ Script_Library | Script Library ]]

Revision as of 00:37, 6 January 2010

Smooth Sliding Door Script

Introduction

It really annoys me that there are so many bad looking doors out there that jerk around. While you can use llSetPos for a sliding door you have no control at what speed it moves. This sliding door script i created because i wanted to time my sliding door movements with the door sounds i had. It uses llMoveToTarget to move the door, with the door temporally turning phantom as it moves.

In laggy sims it will behave a little bit funky .. the door sometimes seeming to zoom off to the side before popping into the correct final position as the door becomes a physical object. However, even with that defect it still looks much better IMHO

The Script

<lsl> // ******************************************************************** // // Basic Physical Sliding Door Script // by SimonT Quinnell // 11/07/2009 // // NOTE: If you are going to reposition the door, do it while the door is closed. // Otherwise it will try and use the old close position as a reference point. // // Licensed under the "OpenCollar License" // Ie. Licensed under the GPLv2, with the additional requirement that these scripts remain "full perms" in Second Life. // // Edit: Simplified the script to be just a basic move script .. removed the sound triggers (5/1/2010) // // ********************************************************************


// ******************************************************************** // CONSTANTS // ********************************************************************

// Movement Constants vector OFFSET = <-2.0, 0.0, 0.0>; // Directional offset for moving the door in x,y,z coordinates float OPENTIME = 3.5; // Time taken to open door float CLOSETIME = 3.5; // Time taken to close door


// ******************************************************************** // Variables // ********************************************************************

vector vPosition; rotation rRot; float omega=0.0; vector vTargetPos; integer bOpen = FALSE; integer bMoving = FALSE;


// ******************************************************************** // Functions // ********************************************************************


MoveDoor() {

   if(!bOpen)
   {   // Initial conditions
       bOpen = TRUE;                
       rRot = llGetRot();
       vPosition = llGetPos();
       
       // Target Position
       omega=OPENTIME/llVecDist(<0,0,0>,OFFSET);
       vTargetPos = vPosition+OFFSET*rRot;
       
       // Set the timer to cleanup position
       llSetTimerEvent(OPENTIME);        
   }else
   {        
       bOpen = FALSE;
       // Target Position
       omega=CLOSETIME/llVecDist(<0,0,0>,OFFSET);
       vTargetPos = vPosition;
       
       // Set the timer to cleanup position
       llSetTimerEvent(CLOSETIME);
   }
   
   // Set Door Physical and move it
   bMoving = TRUE;
   llSetStatus(STATUS_PHANTOM, TRUE);
   llSetStatus(STATUS_PHYSICS, TRUE);
   llMoveToTarget(vTargetPos,omega);

}


default {

   state_entry()
   {   // Initial conditions
       rRot = llGetRot();
       vPosition = llGetPos();
   }    
       
   touch_start(integer num_detected)
   {
       MoveDoor();
   } 
       
   timer()
   {
       // Clean up Position
       bMoving = FALSE;
       llSetTimerEvent(0.0);
       llSetStatus(STATUS_PHYSICS, FALSE);
       llSetStatus(STATUS_PHANTOM, FALSE);         
       llSetPrimitiveParams([ PRIM_POSITION, vTargetPos, PRIM_ROTATION, rRot ]);        
   }

}</lsl>

See also