Geometric
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
Geometric Library
Line Functions
Line Nearest Point | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Calculates the vector from a point to the closest point on a line <lsl> vector gLXdV(vector O,vector D,vector A){ return (O-A)-((O-A)*D)*D;} </lsl>
By Nexii Malthus
|
Line Nearest Point, Distance | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Calculates distance of this vector, but faster on it's own <lsl> float gLXdZ(vector O,vector D,vector A){ return llSqrt( ((A-O)%D) * ((A-O)%D) );} </lsl>
By Nexii Malthus
|
Line and Line, Vector | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Shortest vector of two lines <lsl> vector gLLdV(vector O1,vector D1,vector O2,vector D2){ return Project3D( (O2-O1), D1%D2 );} </lsl>
By Nexii Malthus
|
Line and Line, Distance | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns the distance between two lines <lsl> float gLLdZ(vector O1,vector D1,vector O2,vector D2){ vector A = D1%D2;float B = llVecMag(A);A = <A.x/B,A.y/B,A.z/B>; return (O2-O1) * A;} </lsl>
By Nexii Malthus
|
Line and Line, Nearest point | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Closest point of two lines <lsl> vector gLLnX(vector O1,vector D1,vector O2,vector D2){ vector nO1 = < O1*D1, O1*D2, 0>; vector nO2 = < O2*D1, O2*D2, 0>; vector nD1 = < D1*D1, O1*D2, 0>; vector nD2 = < O2*D1, O2*D2, 0>; float t = ( nD2.x*nD1.y - nD1.x*nD2.y ); t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t; return O1 + D1*t;} </lsl>
By Nexii Malthus
|
Line and Line, two nearest points along both lines | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Two closest points of two lines on each line <lsl> vector X1;vector X2; gLLnnXX(vector O1,vector D1,vector O2,vector D2){ vector nO1 = < O1*D1, O1*D2, 0>; vector nO2 = < O2*D1, O2*D2, 0>; vector nD1 = < D1*D1, O1*D2, 0>; vector nD2 = < O2*D1, O2*D2, 0>; float t = ( nD2.x*nD1.y - nD1.x*nD2.y ); t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t; X1 = O1 + D1*t; X2 = X1 + nD1%nD2;} </lsl>
By Nexii Malthus
|
Line and Line, two nearest points with vector and distance | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Computes two closest points of two lines, vector and distance <lsl> vector X1;vector X2;vector V1;float Z1; gLLnnXXVZ(vector O1,vector D1,vector O2,vector D2){ vector nO1 = < O1*D1, O1*D2, 0>; vector nO2 = < O2*D1, O2*D2, 0>; vector nD1 = < D1*D1, O1*D2, 0>; vector nD2 = < O2*D1, O2*D2, 0>; float t = ( nD2.x*nD1.y - nD1.x*nD2.y ); t = ( nD2.y*(nO1.x-nO2.x) - nD2.x*(nO1.y-nO2.y) ) / t; X1 = O1 + D1*t; X2 = X1 + CP(nD1,nD2); V1 = nD1%nD2; Z1 = llVecMag(V1);} </lsl>
By Nexii Malthus
|
Plane Functions
Plane and Point, Distance | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Finds distance of a point from a plane <lsl> float gPXdZ(vector Pn,float Pd,vector A){ return A * Pn + Pd;} </lsl>
By Nexii Malthus
|
Plane and Point, Vector | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Finds vector that points from point to nearest on plane <lsl> vector gPXdV(vector Pn,float Pd,vector A){ return -(Pn * A + Pd)*Pn;} </lsl>
By Nexii Malthus
|
Plane and Point, Nearest point | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Finds closest point on plane given point <lsl> vector gPXnX(vector Pn,float Pd,vector A){ return A - (Pn * A + Pd) * Pn;} </lsl>
By Nexii Malthus
|
Plane and Ray, Intersection Distance | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Finds distance to intersection of plane along ray <lsl> float gPRxZ(vector Pn,float Pd,vector O,vector D){ return -((Pn*O+Pd)/(Pn*D));} </lsl>
By Nexii Malthus
|
Plane and Ray, Vector | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Finds distance vector along a ray to a plane <lsl> vector gPRdV(vector Pn,float Pd,vector O,vector D){ return D * gPRxZ(Pn,Pd,O,D);} </lsl>
By Nexii Malthus
|
Plane and Ray, Intersection Point | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Finds intersection point along a ray to a plane <lsl> vector gPRxX(vector Pn,float Pd,vector O,vector D){ return O + gPRdV(Pn,Pd,O,D);} </lsl>
By Nexii Malthus
|
Plane and Line, Intersection Point | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Finds interesection point of a line and a plane <lsl> vector gPLxX(vector Pn,float Pd,vector O,vector D){ return O -( (Pn*D)/(Pn*O+Pd) )*D;} </lsl>
By Nexii Malthus
|
Plane and Plane, Intersection Line | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Finds line of intersection of two planes <lsl> vector oO;vector oD; gPPxL(vector Pn,float Pd,vector Qn,float Qd){ oD = (Pn%Qn)/llVecMag(Pn%Qn); vector Cross = (Pn%Qn)%Pn; vector Bleh = (-Pd*Pn); oO = Bleh - (Qn*Cross)/(Qn*Bleh+Qd)*Cross/llVecMag(Cross);} </lsl>
By Nexii Malthus
|
Plane and Ray, Projection | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Projects a ray onto a plane <lsl> vector oO;vector oD; gPRpR(vector Pn,float Pd,vector O,vector D){ oO = O - (Pn * O + Pd) * Pn; vector t = llVecNorm( D - Project3D(D,Pn) );t = <1.0/t.x,1.0/t.y,1.0/t.z>; oD = Pn%t;} </lsl>
By Nexii Malthus
|
Sphere Functions
Sphere and Ray, Intersection Point | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Finds intersection point of sphere and ray <lsl> vector gSRxX(vector Sp, float Sr, vector Ro, vector Rd){ float t;Ro = Ro - Sp; if(Rd == ZERO_VECTOR) return ZERO_VECTOR; float a = Rd * Rd; float b = 2 * Rd * Ro; float c = (Ro * Ro) - (Sr * Sr); float disc = b * b - 4 * a * c; if(disc < 0) return ZERO_VECTOR; float distSqrt = llSqrt(disc); float q; if(b < 0) q = (-b - distSqrt)/2.0; else q = (-b + distSqrt)/2.0; float t0 = q / a; float t1 = c / q; if(t0 > t1){ float temp = t0; t0 = t1; t1 = temp; } if(t1 < 0) return ZERO_VECTOR; if(t0 < 0) t = t1; else t = t0; return Ro + (t * Rd); } </lsl>
By Nexii Malthus
|
Sphere and Ray, Intersection Boolean | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Finds if there is a intersection of sphere and ray <lsl> integer gSRx(vector Sp, float Sr, vector Ro, vector Rd){ float t;Ro = Ro - Sp; //vector RayOrg = llDetectedPos(x) - llGetPos(); if(Rd == ZERO_VECTOR) return FALSE; float a = Rd * Rd; float b = 2 * Rd * Ro; float c = (Ro * Ro) - (Sr * Sr); float disc = b * b - 4 * a * c; if(disc < 0) return FALSE; return TRUE; } </lsl>
By Nexii Malthus
|
Ray Functions
Ray and Point, projected distance | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Finds projected distance of a point along a ray <lsl> float gRXpZ(vector O,vector D,vector A){ return (A-O)*D;} </lsl>
By Nexii Malthus
|
Box Functions
Box and Ray, Intersection Distance | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Finds intersection of a Ray to a Box and returns intersection distance, otherwise -1 if there is no legal intersection. <lsl> float gBRxZ(vector Ro,vector Rd, vector Bo, vector Bs, rotation Br){ vector oB = (Ro-Bo)/Br; vector dB = Rd/Br; vector eB = 0.5*Bs; float mD = -1.0; float D; vector X; if(llFabs(dB.x) > 0.000001){ D = (-eB.x - oB.x ) / dB.x; if(D >= 0.0){ X = oB + D * dB; if(X.y >= -eB.y && X.y <= eB.y && X.z >= -eB.z && X.z <= eB.z) mD = D; } D = ( eB.x - oB.x ) / dB.x; if (D >= 0.0){ X = oB + D * dB; if(X.y >= -eB.y && X.y <= eB.y && X.z >= -eB.z && X.z <= eB.z) if (mD < 0.0 || mD > D) mD = D; } } if(llFabs(dB.y) > 0.000001){ D = (-eB.y - oB.y ) / dB.y; if(D >= 0.0){ X = oB + D * dB; if(X.x >= -eB.x && X.x <= eB.x && X.z >= -eB.z && X.z <= eB.z) if (mD < 0.0 || mD > D) mD = D; } D = ( eB.y - oB.y ) / dB.y; if (D >= 0.0){ X = oB + D * dB; if(X.x >= -eB.x && X.x <= eB.x && X.z >= -eB.z && X.z <= eB.z) if (mD < 0.0 || mD > D) mD = D; } } if(llFabs(dB.z) > 0.000001){ D = (-eB.z - oB.z ) / dB.z; if(D >= 0.0){ X = oB + D * dB; if(X.x >= -eB.x && X.x <= eB.x && X.y >= -eB.y && X.y <= eB.y) if (mD < 0.0 || mD > D) mD = D; } D = ( eB.z - oB.z ) / dB.z; if (D >= 0.0){ X = oB + D * dB; if(X.x >= -eB.x && X.x <= eB.x && X.y >= -eB.y && X.y <= eB.y) if (mD < 0.0 || mD > D) mD = D; } } return mD; } </lsl>
By Hewee Zetkin
|
Box and Ray, Intersection Point | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Finds intersection of a Ray to a Box and returns intersection point, otherwise ZERO_VECTOR if there is no legal intersection. <lsl> vector gBRxX( vector Ro, vector Rd, vector Bo, vector Bs, rotation Br){ float k = gBRxZ(Ro,Rd,Bo,Bs,Br); if( k != -1.0 ) return Ro + Rd * k; else return ZERO_VECTOR;} </lsl>
By Hewee Zetkin
|
Box and Point, Intersection Boolean | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Finds if there is an intersection of a Point and a Box and returns boolean <lsl> integer gBPx(vector A, vector Bo, vector Bs, rotation Br){ vector eB = 0.5*Bs; vector rA = (A-Bo)/Br; if( rA.x < eB.x && rA.x > -eB.x && rA.y < eB.y && rA.y > -eB.y && rA.z < eB.z && rA.z > -eB.z ) return TRUE; else return FALSE;} </lsl>
By Nexii Malthus
|
Other Functions
3D Projection | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
Projects a vector A by vector B. <lsl> vector Project3D(vector A,vector B){ vector proj; proj.x = ( (A*B) / (B.x*B.x + B.y*B.y + B.z*B.z) ) * B.x; proj.y = ( (A*B) / (B.x*B.x + B.y*B.y + B.z*B.z) ) * B.y; proj.z = ( (A*B) / (B.x*B.x + B.y*B.y + B.z*B.z) ) * B.z; return proj;} </lsl>
By Nexii Malthus
|
Legend
For anyone curious to the shorthand used and who wish to use a lookup table can use this as a reference. Or anyone who wishes to add a new function to the library is welcome to but it would be recommended to keep consistency. I tried to minimize the script function names to be easily readable. All the geometric function names start with a g. Here is the legend:
Shorthand | Name | Description |
---|---|---|
Geometric Types | ||
X | Point | vector defining a point in space |
L | Line | A line has an origin and a direction |
R | Ray | A ray is like a line, except it is more distinct as it defines wether it points forward or back |
P | Plane | A 2D doubly ruled surface of infinite size |
S | Sphere | A sphere is defined by origin and radius |
B | Box | A box is six sided and defined by origin, size as well as a rotation. |
What does it do? | ||
d | Distance | Calculate distance |
n | Nearest | Calculate nearest |
p | Project | Calculates projection |
x | Intersection | Calculates intersection |
What kind of data do I get out of it? | ||
Z | Float | Represents that a float is returned |
V | Vector | Represents that a vector is returned |
O | Origin | Represents the Origin of the ray or line |
D | Direction | Direction from the Origin |