Vector4
Represents a vector or a point in four-dimensional space, commonly used for RGBA values, 4D transforms, or 3D transformations with homogeneous coordinates.
Properties
Provides a normalized version of the Vector4, with a length of 1, useful for direction-only applications.
local v1 = Vector4.new(1, 2, 3, 4)
local v2 = v1.normalized
print(v2.x, v2.y, v2.z, v2.w) -- 0.1826, 0.3651, 0.5477, 0.7303
Provides the length of the Vector4, useful for distance calculations or scaling vectors.
local v1 = Vector4.new(1, 2, 3, 4)
print(v1.magnitude) -- 5.4772
Returns the square of the magnitude of the Vector4, a performance-friendly alternative for length comparisons.
local v1 = Vector4.new(1, 2, 3, 4)
print(v1.sqrMagnitude) -- 30
Accesses the 'x' component of the Vector4, representing different dimensions or the 'Red' component in RGBA.
local v1 = Vector4.new(1, 2, 3, 4)
print(v1.x) -- 1
Accesses the 'y' component of the Vector4, representing different dimensions or the 'Green' component in RGBA.
local v1 = Vector4.new(1, 2, 3, 4)
print(v1.y) -- 2
Accesses the 'z' component of the Vector4, representing different dimensions or the 'Blue' component in RGBA.
local v1 = Vector4.new(1, 2, 3, 4)
print(v1.z) -- 3
Accesses the 'w' component of the Vector4, representing different dimensions or the 'Alpha' component in RGBA.
local v1 = Vector4.new(1, 2, 3, 4)
print(v1.w) -- 4
Represents a Vector4 with all components set to zero.
local v1 = Vector4.zero
print(v1.x, v1.y, v1.z, v1.w) -- 0, 0, 0, 0
Represents a Vector4 with all components set to one, useful for certain mathematical operations.
local v1 = Vector4.one
print(v1.x, v1.y, v1.z, v1.w) -- 1, 1, 1, 1
Represents a Vector4 with all components set to positive infinity, useful for setting maximum limits.
local v1 = Vector4.positiveInfinity
print(v1.x, v1.y, v1.z, v1.w) -- inf, inf, inf, inf
Represents a Vector4 with all components set to negative infinity, useful for setting minimum limits.
local v1 = Vector4.negativeInfinity
print(v1.x, v1.y, v1.z, v1.w) -- -inf, -inf, -inf, -inf
Represents a tiny positive number close to zero, used to handle calculations related to precision.
local v1 = Vector4.kEpsilon
print(v1.x, v1.y, v1.z, v1.w) -- 1e-05, 1e-05, 1e-05, 1e-05
Methods
Sets the 'x', 'y', 'z', and 'w' components of the Vector4, enhancing code readability and efficiency.
local v1 = Vector4.new(1, 2, 3, 4)
v1:Set(5, 6, 7, 8)
print(v1.x, v1.y, v1.z, v1.w) -- 5, 6, 7, 8
Parameters
newX
The new 'x' component.
newY
The new 'y' component.
newZ
The new 'z' component.
newW
The new 'w' component.
Returns
Scales a Vector4 by another Vector4's components, useful for dimension or shape scaling.
local v1 = Vector4.new(1, 2, 3, 4)
local v2 = Vector4.new(2, 3, 4, 5)
v1:Scale(v2)
print(v1.x, v1.y, v1.z, v1.w) -- 2, 6, 12, 20
Parameters
scale
Vector4The Vector4 to scale by.
Returns
Transforms the Vector4 into a vector of same direction but with length 1, ideal for direction-only uses.
local v1 = Vector4.new(1, 2, 3, 4)
v1:Normalize()
print(v1.x, v1.y, v1.z, v1.w) -- 0.1826, 0.3651, 0.5477, 0.7303
Returns
Provides the square of the magnitude of the Vector4, an efficient method for length comparisons.
local v1 = Vector4.new(1, 2, 3, 4)
print(v1:SqrMagnitude()) -- 30
Returns
Returns the square of the magnitude.
Performs Linear Interpolation between two Vector4's based on a parameter 't', useful for smooth transitions.
local v1 = Vector4.new(1, 2, 3, 4)
local v2 = Vector4.new(5, 6, 7, 8)
local v3 = Vector4.Lerp(v1, v2, 0.5)
print(v3.x, v3.y, v3.z, v3.w) -- 3, 4, 5, 6
Similar to 'Lerp' but without clamping the 't' parameter, offering more freedom in interpolation.
local v1 = Vector4.new(1, 2, 3, 4)
local v2 = Vector4.new(5, 6, 7, 8)
local v3 = Vector4.LerpUnclamped(v1, v2, 2)
print(v3.x, v3.y, v3.z, v3.w) -- 9, 10, 11, 12
Moves a Vector4 'current' towards 'target', limited by 'maxDistanceDelta', useful for smooth object movement.
local v1 = Vector4.new(1, 2, 3, 4)
local v2 = Vector4.new(5, 6, 7, 8)
local v3 = Vector4.MoveTowards(v1, v2, 2)
print(v3.x, v3.y, v3.z, v3.w) -- 3, 4, 5, 6
Multiplies two Vector4's component-wise, useful for component-wise data manipulation.
local v1 = Vector4.new(1, 2, 3, 4)
local v2 = Vector4.new(2, 3, 4, 5)
local v3 = Vector4.Scale(v1, v2)
print(v3.x, v3.y, v3.z, v3.w) -- 2, 6, 12, 20
Normalizes a Vector4 and returns a new Vector4 with length 1, ideal for direction-only vectors.
local v1 = Vector4.new(1, 2, 3, 4)
local v2 = Vector4.Normalize(v1)
print(v2.x, v2.y, v2.z, v2.w) -- 0.1826, 0.3651, 0.5477, 0.7303
Computes the dot product of two Vector4's, reflecting their angular relationship.
local v1 = Vector4.new(1, 2, 3, 4)
local v2 = Vector4.new(5, 6, 7, 8)
local dotProduct = Vector4.Dot(v1, v2)
print(dotProduct) -- 70
Projects a Vector4 onto another Vector4, useful for finding the nearest point on a defined line.
local v1 = Vector4.new(1, 2, 3, 4)
local v2 = Vector4.new(5, 6, 7, 8)
local v3 = Vector4.Project(v1, v2)
print(v3.x, v3.y, v3.z, v3.w) -- 2.5, 3, 3.5, 4
Computes the distance between two Vector4's, useful for object spacing or collision detection.
local v1 = Vector4.new(1, 2, 3, 4)
local v2 = Vector4.new(5, 6, 7, 8)
local distance = Vector4.Distance(v1, v2)
print(distance) -- 8.0
Calculates the length of the Vector4, useful for finding the distance it spans in 4D space.
local v1 = Vector4.new(1, 2, 3, 4)
local magnitude = Vector4.Magnitude(v1)
print(magnitude) -- 5.4772
Parameters
The Vector4 to compute the magnitude for.
Returns
Returns the magnitude of the Vector4.
Returns a Vector4 with the smallest components of two Vector4's, useful for bounding box operations.
local v1 = Vector4.new(1, 2, 3, 4)
local v2 = Vector4.new(5, 6, 7, 8)
local v3 = Vector4.Min(v1, v2)
print(v3.x, v3.y, v3.z, v3.w) -- 1, 2, 3, 4
Returns a Vector4 with the largest components of two Vector4's, useful for limiting operations.
local v1 = Vector4.new(1, 2, 3, 4)
local v2 = Vector4.new(5, 6, 7, 8)
local v3 = Vector4.Max(v1, v2)
print(v3.x, v3.y, v3.z, v3.w) -- 5, 6, 7, 8
Gives the square of the magnitude of a Vector4, an efficient comparison method that avoids square root calculations.
local v1 = Vector4.new(1, 2, 3, 4)
local sqrMagnitude = Vector4.SqrMagnitude(v1)
print(sqrMagnitude) -- 30
Parameters
The Vector4 to square the magnitude of.
Returns
Returns the square of the Vector4's magnitude.