llAngleBetween

From Second Life Wiki
Revision as of 08:25, 2 September 2012 by Strife Onizuka (talk | contribs) (I've re-arranged the math)
Jump to navigation Jump to search

Summary

Function: float llAngleBetween( rotation a, rotation b );
0.0 Forced Delay
10.0 Energy

Returns a float that is the angle between rotation a and b.

• rotation a start rotation
• rotation b end rotation

Examples

See Also

Functions

•  llRotBetween
•  llRot2Angle Similar functionality used for the Axis-Angle format

Deep Notes

Reference

<lsl>float AngleBetween(rotation a, rotation b)//simple but turns out to not be very accurate. {

   return 2.0 * llAcos((a.x * b.x + a.y * b.y + a.z * b.z + a.s * b.s)
              / llSqrt((a.x * a.x + a.y * a.y + a.z * a.z + a.s * a.s)
                     * (b.x * b.x + b.y * b.y + b.z * b.z + b.s * b.s)));

}</lsl>

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

   rotation r = b / a; // calculate the rotation between the two arguments as quaternion
   float s2 = r.s * r.s; // square of the s-element
   float v2 = r.x * r.x + r.y * r.y + r.z * r.z; // sum of the squares of the v-elements
   if (s2 < v2) // compare the s-component to the v-component
       return 2 * llAcos(llSqrt(s2 / (s2 + v2))); // use arccos if the v-component is dominant
   else if (v2)
       return 2 * llAsin(llSqrt(v2 / (s2 + v2))); // use arcsin if the s-component is dominant
   return 0; // one or both arguments are scaled too small to be meaningful, or the values are the same, so return zero

}//Written by Moon Metty, Miranda Umino. Minor optimizations by Strife Onizuka</lsl>

Watch for developments on this function with SVC-2424

Signature

function float llAngleBetween( rotation a, rotation b );