Vector2 - stingray.Vector2 object reference - Stingray Lua API Reference

stingray.Vector2 object reference

Description

Describes a three-dimensional vector.

NOTE: Vector2 objects are temporary. You can only use them in the frame in which they were generated. If you need to save a Vector2 across multiple frames, use a Vector2Box instead. For more information, see Object Lifetimes and Userdata Binding.

Operators

You can use several operators like + and - to modify and transform Vector2 objects, described below.

Addition: +

You can add two vectors together using the + operator. For example:

newVector = vector1 + vector2

This is equivalent to the add() function.

Subtraction: -

You can subtract one vector from another using the - operator. For example:

newVector = vector1 - vector2

This is equivalent to the subtract() function.

Negation: -

You can negate a vector using the - operator as a prefix to any vector variable. For example:

negativeVector = -vector1

Multiplication: *

You can multiply a vector by a scalar value using the * operator. For example:

newVector = vector * scalarFactor

This is equivalent to the multiply() function. See also multiply_elements(), which lets you multiply two vectors together per-element.

Division: /

You can divide a vector by a scalar value using the / operator. For example:

newVector = vector / scalarFactor

This is equivalent to the divide() function. See also divide_elements(), which lets you divide one vector by another per-element.

Accessing components: []

You can set and retrieve component values by using the [] operator. Use the indices 1 or "x" for the X component, and 2 or "y" for the Y component. For example:

yComponentValue = vector[2]
xComponentValue = vector["x"]

Data Members

x : number

The extent of the vector along the X axis.

y : number

The extent of the vector along the Y axis.

Functions

Parameters

vector_a :

stingray.Vector2

The first vector to add.

vector_b :

stingray.Vector2

The second vector to add.

Returns

stingray.Vector2

The sum of the two input vectors.

Parameters

vector_a :

stingray.Vector2

The first point.

vector_b :

stingray.Vector2

The second point.

Returns

number

The scalar distance between vector_a and vector_b.

Parameters

vector_a :

stingray.Vector2

The first vector.

vector_b :

stingray.Vector2

The second vector.

Returns

number

The square of the distance between vector_a and vector_b.

Parameters

vector :

stingray.Vector2

The vector to divide.

factor :

number

A scalar value to divide into the vector.

Returns

stingray.Vector2

The quotient of the input vector and the scalar value.

Parameters

vector_a :

stingray.Vector2

The vector to be divided.

vector_b :

stingray.Vector2

The vector to divide into vector_a.

Returns

stingray.Vector2

The quotient of the two input vectors.

Parameters

vector_a :

stingray.Vector2

The first vector.

vector_b :

stingray.Vector2

The second vector.

Returns

number

The dot product.

Parameters

vector :

stingray.Vector2

The vector whose component will be returned.

index :

integer

The index of the component to return. Use 1 for the X component, or 2 for the Y component.

Returns

number

The value of the element at the specified index.

Parameters

vector_a :

stingray.Vector2

The first vector to compare.

vector_b :

stingray.Vector2

The second vector to compare.

Returns

boolean

Returns true if the two vectors are identical, or false otherwise.

Parameters

vector :

stingray.Vector2

The vector to be validated.

Returns

boolean

Returns true if all components of the vector are valid numbers, or false if any component is #NaN or #INF.

Parameters

vector :

stingray.Vector2

The vector whose length will be returned.

Returns

number

The length of the vector.

Parameters

vector_a :

stingray.Vector2

The first vector.

vector_b :

stingray.Vector2

The second vector.

ratio :

number

The interpolation ratio, or weight. May be any value between 0 and 1 inclusive. Values closer to 0 produce vectors closer to vector_a; values closer to 1 produce vectors closer to vector_b.

Returns

stingray.Vector2

The resulting vector.

Parameters

vector_a :

stingray.Vector2

The first vector.

vector_b :

stingray.Vector2

The second vector.

Returns

stingray.Vector2

The resulting vector.

Parameters

vector_a :

stingray.Vector2

The first vector.

vector_b :

stingray.Vector2

The second vector.

Returns

stingray.Vector2

The resulting vector.

Parameters

vector :

stingray.Vector2

The vector to multiply.

factor :

number

A scalar value to multiply with the vector.

Returns

stingray.Vector2

The product of the input vector and the scalar value.

Parameters

vector_a :

stingray.Vector2

The first vector to multiply.

vector_b :

stingray.Vector2

The second vector to multiply.

Returns

stingray.Vector2

The product of the two input vectors.

Parameters

vector :

stingray.Vector2

The vector to be normalized.

Returns

stingray.Vector2

The normalized vector.

Parameters

vector :

stingray.Vector2

The vector whose component will be set.

index :

integer

The index of the component to set. Use 1 for the X component, or 2 for the Y component.

value :

number

The value to set at the specified index.

Returns
This function does not return any values.
Parameters

vector :

stingray.Vector2

The vector whose component will be set.

value :

number

The new value for the X component.

Returns
This function does not return any values.
Parameters

vector :

stingray.Vector2

The vector whose components will be set.

x :

number

The new value for the X component.

y :

number

The new value for the Y component.

Returns
This function does not return any values.
Parameters

vector :

stingray.Vector2

The vector whose component will be set.

value :

number

The new value for the Y component.

Returns
This function does not return any values.
Parameters

vector_a :

stingray.Vector2

The starting vector.

vector_b :

stingray.Vector2

The vector that will be subtracted.

Returns

stingray.Vector2

The result of the subtraction.

Parameters

vector :

stingray.Vector2

The vector to be broken down into its components.

Returns

number

The X component of the vector.

number

The Y component of the vector.

Parameters

vector :

stingray.Vector2

The vector to return as a string.

Returns

string

The string representation.

Use this function only for debugging purposes; do not attempt to save the string and read it back into a new vector. Use a Vector2Box to save vectors across multiple frames.

Parameters

vector :

stingray.Vector2

The vector whose component will be returned.

Returns

number

The value of the X component.

Parameters

vector :

stingray.Vector2

The vector whose component will be returned.

Returns

number

The value of the Y component.

Parameters
This function does not accept any parameters.
Returns

stingray.Vector2

The zero vector.