Talk:LlAngleBetween

From Second Life Wiki
Jump to navigation Jump to search

LSO Optimized Reference Impelementation

This version of the latest reference implementation is optimized for LSO bytecode. Execution speed should be about the same.

<lsl>float AngleBetween(rotation a, rotation b)//more complex implementation but more accurate, and reasonably fast. {

   b /= a; // calculate the rotation between the two arguments as quaternion
   float s2 = b.s * b.s; // square of the s-element
   float v2 = <b.x, b.y, b.z> * <b.x, b.y, b.z>; // sum of the squares of the v-elements
   //we bulk all our returns into a single return because returning requires several bytecodes.
   //specifically the local variables and parameters must be popped off the stack.
   if (s2 < v2) // compare the s-component to the v-component
       v2 = llAcos(llSqrt(s2 / (s2 + v2))); // use arccos if the v-component is dominant
   else if (v2) // make sure the v-component is non-zero
       v2 = llAsin(llSqrt(v2 / (s2 + v2))); // use arcsin if the s-component is dominant
   //if one or both arguments are scaled too small to be meaningful v2 is zero, or the values are the same, v2 is zero.
   //in that case we want to return zero so multiplying v2 by 2.0 will have no effect on the value returned.
   return v2 * 2.0; 

}//Written by Moon Metty & Miranda Umino. Optimizations by Strife Onizuka</lsl>

-- Strife (talk|contribs) 09:48, 2 September 2012 (PDT)