SHARE
TWEET

fancy door

a guest Sep 7th, 2019 119 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. list agents = [];
  2.  
  3. float closingStartAngle;
  4. float angle;
  5. vector hinge;
  6. vector ref_pos;
  7.  
  8.  
  9. float distance_from_hinge(vector aPos)
  10. {
  11.     return llVecDist(<hinge.x, hinge.y, 0>, <aPos.x, aPos.y, 0>);
  12. }
  13.  
  14. //https://stackoverflow.com/questions/1211212/how-to-calculate-an-angle-from-three-points
  15. float getAngle(float centreX, float centreY, float pointX, float pointY)
  16. {
  17.     float x = pointX - centreX;
  18.     float y = pointY - centreY;
  19.  
  20.     float magnitude = llSqrt(x * x + y * y);
  21.     float angle;
  22.     if (magnitude > 0)
  23.         angle = llAcos(x / magnitude);
  24.        
  25.     angle = angle * 180 / PI;
  26.     if (y < 0)
  27.         angle = 360 - angle;
  28.     return angle;
  29. }
  30.  
  31. vector get_hinge()
  32. {
  33.     vector scale = llGetScale();
  34.     return <ref_pos.x - (scale.x/2), ref_pos.y + (scale.y/2),  ref_pos.z>;
  35. }
  36.  
  37.  
  38. vector rotate_around_axis(vector axis, vector position, rotation r)
  39. {
  40.     // Calculate position's offset from axis.
  41.     vector offset = position - axis;
  42.     // Rotate the offset
  43.     vector rotOffset = offset * r;
  44.  
  45.     return axis + rotOffset;
  46. }
  47.  
  48. float sine_easeInOut2(float time, float begin, float cend, float duration)
  49. {
  50.     cend = cend - begin;
  51.     return -cend/2 * (llCos(PI * time / duration) - 1) + begin;
  52. }
  53.  
  54.  
  55. default
  56. {
  57.     state_entry()
  58.     {
  59.         ref_pos = llGetPos();
  60.         hinge = get_hinge();
  61.        
  62.         state idle;
  63.     }
  64. }
  65.  
  66. state idle
  67. {
  68.     state_entry()
  69.     {
  70.         agents = [];
  71.         llVolumeDetect(TRUE);
  72.         llSetText("Idle", <1,1,1>, 0.5);
  73.         angle = 90;
  74.     }
  75.  
  76.     collision_start(integer num_detected)
  77.     {
  78.         integer i;
  79.         for (i = 0; i < num_detected; i++)
  80.         {
  81.             if (llDetectedType(i) & AGENT)
  82.             {  
  83.                 agents += [llDetectedKey(i)];
  84.             }
  85.         }
  86.         if (llGetListLength(agents) > 0)
  87.         {
  88.             state open;
  89.         }
  90.     }
  91. }
  92.  
  93. state open
  94. {
  95.     state_entry()
  96.     {
  97.         llSetTimerEvent(2 / llGetRegionFPS());
  98.         llSensorRepeat("", NULL_KEY, AGENT_BY_LEGACY_NAME, 2.5, PI, 0.1);
  99.        
  100.  
  101.     }
  102.  
  103.     no_sensor()
  104.     {
  105.         llSetTimerEvent(FALSE);
  106.         state closing;
  107.     }
  108.  
  109.     timer()
  110.     {
  111.         integer i;
  112.         integer numAgents = llGetListLength(agents);
  113.         integer agentsInRange = 0;
  114.         for (i = 0; i < numAgents; i++)
  115.         {
  116.             key agent = llList2Key(agents, i);
  117.             vector agentPos = llList2Vector(llGetObjectDetails(agent, [OBJECT_POS]), 0);
  118.             if (distance_from_hinge(agentPos) < 1.25)
  119.             {
  120.                 agentsInRange++;
  121.                 float thisAngle = getAngle(hinge.x, hinge.y, agentPos.x, agentPos.y) + 15;
  122.  
  123.                 if (thisAngle > 90 && thisAngle < 180)
  124.                 {
  125.  
  126.                     if(numAgents == 1 || thisAngle > angle) angle = thisAngle;
  127.                 }
  128.             }
  129.         }
  130.         if (agentsInRange > 0)
  131.         {
  132.             llSetText("Open " + (string) angle, <1,1,1>, 0.5);
  133.             rotation r = llAxisAngle2Rot(<0,0,1>, angle * DEG_TO_RAD);
  134.             vector   p = rotate_around_axis(hinge, ref_pos, r);
  135.             llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_ROTATION, r, PRIM_POSITION, p]);
  136.         } else {
  137.             state closing;
  138.         }
  139.     }
  140. }
  141.  
  142. state closing
  143. {
  144.     state_entry()
  145.     {
  146.         agents = [];
  147.         closingStartAngle = angle;
  148.         llSetTimerEvent(1 / llGetRegionFPS());
  149.         llResetTime();
  150.         llSetText("Closing", <1,1,1>, 0.5);
  151.     }
  152.  
  153.     collision_start(integer num_detected)
  154.     {
  155.         integer i;
  156.         for (i = 0; i < num_detected; i++)
  157.         {
  158.             if (llDetectedType(i) & AGENT)
  159.             {  
  160.                 agents += [llDetectedKey(i)];
  161.             }
  162.         }
  163.         if (llGetListLength(agents) > 0)
  164.         {
  165.             state open;
  166.         }
  167.     }
  168.  
  169.     timer()
  170.     {
  171.         float elapsed_time = llGetTime();
  172.  
  173. // float sine_easeInOut(float time, float begin, float cend, float duration)
  174. // {
  175. //     return -cend/2 * (llCos(PI * time / duration) - 1) + begin;
  176. // }
  177.  
  178.         if (elapsed_time > 3.0)
  179.         {
  180.             llSetTimerEvent(FALSE);
  181.             angle = 90;
  182.             rotation r = llAxisAngle2Rot(<0,0,1>, angle * DEG_TO_RAD);
  183.             vector   p = rotate_around_axis(hinge, ref_pos, r);
  184.             llSetLinkPrimitiveParams(LINK_THIS, [PRIM_ROTATION, r, PRIM_POSITION, p]);
  185.             state idle;
  186.  
  187.         }
  188.         if (elapsed_time < 3.0)
  189.         {
  190.             angle = sine_easeInOut2(elapsed_time, closingStartAngle, 90, 3.0);
  191.             llSetText("Closing " + (string) angle, <1,1,1>, 0.5);
  192.             rotation r = llAxisAngle2Rot(<0,0,1>, angle * DEG_TO_RAD);
  193.             vector   p = rotate_around_axis(hinge, ref_pos, r);
  194.             llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_ROTATION, r, PRIM_POSITION, p]);
  195.         }
  196.     }
  197. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top