Difference between revisions of "User:Dora Gustafson/llAxes2Rot right and wrong"

From Second Life Wiki
Jump to: navigation, search
(Visualize the importance of mutually orthogonal unit vectors in the llAxes2Rot function)
 
Line 91: Line 91:
 
Move around and fly up to see where the prim will point.
 
Move around and fly up to see where the prim will point.
 
Each routine will color the prim its own color:
 
Each routine will color the prim its own color:
Red: "Bastard" routine.
+
* Red: "Bastard" routine.
Yellow: "Norm" routine, unit vectors but not orthogonal.
+
* Yellow: "Norm" routine, unit vectors but not orthogonal.
Green: "True" routine, true direction.
+
* Green: "True" routine, true direction.
Blue: "Hor" routine, direction in the horizontal plane only.
+
* Blue: "Hor" routine, direction in the horizontal plane only.

Revision as of 07:11, 5 August 2009

To see if it really matters this demands on unit vectors and orthogonality, I made a test script to satisfy my own curiosity: <lsl> key model; integer tog=0; vector lookat;

rotation Vec2RotBastard( vector V ) {

   vector UP = < 0.0, 0.0, 1.0 >;
   vector LEFT = llVecNorm(UP%V);
   return llAxes2Rot(V, LEFT, UP);
   // demand for orthogonal, unit vectors is not fulfilled in general:
   // V is a unit vector in special cases only
   // UP and V are orthogonal in special cases only

}

rotation Vec2RotNorm( vector V ) {

   V = llVecNorm( V );
   vector UP = < 0.0, 0.0, 1.0 >;
   vector LEFT = llVecNorm(UP%V);
   return llAxes2Rot(V, LEFT, UP);
   // demand for orthogonal vectors is not fulfilled in general:
   // UP and V are orthogonal in special cases only

}

rotation Vec2RotTrue( vector V ) {

   V = llVecNorm( V );
   vector UP = < 0.0, 0.0, 1.0 >;
   vector LEFT = llVecNorm(UP%V);
   UP = llVecNorm(V%LEFT); // you want to keep the direction of V
   return llAxes2Rot(V, LEFT, UP);

}

rotation Vec2RotHor( vector V ) {

   V = llVecNorm( V );
   vector UP = < 0.0, 0.0, 1.0 >;
   vector LEFT = llVecNorm(UP%V);
   V = llVecNorm(LEFT%UP); // you want V confined to the horizontal plane
   return llAxes2Rot(V, LEFT, UP);

}

default {

   touch_end(integer n)
   {
       llSetTimerEvent( 4.0 );
       model = llDetectedKey(0);
   }
   timer()
   {
       lookat = llList2Vector( llGetObjectDetails( model, [OBJECT_POS]), 0 );
       if ( lookat != ZERO_VECTOR )
       {
           if ( !tog )
           {
               llSetColor ( <1.0, 0.2, 0.0>, ALL_SIDES );
               llSetRot( Vec2RotBastard( lookat - llGetPos()));
           }
           else if ( tog == 1 )
           {
               llSetColor ( <1.0, 1.0, 0.0>, ALL_SIDES );
               llSetRot( Vec2RotNorm( lookat - llGetPos()));
           }
           else if ( tog == 2 )
           {
               llSetColor ( <0.4, 1.0, 0.0>, ALL_SIDES );
               llSetRot( Vec2RotTrue( lookat - llGetPos()));
           }
           else if ( tog == 3 )
           {
               llSetColor ( <0.0, 0.6, 1.0>, ALL_SIDES );
               llSetRot( Vec2RotHor( lookat - llGetPos()));
           }
           tog = ++tog % 4;
       }
       else
       {
           llSetColor ( <0.0, 0.0, 0.0>, ALL_SIDES );
           llSetTimerEvent( 0.0 );
       }
   }

} </lsl> How to use: rezz a prim say x,y,z = 5.0, 0.2, 0.2 meters, put he script in and touch the prim. It will point you for 4 sec with each routine in turn. Move around and fly up to see where the prim will point. Each routine will color the prim its own color:

  • Red: "Bastard" routine.
  • Yellow: "Norm" routine, unit vectors but not orthogonal.
  • Green: "True" routine, true direction.
  • Blue: "Hor" routine, direction in the horizontal plane only.