Difference between revisions of "Category:LSL Vector"

From Second Life Wiki
Jump to navigation Jump to search
(added note about scaling being supported)
m (Replaced <source> with <syntaxhighlight>)
Line 7: Line 7:
Each element can be accessed individually by appending .x, .y, or .z to the variable name.   
Each element can be accessed individually by appending .x, .y, or .z to the variable name.   


<source lang="lsl2">vector vec;
<syntaxhighlight lang="lsl2">vector vec;
float x = vec.x;
float x = vec.x;
float y = vec.y;
float y = vec.y;
float z = vec.z;</source>
float z = vec.z;</syntaxhighlight>


===Uses===
===Uses===
Line 30: Line 30:


===Example===
===Example===
<source lang="lsl2">vector test=<1.0, 2.0, 3.0>;
<syntaxhighlight lang="lsl2">vector test=<1.0, 2.0, 3.0>;
llOwnerSay((string)test.z); // Outputs 3.0</source>
llOwnerSay((string)test.z); // Outputs 3.0</syntaxhighlight>


===Useful Snippets===
===Useful Snippets===
<source lang="lsl2">integer IsVector(string s)
<syntaxhighlight lang="lsl2">integer IsVector(string s)
{
{
     list split = llParseString2List(s, [" "], ["<", ">", ","]);
     list split = llParseString2List(s, [" "], ["<", ">", ","]);
Line 44: Line 44:
     //if the vector was already broken then the sign flip will have no affect and the values will match
     //if the vector was already broken then the sign flip will have no affect and the values will match
     //we cast back to string so we can catch negative zero which allows for support of ZERO_VECTOR
     //we cast back to string so we can catch negative zero which allows for support of ZERO_VECTOR
}//Strife Onizuka</source>
}//Strife Onizuka</syntaxhighlight>

Revision as of 10:14, 11 February 2023

Vector

A vector is a data type that contains a set of three float values.

Components

Each element can be accessed individually by appending .x, .y, or .z to the variable name.

vector vec;
float x = vec.x;
float y = vec.y;
float z = vec.z;

Uses

Vectors can be used to hold the following:

  • Position: x, y and z are in metres.
  • Velocity: x, y and z represent speeds.
  • Colour: Red is x, Green is y and Blue is z.

Operators

Vectors support the following operations:

  • Addition, Operator "+"
  • Subtraction, Operator "-"
  • Multiplication (Dot Product), Operator "*"
  • Cross Product, Operator "%"

A vector can be multiplied or divided by a Quaternion to rotate it.

A vector can be multiplied or divided by a Float or Integer to scale it.

Example

vector test=<1.0, 2.0, 3.0>;
llOwnerSay((string)test.z); // Outputs 3.0

Useful Snippets

integer IsVector(string s)
{
    list split = llParseString2List(s, [" "], ["<", ">", ","]);
    if(llGetListLength(split) != 7)//we must check the list length, or the next test won't work properly.
        return FALSE;
    return !((string)((vector)s) == (string)((vector)((string)llListInsertList(split, ["-"], 5))));
    //it works by trying to flip the sign on the Z element of the vector,
    //if it works or breaks the vector then the values won't match.
    //if the vector was already broken then the sign flip will have no affect and the values will match
    //we cast back to string so we can catch negative zero which allows for support of ZERO_VECTOR
}//Strife Onizuka

Subcategories

This category has the following 2 subcategories, out of 2 total.

Pages in category "LSL Vector"

The following 6 pages are in this category, out of 6 total.