Library
vector
A library of vector functions.
About vector
This library implements functionality for the vector type in addition to the
built-in primitive operator support. It uses vectors with three components
(x, y, and z).
Individual vector components can be accessed using the fields x or X, y
or Y, z or Z. Since vector values are immutable, writing to individual
components is not supported.
Properties 2
zero
: vectorConstant vector with all components set to0.A pre-allocated constant equivalent to
vector.create(0, 0, 0). Useful as a default value or additive identity in vector arithmetic without allocating a new vector each time.one
: vectorConstant vector with all components set to1.A pre-allocated constant equivalent to
vector.create(1, 1, 1). Useful as a multiplicative identity or scale factor in vector arithmetic without allocating a new vector each time.
Functions 14
create
(x: number, y: number, z: number) → vectorCreates a new vector with the given component values.Creates a new vector with the given component values. The
zparameter defaults to0when omitted, sovector.create(1, 2)is equivalent tovector.create(1, 2, 0).magnitude
(vec: vector) → numberCalculates the magnitude of a given vector.Returns the magnitude (Euclidean length) of the vector, computed as
math.sqrt(vec.x^2 + vec.y^2 + vec.z^2).normalize
(vec: vector) → vectorComputes the normalized version (unit vector) of a given vector.Returns a unit vector (magnitude of 1) pointing in the same direction as the input. Computed as
vec / vector.magnitude(vec). If the input vector has zero magnitude, the result containsnancomponents.cross
(vec1: vector, vec2: vector) → vectorComputes the cross product of two vectors.Returns the cross product of two vectors, which is a vector perpendicular to both inputs whose magnitude equals the area of the parallelogram they span. The result is computed as
vector.create(vec1.y*vec2.z - vec1.z*vec2.y, vec1.z*vec2.x - vec1.x*vec2.z, vec1.x*vec2.y - vec1.y*vec2.x).dot
(vec1: vector, vec2: vector) → numberComputes the dot product of two vectors.Returns the dot product (scalar product) of two vectors, computed as
vec1.x*vec2.x + vec1.y*vec2.y + vec1.z*vec2.z. The result is a single number equal to the product of the magnitudes of the two vectors times the cosine of the angle between them.angle
(vec1: vector, vec2: vector, axis: vector?) → numberComputes the angle between two vectors in radians.Computes the angle between two vectors in radians. The axis, if specified, is used to determine the sign of the angle.
floor
(vec: vector) → vectorAppliesmath.floorto every component of the input vector.Returns a new vector with each component rounded down to the nearest integer, equivalent to
vector.create(math.floor(vec.x), math.floor(vec.y), math.floor(vec.z)).ceil
(vec: vector) → vectorAppliesmath.ceilto every component of the input vector.Returns a new vector with each component rounded up to the nearest integer, equivalent to
vector.create(math.ceil(vec.x), math.ceil(vec.y), math.ceil(vec.z)).abs
(vec: vector) → vectorAppliesmath.absto every component of the input vector.Returns a new vector with each component replaced by its absolute value, equivalent to
vector.create(math.abs(vec.x), math.abs(vec.y), math.abs(vec.z)).sign
(vec: vector) → vectorAppliesmath.signto every component of the input vector.Returns a new vector where each component is
1if the corresponding input component is positive,-1if negative, or0if zero. Equivalent tovector.create(math.sign(vec.x), math.sign(vec.y), math.sign(vec.z)). The behavior for negative zero andnanis consistent withmath.sign.clamp
(vec: vector, min: vector, max: vector) → vectorAppliesmath.clampto every component of the input vector.Returns a new vector with each component clamped between the corresponding components of
minandmax. Each component ofmaxmust be greater than or equal to the corresponding component ofmin; otherwise, an error is raised.lerp
(vec1: vector, vec2: vector, alpha: number) → vectorReturns a vector linearly interpolated between two vectors by a fractional alpha.Returns a vector linearly interpolated between two vectors (
vec1,vec2) by the fractionalpha. Note thatalphais not limited to the range[0, 1].max
(...: vector) → vectorAppliesmath.maxto the corresponding components of the input vectors.Returns a new vector where each component is the maximum of the corresponding components across all input vectors. Accepts a variadic number of vectors (at least one). When a component is
nanin one vector but a valid number in another, the valid number is used.min
(...: vector) → vectorAppliesmath.minto the corresponding components of the input vectors.Returns a new vector where each component is the minimum of the corresponding components across all input vectors. Accepts a variadic number of vectors (at least one). When a component is
nanin one vector but a valid number in another, the valid number is used.
Reference text from the Roblox Creator Documentation, licensed CC BY 4.0, reorganized by rbxtoolshub.com · Studio 0.743.0.7431133.