vectorAdded by Luau 0.650.
Changed by Luau: vector.create
Luau
vector.createvector is a built-in value type for positions, directions and other groups of three numbers. Like numbers, vectors are plain values: creating one never allocates, and two vectors with the same components are equal. Read the components through x, y and z or their uppercase forms. Vectors are immutable: assigning to a component raises an error.
+, -, *, / and // work on each component, and *, / and // also take a number on either side. The vector library creates vectors and provides length, normalization, dot and cross products, angles, rounding and interpolation.
const position = vector.create(1, 2, 3)
const moved = position * 2 + vector.one
print(moved.x)
print(`{moved}`)
print(moved == vector.create(3, 5, 7))3 3, 5, 7 true
Summary
Creating
vector.zero: vectorvector.zeroAdded by Luau 0.650.
Luau- Holds a vector with every component set to 0
vector.one: vectorvector.oneAdded by Luau 0.650.
Luau- Holds a vector with every component set to 1
function vector.create(x: number, y: number, z: number?): vectorvector.createAdded by Luau 0.650.
Changed by Luau 0.657: The z component is optional and defaults to 0.
Luau- Creates a vector from its components
Length and direction
function vector.magnitude(vec: vector): numbervector.magnitudeAdded by Luau 0.650.
Luau- Returns the length of a vector
function vector.normalize(vec: vector): vectorvector.normalizeAdded by Luau 0.650.
Luau- Returns the unit vector pointing the same way as a vector
Products and angles
function vector.cross(vec1: vector, vec2: vector): vectorvector.crossAdded by Luau 0.650.
Luau- Returns the cross product of two vectors
function vector.dot(vec1: vector, vec2: vector): numbervector.dotAdded by Luau 0.650.
Luau- Returns the dot product of two vectors
function vector.angle(vec1: vector, vec2: vector, axis: vector?): numbervector.angleAdded by Luau 0.650.
Luau- Returns the angle between two vectors in radians
Component by component
function vector.floor(vec: vector): vectorvector.floorAdded by Luau 0.650.
Luau- Applies math.floor to every component
function vector.ceil(vec: vector): vectorvector.ceilAdded by Luau 0.650.
Luau- Applies math.ceil to every component
function vector.abs(vec: vector): vectorvector.absAdded by Luau 0.650.
Luau- Applies math.abs to every component
function vector.sign(vec: vector): vectorvector.signAdded by Luau 0.650.
Luau- Applies math.sign to every component
function vector.clamp(vec: vector, min: vector, max: vector): vectorvector.clampAdded by Luau 0.650.
Luau- Clamps every component to the matching components of min and max
function vector.max(first: vector, ...: vector): vectorvector.maxAdded by Luau 0.650.
Luau- Returns the largest of each component across the arguments
function vector.min(first: vector, ...: vector): vectorvector.minAdded by Luau 0.650.
Luau- Returns the smallest of each component across the arguments
Interpolation
function vector.lerp(vec1: vector, vec2: vector, t: number): vectorvector.lerpAdded by Luau 0.688.
Luau- Interpolates linearly between two vectors
Creating
vector.zeroAdded by Luau 0.650.
Luau
vector.zero: vectorA vector with every component set to 0, including the fourth in 4-wide mode.
vector.oneAdded by Luau 0.650.
Luau
vector.one: vectorA vector with every component set to 1, including the fourth in 4-wide mode.
vector.createAdded by Luau 0.650.
Changed by Luau 0.657: The z component is optional and defaults to 0.
Luau
function vector.create(x: number, y: number, z: number?): vectorCreates a vector from its components. z defaults to 0. In 4-wide mode it takes a fourth argument for w that also defaults to 0.
Upstream Luau's library reference lists z as required. Luwu's type checker and VM both accept two arguments.
Length and direction
vector.magnitudeAdded by Luau 0.650.
Luau
function vector.magnitude(vec: vector): numberReturns the length of the vector, including the fourth component in 4-wide mode.
vector.normalizeAdded by Luau 0.650.
Luau
function vector.normalize(vec: vector): vectorReturns the unit vector pointing the same way as vec. It includes the fourth component in 4-wide mode. Normalizing vector.zero gives NaN in every component.
Products and angles
vector.crossAdded by Luau 0.650.
Luau
function vector.cross(vec1: vector, vec2: vector): vectorReturns the cross product of two vectors. In 4-wide mode it ignores the fourth component and sets it to 0 in the result.
vector.dotAdded by Luau 0.650.
Luau
function vector.dot(vec1: vector, vec2: vector): numberReturns the dot product of two vectors, including the fourth component in 4-wide mode.
vector.angleAdded by Luau 0.650.
Luau
function vector.angle(vec1: vector, vec2: vector, axis: vector?): numberReturns the angle between two vectors in radians, from 0 to π. When axis is given, the angle is negative if the cross product of vec1 and vec2 points away from axis. The fourth component is ignored in 4-wide mode.
const right = vector.create(1, 0, 0)
const up = vector.create(0, 1, 0)
print(vector.angle(right, up))
print(vector.angle(right, up, vector.create(0, 0, -1)))1.5707963267948966 -1.5707963267948966
Component by component
vector.floorAdded by Luau 0.650.
Luau
function vector.floor(vec: vector): vectorApplies math.floor to every component.
vector.ceilAdded by Luau 0.650.
Luau
function vector.ceil(vec: vector): vectorApplies math.ceil to every component.
vector.absAdded by Luau 0.650.
Luau
function vector.abs(vec: vector): vectorApplies math.abs to every component.
vector.signAdded by Luau 0.650.
Luau
function vector.sign(vec: vector): vectorApplies math.sign to every component: -1 for negative, 0 for zero and 1 for positive.
vector.clampAdded by Luau 0.650.
Luau
function vector.clamp(vec: vector, min: vector, max: vector): vectorApplies math.clamp to every component of vec using the matching components of min and max. It throws when a component of min is greater than the same component of max.
vector.maxAdded by Luau 0.650.
Luau
function vector.max(first: vector, ...: vector): vectorReturns a vector whose every component is the largest of that component across the arguments. It takes at least one vector.
const a = vector.create(1, 5, 2)
const b = vector.create(4, 0, 3)
print(`{vector.max(a, b)}`)
print(`{vector.min(a, b)}`)4, 5, 3 1, 0, 2
vector.minAdded by Luau 0.650.
Luau
function vector.min(first: vector, ...: vector): vectorReturns a vector whose every component is the smallest of that component across the arguments. It takes at least one vector.
Interpolation
vector.lerpAdded by Luau 0.688.
Luau
function vector.lerp(vec1: vector, vec2: vector, t: number): vectorInterpolates linearly from vec1 to vec2: it computes vec1 + (vec2 - vec1) * t for each component. When t is exactly 1 the result is exactly vec2. A t outside 0 to 1 extrapolates past the ends.
const start = vector.zero
const finish = vector.create(10, 20, 40)
print(`{vector.lerp(start, finish, 0.25)}`)2.5, 5, 10
Upstream Luau's library reference doesn't list vector.lerp yet.