Difference between revisions of "Category:LSL Vector"

From Second Life Wiki
Jump to navigation Jump to search
m
(→‎Components: can access components only with variables, not literals or function returns)
 
(12 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{Multi-lang}}
{{LSL Header|ml=*}}{{LSLC|}}{{LSLC|Types}}
{{LSL Header}}
==Vector==
==Vector==


A vector is a data [[:Category:LSL Types|type]] that contains a set of three [[LSL_float|float]] values. Each element can be accessed individually by appending .x, .y, or .z to the variable name. 
A vector is a data {{LSLGC|Types|type}} that contains a set of three {{LSLGC|Float|float}} values.


Vectors can be used to hold the following:
===Components===
* Position: x, y and z are in metres.
Each element can be accessed individually by appending .x, .y, or .z to the variable name.
* Velocity: x, y and z represent speeds.
* {{LSLGC|Color|Colour}}: Red is x, Green is y and Blue is z.


<syntaxhighlight lang="lsl2">
vector vec = <1, 2, 3>;
float x = vec.x; // 1.0
float y = vec.y; // 2.0
float z = vec.z; // 3.0
</syntaxhighlight>
Note that components can be only accessed with vector variables. Attempting to access the components of a vector literal or a return value from a function is illegal.
<syntaxhighlight lang="lsl2">
// These are not valid component accesses
float f1 = <0, 1, 2>.z;
float f2 = llGetScale().x;
</syntaxhighlight>
===Uses===
Vectors are often used for...
* {{LSLGC|Movement|Position}}: x, y, and z are in metres.
* {{LSLGC|Movement|Velocity}}: x, y, and z are in meters per second.
* {{LSLGC|Color|Colour}}: x is Red, y is Green, and z is Blue.
===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">
vector v = <2, 3, 4> % <5, 6, 7>; // <-3, 6, -3>
</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>
vector v = <1, 2, 3> / 1.5; // <0.666667, 1.333333, 2.000000>
</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);
// 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>


===Example===
// Vector whose direction is south-east
<lsl>vector test=<1.0, 2.0, 3.0>;
// (division performs a rotation in the opposite direction)
llOwnerSay((string)test.z); // Outputs 3.0</lsl>
vector rotated = v / r; // <0.707107, -0.707107, 0.000000>
</syntaxhighlight>


===Useful Snippets===
===Useful Snippets===
<lsl>integer IsVector(string s)
Check whether a string value is a valid vector:
<syntaxhighlight lang="lsl2">integer IsVector(string s)
{
{
     list split = llParseString2List(s, [" "], ["<", ">", ","]);
     list split = llParseString2List(s, [" "], ["<", ">", ","]);
     if(llGetListLength(split) != 7)//we must check the list length, or the next test won't work properly.
     if(llGetListLength(split) != 7)//we must check the list length, or the next test won't work properly.
         return 0;
         return FALSE;
     return !((string)((vector)s) == (string)((vector)((string)llListInsertList(split, ["-"], 5))));
     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,
     //it works by trying to flip the sign on the Z element of the vector,
Line 33: Line 87:
     //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</lsl>
}//Strife Onizuka</syntaxhighlight>

Latest revision as of 16:01, 15 October 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

Note that components can be only accessed with vector variables. Attempting to access the components of a vector literal or a return value from a function is illegal.

// These are not valid component accesses
float f1 = <0, 1, 2>.z;
float f2 = llGetScale().x;

Uses

Vectors are often used for...

  • 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 "%"

vector v = <2, 3, 4> % <5, 6, 7>; // <-3, 6, -3>

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>
vector v = <1, 2, 3> / 1.5; // <0.666667, 1.333333, 2.000000>

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>

// Vector whose direction is south-east
// (division performs a rotation in the opposite direction)
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.