Talk:Send vector as on rez parameter

From Second Life Wiki
Revision as of 02:51, 27 December 2012 by Omei Qunhua (talk | contribs) (Simpler suggestons)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Conversion of a vector to an integer and back can be done far more simply.

Firstly, you need to realise that the 3 components of a vector v can be accessed as v.x, v.y and v.z, so your process of casting a vector to a string and then parsing to a list is ... (words fail me)

Here is a simple method with similar effect to yours for decimal vectors (each component 0 thru 999)

<lsl> integer Vector2IntDec(vector v) {

   return ( 1000000 * (integer) v.x + 1000 * (integer) v.y + (integer) v.z);

}

vector Int2VectorDec(integer i) {

   vector v;
   v.x = i / 1000000;
   v.y = (i % 1000000) / 1000 ;
   return <v.x, v.y, i % 1000>;

} </lsl>

However, it's nicer to do the conversions using bit shifts. As an integer (in LSL) consists of 31 bits plus sign bit, we can comfortably allocate 10 bits to each vector component, allowing each component to hold a value in the range 0 to 1023.

<lsl> integer Vector2Int(vector v) {

   integer n = (integer) v.x << 20;
   n = n | ( (integer) v.y) << 10;
   return  n | ( integer) v.z;

}

vector Int2Vector(integer n) {

   vector v;
   v.x = n >> 20;
   v.y = (n >> 10) & 0x3FF;
   return <v.x, v.y,  (n & 0x3FF) >;

} </lsl>

(These examples have been compiled and tested using lslEditor and Mono)

Omei Qunhua 01:51, 27 December 2012 (PST)