Skip to Content
Kinemium
Datatypes

Vectors and rotations

Vector2, Vector3, their 16-bit variants, and Quaternion.

Vectors store components and support the arithmetic you expect. The float versions are used for positions, sizes, directions, and UVs; the int16 versions are compact integer pairs and triples.

Vector3

Vector3.new(x?, y?, z?) creates a vector. All three components default to 0.

local a = Vector3.new(1, 2, 3)
local b = Vector3.new(0, 1, 0)
print(a:Dot(b)) -- 2
print(a:Cross(b))
print(a.Unit)
MemberTypeDescription
x, XnumberX component.
y, YnumberY component.
z, ZnumberZ component.
MagnitudenumberLength of the vector.
UnitVector3Vector scaled to length 1.
MethodReturnsDescription
Abs()Vector3Absolute value of each component.
Ceil()Vector3Rounds each component up.
Floor()Vector3Rounds each component down.
Sign()Vector3Sign of each component.
Cross(other)Vector3Cross product with other.
Angle(other, axis?)numberAngle in radians. Pass an axis to get a signed angle.
Dot(other)numberDot product with other.
FuzzyEq(other, epsilon?)booleanComponent-wise comparison. epsilon defaults to 1e-5.
Lerp(goal, alpha)Vector3Interpolates toward goal.
Max(other)Vector3Larger component from each vector.
Min(other)Vector3Smaller component from each vector.

Static constructors:

CallResult
Vector3.FromNormalId(Enum.NormalId)Unit vector for a face direction.
Vector3.FromAxis(Enum.Axis)Unit vector for an axis.

Arithmetic supports +, -, unary -, ==, and * and / for scaling by a number.

Vector2

Vector2.new(x?, y?) creates a 2D vector. Both components default to 0.

MemberTypeDescription
x, XnumberX component.
y, YnumberY component.
MagnitudenumberLength of the vector.
UnitVector2Vector scaled to length 1.
MethodReturnsDescription
Cross(other)number2D cross product.
Dot(other)numberDot product.
FuzzyEq(other, epsilon?)booleanComponent-wise comparison. epsilon defaults to 1e-5.
Lerp(goal, alpha)Vector2Interpolates toward goal.

Arithmetic supports +, -, unary -, ==, and * and / for scaling by a number.

Vector3int16

Vector3int16.new(x?, y?, z?) stores three signed 16-bit integers. Components default to 0 and must be between -32768 and 32767; anything outside that range raises an error.

MemberTypeDescription
XnumberX component.
YnumberY component.
ZnumberZ component.

Supports +, -, and ==.

Vector2int16

Vector2int16.new(x?, y?) stores two signed 16-bit integers with the same range and default rules as Vector3int16.

MemberTypeDescription
XnumberX component.
YnumberY component.

Supports +, -, and ==.

Quaternion

Quaternion.new(x?, y?, z?, w?) creates a quaternion. x, y, and z default to 0; w defaults to 1, which is the identity rotation.

local turn = Quaternion.fromAxisAngle(Vector3.new(0, 1, 0), math.rad(90))
print(turn:ToAxisAngle())
MemberTypeDescription
X, Y, Z, WnumberQuaternion components.
MagnitudenumberLength of the quaternion.
UnitQuaternionNormalized quaternion.
MethodReturnsDescription
Conjugate()QuaternionConjugate of the quaternion.
Inverse()QuaternionInverse rotation.
Dot(other)numberDot product with another quaternion.
Lerp(other, alpha)QuaternionLinear interpolation. alpha is clamped to 0 through 1.
Slerp(other, alpha)QuaternionSpherical interpolation. alpha is clamped to 0 through 1.
ToAxisAngle()(Vector3, number)Rotation axis and angle in radians.

Quaternion.fromAxisAngle(axis, angle) builds a rotation around axis. Multiplying two quaternions with * combines their rotations, and == compares components.

See also

On this page