Represents a vector with x and y components.
Fields
The x component of the vector (read-only).
local v = Vector.xy(10, -5)
local xValue = v.x -- 10
The y component of the vector (read-only).
local v = Vector.xy(10, -5)
local yValue = v.y -- -5
Constructors
Creates a vector with the specified x and y components.
local v = Vector.xy(5, -2) -- 5 -2
origin
Returns the zero vector (0, 0).
local origin = Vector.origin() -- 0 0
scaleAndAdd
Returns a + b * scale.
local r = Vector.scaleAndAdd(Vector.xy(1,2), Vector.xy(3,4), 2) -- 7 10
scaleAndSub
Returns a - b * scale.
local r = Vector.scaleAndSub(Vector.xy(7,10), Vector.xy(3,4), 2) -- 1 2
lerp
Returns the linear interpolation between the to and from
vector, using t where 0 returns the vector and 1 returns the other.
local p = Vector.lerp(Vector.xy(0,0), Vector.xy(8,0), 0.25) -- 2 0
normalized
Returns a normalized copy of the given vector.
local n = Vector.normalized(Vector.xy(10, 0)) -- Vector.xy(1, 0)
Static Functions
distance
Returns the distance between the two vectors.
local d = Vector.distance(Vector.xy(3, 4), Vector.xy(0, 0)) -- 5
distanceSquared
Returns the squared distance between the two vectors.
local d2 = Vector.distanceSquared(Vector.xy(3,4), Vector.xy(0,0)) -- 25
dot
Returns the dot product of the two vectors.
local dp = Vector.dot(Vector.xy(1,2), Vector.xy(3,4)) -- 11 (1*3 + 2*4)
cross
Returns the cross product of two vectors (z-component of the 3D cross product).
local c = Vector.cross(Vector.xy(1, 0), Vector.xy(0, 1)) -- 1
length
Returns the length of the given vector.
local len = Vector.length(Vector.xy(3, 4)) -- 5
lengthSquared
Returns the squared length of the given vector.
local len2 = Vector.lengthSquared(Vector.xy(3, 4)) -- 25
Methods
length
Deprecated - Use Vector.length instead.
Returns the length of the vector.
use Vector.length(v) instead.
lengthSquared
Deprecated - Use Vector.lengthSquared instead.
Returns the squared length of the vector.
Use Vector.lengthSquared(v) instead for better performance.
local v = Vector.xy(3, 4)
local len2 = v:lengthSquared() -- 25
normalized
Deprecated - Use Vector.normalized instead.
Returns a normalized copy of the vector. If the length is zero,
the result is the zero vector.
Use Vector.normalized(v) instead for better performance.
local v = Vector.xy(10, 0)
local n = v:normalized() -- 1 0
__eq
Returns true if the two vectors have equal components.
local a = Vector.xy(1, 2)
local b = Vector.xy(1, 2)
local c = Vector.xy(2, 1)
print(a == b) -- true
print(a == c) -- false
__unm
Returns the negated vector.
local v = Vector.xy(2, -3)
local neg = -v -- -2 3
__add
Returns the sum of two vectors.
local a = Vector.xy(2, 3)
local b = Vector.xy(-1, 5)
local c = a + b -- 1 8
__sub
Returns the difference between two vectors.
local a = Vector.xy(2, 3)
local b = Vector.xy(-1, 5)
local c = a - b -- 3 -2
__mul
Returns the vector scaled by the given number.
local v = Vector.xy(3, -2)
local doubled = v * 2 -- 6 -4
__div
Returns the vector divided by the given number.
local v = Vector.xy(6, -4)
local half = v / 2 -- 3 -2
distance
Deprecated - Use Vector.distance instead.
Returns the distance to the other vector.
Use Vector.distance(a, b) instead for better performance.
local a = Vector.xy(0, 0)
local b = Vector.xy(3, 4)
print(a:distance(b)) -- 5
distanceSquared
Deprecated - Use Vector.distanceSquared instead.
Returns the squared distance to the other vector.
Use Vector.distanceSquared(a, b) instead for better performance.
local a = Vector.xy(0, 0)
local b = Vector.xy(3, 4)
print(a:distanceSquared(b)) -- 25
dot
Deprecated - Use Vector.dot instead.
Returns the dot product of the vector and the other vector.
Use Vector.dot(a, b) instead for better performance.
local a = Vector.xy(1, 2)
local b = Vector.xy(3, 4)
print(a:dot(b)) -- 11 (1*3 + 2*4)
lerp
Deprecated - Use Vector.lerp instead.
Returns the linear interpolation between the vector and the other
vector, using t where 0 returns the vector and 1 returns the other.
Use Vector.lerp(a, b, t) instead for better performance.
local a = Vector.xy(0, 0)
local b = Vector.xy(10, 0)
local p = a:lerp(b, 0.5) -- 5 0