Category:LSL Vector

From Second Life Wiki
Jump to navigation Jump to search

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.