Difference between revisions of "Category:LSL Vector"

From Second Life Wiki
Jump to navigation Jump to search
m (Replaced <source> with <syntaxhighlight>)
(Added explicit examples for each operator)
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.   


<syntaxhighlight lang="lsl2">vector vec;
<syntaxhighlight lang="lsl2">
float x = vec.x;
vector vec = <1, 2, 3>;
float y = vec.y;
float x = vec.x; // 1.0
float z = vec.z;</syntaxhighlight>
float y = vec.y; // 2.0
float z = vec.z; // 3.0
</syntaxhighlight>


===Uses===
===Uses===
Vectors can be used to hold the following:
Vectors can be used to hold the following:
* {{LSLGC|Movement|Position}}: x, y and z are in metres.
* {{LSLGC|Movement|Position}}: x, y, and z are in metres.
* {{LSLGC|Movement|Velocity}}: x, y and z represent speeds.
* {{LSLGC|Movement|Velocity}}: x, y, and z are in meters per second.
* {{LSLGC|Color|Colour}}: Red is x, Green is y and Blue is z.
* {{LSLGC|Color|Colour}}: x is Red, y is Green, and z is Blue.


===Operators===
===Operators===
Vectors support the following operations:
Vectors support the following operations:
* Addition, Operator "+"
==== Addition, Operator "+" ====
* Subtraction, Operator "-"
<syntaxhighlight lang="lsl2">
* Multiplication (Dot Product), Operator "*"
vector v = <1, 2, 3> + <0.5, 0.0, 3> // <1.5, 2.0, 6.0>
* Cross Product, Operator "%"
</syntaxhighlight>


==== Subtraction, Operator "-" ====
<syntaxhighlight lang="lsl2">
vector v = <1, 1, 1> - <0.5, 0.0, 3> // <0.5, 1.0, -2.0>
</syntaxhighlight>
==== Dot Product, Operator "*" ====
<syntaxhighlight lang="lsl2">
float f = <9, 2, 7> * <4, 8, 10> // 122.0
</syntaxhighlight>
==== Cross Product, Operator "%" ====
<syntaxhighlight lang="lsl2">
float f = <2, 3, 4> * <5, 6, 7> // 56.0
</syntaxhighlight>
==== Scalar ====
A vector can be multiplied or divided by a [[Float]] or [[Integer]] to scale it.
Each component of the vector will be multiplied or divided by the same value.
<syntaxhighlight lang="lsl2">
vector v = <1, 2, 3> * 1.5 // <1.5, 3.0, 4.5>
</syntaxhighlight>
==== Rotation ====
A vector can be multiplied or divided by a [[Quaternion]] to rotate it.
A vector can be multiplied or divided by a [[Quaternion]] to rotate it.
<syntaxhighlight lang="lsl2">
// 45-degree rotation around the Z (up) axis
rotation r = llEuler2Rot(<0, 0, 45> * DEG_TO_RAD);


A vector can be multiplied or divided by a [[Float]] or [[Integer]] to scale it.
// Vector whose direction is 1 meter to the east
vector v = <1, 0, 0>;


===Example===
// Vector whose direction is north-east
<syntaxhighlight lang="lsl2">vector test=<1.0, 2.0, 3.0>;
vector rotated = v * r; // <0.707107, 0.707107, 0.000000>
llOwnerSay((string)test.z); // Outputs 3.0</syntaxhighlight>
</syntaxhighlight>


===Useful Snippets===
===Useful Snippets===
Check whether a string value is a valid vector:
<syntaxhighlight lang="lsl2">integer IsVector(string s)
<syntaxhighlight lang="lsl2">integer IsVector(string s)
{
{

Revision as of 15:16, 1 April 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 = <1, 2, 3>;
float x = vec.x; // 1.0
float y = vec.y; // 2.0
float z = vec.z; // 3.0

Uses

Vectors can be used to hold the following:

  • Position: x, y, and z are in metres.
  • Velocity: x, y, and z are in meters per second.
  • Colour: x is Red, y is Green, and z is Blue.

Operators

Vectors support the following operations:

Addition, Operator "+"

vector v = <1, 2, 3> + <0.5, 0.0, 3> // <1.5, 2.0, 6.0>

Subtraction, Operator "-"

vector v = <1, 1, 1> - <0.5, 0.0, 3> // <0.5, 1.0, -2.0>

Dot Product, Operator "*"

float f = <9, 2, 7> * <4, 8, 10> // 122.0

Cross Product, Operator "%"

float f = <2, 3, 4> * <5, 6, 7> // 56.0

Scalar

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

Each component of the vector will be multiplied or divided by the same value.

vector v = <1, 2, 3> * 1.5 // <1.5, 3.0, 4.5>

Rotation

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

// 45-degree rotation around the Z (up) axis
rotation r = llEuler2Rot(<0, 0, 45> * DEG_TO_RAD);

// Vector whose direction is 1 meter to the east
vector v = <1, 0, 0>;

// Vector whose direction is north-east
vector rotated = v * r; // <0.707107, 0.707107, 0.000000>

Useful Snippets

Check whether a string value is a valid vector:

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.