Vector API Reference¶
The Vector API provides comprehensive tools for creating and manipulating 2D and 3D vectors. These APIs are essential for geometric calculations, physics simulations, and spatial transformations.
Vector2D¶
A 2D vector represents a point or direction in two-dimensional space with X and Y coordinates.
Create¶
Creates a new 2D vector instance.
Parameters¶
x(number): The X coordinate.y(number): The Y coordinate.
Returns Vector2D¶
- A new Vector2D object.
Example:¶
- Creates a vector at position (3, 4).
IsEqualTo¶
Checks if this vector is equal to another vector.
Parameters¶
otherVector(Vector2D): Another vector to compare.
Returns boolean¶
- True if both X and Y components are equal.
Example:¶
- Prints
truesince both vectors have identical coordinates.
GetXY¶
Returns the X and Y coordinates of the vector.
Returns number, number¶
- The X and Y components.
Example:¶
- Returns x=3, y=4.
SetXY¶
Sets the X and Y coordinates of the vector.
Parameters¶
x(number): The new X coordinate.y(number): The new Y coordinate.
Example:¶
- Updates the vector to position (5, 6).
ScaleBy¶
Scales the vector by a given factor, modifying it in place.
Parameters¶
scalar(number): The multiplier.
Example:¶
- Doubles the vector components: v.x=4, v.y=6.
DivideBy¶
Divides the vector by a given factor, modifying it in place.
Parameters¶
scalar(number): The divisor.
Example:¶
- Halves the vector components: v.x=2, v.y=3.
Add¶
Adds another vector to this vector, modifying it in place.
Parameters¶
other(Vector2D): The vector to add.
Example:¶
- Vector
anow contains (4, 6).
Subtract¶
Subtracts another vector from this vector, modifying it in place.
Parameters¶
other(Vector2D): The vector to subtract.
Example:¶
- Vector
anow contains (3, 3).
Cross¶
Computes the 2D cross product (scalar) with another vector.
Parameters¶
other(Vector2D): Another vector.
Returns number¶
- The scalar cross product (x₁y₂ - y₁x₂).
Example:¶
- Prints
1, indicating perpendicular vectors.
Dot¶
Computes the dot product with another vector.
Parameters¶
other(Vector2D): Another vector.
Returns number¶
- The dot product.
Example:¶
- Prints
11(1×3 + 2×4).
IsZero¶
Checks if the vector is zero (0, 0).
Returns boolean¶
- True if both X and Y are 0.
Example:¶
- Prints
true.
GetLengthSquared¶
Returns the squared length of the vector. More efficient than GetLength when comparing distances.
Returns number¶
- Squared magnitude (x² + y²).
Example:¶
- Prints
25(3² + 4²).
GetLength¶
Returns the length (magnitude) of the vector.
Returns number¶
- Magnitude √(x² + y²).
Example:¶
- Prints
5.
Normalize¶
Normalizes the vector to unit length (length = 1), modifying it in place.
Example:¶
- Vector
vnow has length 1: v.x=0.6, v.y=0.8.
AngleBetween¶
Returns the angle (in radians) between this vector and another.
Parameters¶
other(Vector2D): Another vector.
Returns number¶
- Angle in radians.
Example:¶
local a = _A.Vector2D:Create(1, 0)
local b = _A.Vector2D:Create(0, 1)
print(a:AngleBetween(b)) -- (1)!
- Prints approximately
1.5708(π/2 radians or 90 degrees).
RotateDirection¶
Rotates the vector by a given angle in radians, modifying it in place.
Parameters¶
rotationRadians(number): The rotation angle in radians.
Example:¶
- Rotates the vector 90 degrees: v.x≈0, v.y≈1.
Clone¶
Returns a clone of the vector.
Returns Vector2D¶
- A new vector with the same X and Y values.
Example:¶
- Creates an independent copy of the vector.
Vector2D Operators¶
Vector2D supports standard arithmetic operators for convenient mathematical operations.
tostring¶
Converts the vector to a string representation.
Example:¶
- Prints
Vector2D(3.000, 4.000).
Addition (+)¶
Returns a new vector representing the sum of two vectors.
Example:¶
local a = _A.Vector2D:Create(1, 2)
local b = _A.Vector2D:Create(3, 4)
local c = a + b -- (1)!
print(c) -- (2)!
- Creates a new vector without modifying
aorb. - Prints
Vector2D(4.000, 6.000).
Subtraction (-)¶
Returns a new vector representing the difference of two vectors.
Example:¶
local a = _A.Vector2D:Create(5, 6)
local b = _A.Vector2D:Create(2, 3)
local c = a - b -- (1)!
print(c) -- (2)!
- Creates a new vector: c = (3, 3).
- Prints
Vector2D(3.000, 3.000).
Multiplication (*)¶
Returns a new vector scaled by a scalar value.
Example:¶
- Doubles the vector components.
- Prints
Vector2D(4.000, 6.000).
Division (/)¶
Returns a new vector divided by a scalar value.
Example:¶
- Halves the vector components.
- Prints
Vector2D(2.000, 3.000).
Equality (==)¶
Compares two vectors for equality.
Example:¶
- Prints
truesince coordinates match.
Vector3D¶
A 3D vector represents a point or direction in three-dimensional space with X, Y, and Z coordinates.
Create¶
Creates a new 3D vector instance.
Parameters¶
x(number): The X coordinate.y(number): The Y coordinate.z(number): The Z coordinate.
Returns Vector3D¶
- A new Vector3D object.
Example:¶
- Creates a vector at position (1, 2, 3).
IsEqualTo¶
Checks if this vector is equal to another vector.
Parameters¶
otherVector(Vector3D): Another vector to compare.
Returns boolean¶
- True if X, Y, and Z components are equal.
Example:¶
local a = _A.Vector3D:Create(1, 2, 3)
local b = _A.Vector3D:Create(1, 2, 3)
print(a:IsEqualTo(b)) -- (1)!
- Prints
truesince all coordinates match.
GetXYZ¶
Returns the X, Y, and Z coordinates of the vector.
Returns number, number, number¶
- The X, Y, and Z components.
Example:¶
- Returns x=3, y=4, z=5.
SetXYZ¶
Sets the X, Y, and Z coordinates of the vector.
Parameters¶
x(number): The new X coordinate.y(number): The new Y coordinate.z(number): The new Z coordinate.
Example:¶
- Updates the vector to position (1, 2, 3).
ScaleBy¶
Scales the vector by a given factor, modifying it in place.
Parameters¶
scalar(number): The multiplier.
Example:¶
- Doubles all components: v.x=2, v.y=4, v.z=6.
DivideBy¶
Divides the vector by a given factor, modifying it in place.
Parameters¶
scalar(number): The divisor.
Example:¶
- Halves all components: v.x=1, v.y=2, v.z=3.
Add¶
Adds another vector to this vector, modifying it in place.
Parameters¶
other(Vector3D): The vector to add.
Example:¶
- Vector
anow contains (5, 7, 9).
Subtract¶
Subtracts another vector from this vector, modifying it in place.
Parameters¶
other(Vector3D): The vector to subtract.
Example:¶
- Vector
anow contains (4, 5, 6).
Cross¶
Computes the 3D cross product with another vector, modifying this vector in place.
Parameters¶
other(Vector3D): Another vector.
Example:¶
- Vector
anow contains (0, 0, 1), perpendicular to both original vectors.
Dot¶
Computes the dot product with another vector.
Parameters¶
other(Vector3D): Another vector.
Returns number¶
- The dot product.
Example:¶
- Prints
32(1×4 + 2×5 + 3×6).
GetLengthSquared¶
Returns the squared length of the vector. More efficient than GetLength when comparing distances.
Returns number¶
- Squared magnitude (x² + y² + z²).
Example:¶
- Prints
9(1² + 2² + 2²).
GetLength¶
Returns the length (magnitude) of the vector.
Returns number¶
- Magnitude √(x² + y² + z²).
Example:¶
- Prints
3.
Normalize¶
Normalizes the vector to unit length (length = 1), modifying it in place.
Example:¶
- Vector
vnow has length 1: v.x≈0.333, v.y≈0.667, v.z≈0.667.
Clone¶
Returns a clone of the vector.
Returns Vector3D¶
- A new vector with the same X, Y, and Z values.
Example:¶
- Creates an independent copy of the vector.
AddVector¶
Adds two vectors and returns a new Vector3D without modifying the originals.
Parameters¶
other(Vector3D): The vector to add.
Returns Vector3D¶
- A new vector representing the sum.
Example:¶
local a = _A.Vector3D:Create(1, 1, 1)
local b = _A.Vector3D:Create(2, 2, 2)
local c = a:AddVector(b) -- (1)!
print(c:GetXYZ()) -- (2)!
- Creates a new vector without modifying
aorb. - Prints
3, 3, 3.
SubtractVector¶
Subtracts two vectors and returns a new Vector3D without modifying the originals.
Parameters¶
other(Vector3D): The vector to subtract.
Returns Vector3D¶
- A new vector representing the difference.
Example:¶
local a = _A.Vector3D:Create(3, 3, 3)
local b = _A.Vector3D:Create(1, 1, 1)
local c = a:SubtractVector(b) -- (1)!
print(c:GetXYZ()) -- (2)!
- Creates a new vector: c = a - b.
- Prints
2, 2, 2.
NormalizeVector¶
Normalizes the vector and returns a new Vector3D without modifying the original.
Returns Vector3D¶
- A new normalized vector with length 1.
Example:¶
local v = _A.Vector3D:Create(1, 2, 2)
local n = v:NormalizeVector() -- (1)!
print(n:GetLength()) -- (2)!
- Original vector
vremains unchanged. - Prints
1.
ScaleVector¶
Scales the vector and returns a new Vector3D without modifying the original.
Parameters¶
scalar(number): The multiplier.
Returns Vector3D¶
- A new scaled vector.
Example:¶
- Original vector
vremains unchanged. - Prints
2, 4, 6.
NormalFromYawPitch¶
Returns a normal vector's coordinates from yaw and pitch angles.
Parameters¶
yaw(number): Rotation around the Z axis in radians.pitch(number): Rotation around the Y axis in radians.
Returns number, number, number¶
- The X, Y, Z coordinates of the normal vector.
Example:¶
- Computes the normal vector pointing in the specified direction.
NormalVectorFromYawPitch¶
Returns a Vector3D object from yaw and pitch angles.
Parameters¶
yaw(number): Rotation around the Z axis in radians.pitch(number): Rotation around the Y axis in radians.
Returns Vector3D¶
- A new normalized vector pointing in the specified direction.
Example:¶
- Creates a unit vector from angular coordinates.
YawPitchFromNormal¶
Computes yaw and pitch angles from X, Y, Z coordinates.
Parameters¶
x(number): The X coordinate.y(number): The Y coordinate.z(number): The Z coordinate.
Returns number, number¶
- The yaw and pitch in radians.
Example:¶
- Converts Cartesian coordinates to angular representation.
YawPitchFromVector¶
Computes yaw and pitch angles from the vector itself.
Returns number, number¶
- The yaw and pitch in radians.
Example:¶
- Extracts angular information from the vector's direction.
Common Use Cases¶
Distance Calculation¶
local pos1 = _A.Vector2D:Create(0, 0)
local pos2 = _A.Vector2D:Create(3, 4)
local diff = pos2 - pos1
local distance = diff:GetLength()
print(distance) -- (1)!
- Prints
5, the distance between the two points.
Direction Vector¶
local from = _A.Vector3D:Create(0, 0, 0)
local to = _A.Vector3D:Create(10, 0, 0)
local direction = to:SubtractVector(from)
direction:Normalize() -- (1)!
- Creates a unit vector pointing from
fromtoto.
Vector Projection¶
local a = _A.Vector2D:Create(3, 4)
local b = _A.Vector2D:Create(1, 0)
local projection = (a:Dot(b) / b:GetLengthSquared()) * b -- (1)!
- Projects vector
aonto vectorb.
Perpendicular Vector (2D)¶
- Rotates 90 degrees to get perpendicular vector.
- Prints approximately
0, 1.
Performance Tips¶
- Use
GetLengthSquared()instead ofGetLength()when comparing distances to avoid expensive square root calculations. - Use in-place methods (
Add,Subtract,Normalize) when you don't need to preserve the original vector. - Use immutable methods (
AddVector,SubtractVector,ScaleVector) when you need to keep the original vector unchanged. - Cache frequently used vectors instead of recreating them every frame.