PhysicsLib

From Second Life Wiki
Revision as of 03:02, 6 May 2009 by Nexii Malthus (talk | contribs) (New page: {{LSL Header}} <lsl> //===================================================// // Physics Library 0.2 // // "May 6 2009", "11:00:00 GMT-0" /...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

<lsl> //===================================================// // Physics Library 0.2 // // "May 6 2009", "11:00:00 GMT-0" // // Copyright (C) 2009, Nexii Malthus (cc-by) // // http://creativecommons.org/licenses/by/3.0/ // //===================================================//

// TRAJECTORY // float pTimeOfFlight(float Dist, float Angle, float Height, float Vel, float Gravity ){// UNTESTED!

   float cos = llCos(Angle); float sin = llSin(Angle);
   return ( Dist / Vel*cos ) * ( ( llPow(Vel*sin,2) + 2 * Gravity * Height ) / Gravity);}

rotation pAngleOfReach(vector cPos,vector tPos,float V,float G,integer cAng){

   float Theta;float D = llVecDist(cPos,tPos);float D2 = D*D;
   float V2 = V*V;float V4 = V*V*V*V;
   float X = llVecDist(<cPos.x,cPos.y,0>,<tPos.x,tPos.y,0>);float X2 = X*X;
   float Y = 0.0;
   
   float Thingy = V4 - ( G * (G*X2 + 2*Y*V2 ));
   if(Thingy <= 0){
       //llSetText("Over max distance!",<1,1,1>,1.0);
       return ZERO_ROTATION;}
   
   float Sqrt = llSqrt(Thingy);
   float x;// = V2 - Sqrt;//llSqrt(V4 - G * (G*X2 + 2*Y*V2 ) );
   if( cAng ) x = V2 + Sqrt;
   else x = V2 - Sqrt;
   float y = G*X;
    Theta = llAtan2(x,y);
   return <0, llSin( Theta/2 ), 0, -llCos( Theta/2 )>;

}

rotation pAngleOfReachHeight(vector cPos,vector tPos,float V,float G,integer cAng){

   float Theta;
   float D = llVecDist(cPos,tPos);float D2 = D*D;
   float V2 = V*V;float V4 = V*V*V*V;
   float X = llVecDist(<cPos.x,cPos.y,0>,<tPos.x,tPos.y,0>);float X2 = X*X;
   float Y = tPos.z - cPos.z;
   
   float MaxD =  (V * llCos(PI/4) / G) + (V * llSin(PI/4) + llSqrt( llPow( (V*llSin(PI/4)) + (2*G*Y),2) ) );
   
   float Thingy = V4 - ( G * (G*X2 + 2*Y*V2 ));
   if(Thingy <= 0){
       //llSetText("Over max distance!",<1,1,1>,1.0);
       return ZERO_ROTATION;}
   
   float Sqrt = llSqrt(Thingy);
   float x;
   if( cAng ) x = V2 + Sqrt;
   else x = V2 - Sqrt;
   float y = G*X;
   Theta = llAtan2(x,y);
   return <0, llSin( Theta/2 ), 0, -llCos( Theta/2 )>;

}

// rotation PointRot = llRotBetween(<1,0,0>,llVecNorm(<tPos.x,tPos.y,cPos.z> - cPos)); // rotation TrajRot = AngleOfReach2(cPos, tPos, 10.0, 9.8);

// if(TrajRot == ZERO_ROTATION) return;

// RezPos = cPos; // RezVel = < 10.0, 0.0, 0.0 > * (TrajRot * PointRot); // RezRot = PointRot; // RezObj = llGetInventoryName(INVENTORY_OBJECT,0);


// TARGET INTERCEPTION/LEADING

vector Interception( vector cPos, vector tPos, vector cVel, vector tVel ){

   vector rPos = tPos - cPos;
   float tVxtV = tVel * tVel; 
   float b = rPos * tVel; 
   float a = cVel * cVel - tVxtV;
   return tPos + ( b + llSqrt( b*b + a * (rPos*rPos) ) ) / a * tVel;}

default{state_entry(){}} </lsl>