Difference between revisions of "Global to root relative transformation script"

From Second Life Wiki
Jump to navigation Jump to search
m
m (links)
Line 1: Line 1:
A script in a child prim of an object may need to transform a location or other vector from global coordinates to root relative coordinates.
A script in a child [[primitive] of an [[object]] may need to transform a location or other [[vector]] from global [[Viewer coordinate frames|coordinates]] to root relative coordinates.
The angular transformation for a vector can be done by:
The angular transformation for a vector can be done by:



Revision as of 16:09, 6 June 2007

A script in a child [[primitive] of an object may need to transform a location or other vector from global coordinates to root relative coordinates. The angular transformation for a vector can be done by:

vector Glob2Rel( vector glob ) // global function
{
   rotation rot;
   vector rel;
   float mag;
   //
   rot = llGetRootRotation(); // The rotation from global to relative coordinates.
   mag = llSqrt( rel * rel ); // Vector dot product then square root gives magnitude.
   rel = llRot2Axis( rot *  ( llAxisAngle2Rot(rel,1) / rot ) ); // See below.
   rel = mag * rel; // Restore the magnitude of the vector.
   return rel;
}

First the magnitude is extracted, because this way of casting a vector to a rotation may not preserve its magnitude. (At best it would require an additional function call to retrieve it.) Next, the vector, treated as a coordinate axis, is converted to a rotation. This rotation is then multiplied on the right by the inverse of the coordinate rotation, and the product is multiplied on the left by the rotation. This transforms the rotation to the relative coordinate axes. The rotation is converted back to a vector. And finally the magnitude is restored. (Recall that quaternions do not commute.)

To transform a location, the root position is first subtracted:

   rel_x = Glob2Rel( glob_x - llGetRootPosition() );  // The root position is in global coordinates.

There are probably other ways of doing this, and there are also other ways of writing this sequence of operations.