Math¶
2D Vectors¶
- class vec2¶
This type represents a 2D vector. Most mathematical operators such as equality, addition, subtraction, multiplication and division are provided, so you can use
vec2data types similarly to how you use numerical types.- Parameters:
x (
number) – Initial x value of the vectory (
number) – Initial y value of the vector
- Syntax:
v = vec2(1, 2) v = vec2(1) -- set both x and y to 1 v = vec2() -- set both x and y to 0
- x: number¶
The x component of this vector
- y: number¶
The y component of this vector
- static min(v1, v2)¶
Return a
vec2containing the component-wise minimum of two vectors
- static max(v1, v2)¶
Return a
vec2containing the component-wise maximum of two vectors
- length: number¶
The length of this vector
- length2: number¶
The squared length of this vector
- normalize()¶
Normalize this vector in-place
- normalized() vec2¶
Return a normalized copy of this vector
- Returns:
A normalized copy of this vector
- Return type:
- dot(v) number¶
Perform a scalar dot product with another vector and return the result
- Parameters:
v (
vec2) – The other vector- Returns:
The scalar dot product
- Return type:
number
- distance(v) number¶
Calculate the Euclidean distance to another vector
- Parameters:
v (
vec2) – The other vector- Returns:
The distance between the two vectors
- Return type:
number
- distance2(v) number¶
Calculate the squared Euclidean distance to another vector
- Parameters:
v (
vec2) – The other vector- Returns:
The squared distance between the two vectors
- Return type:
number
- reflect(normal) vec2¶
Reflect this vector about a normal
- refract(normal, ior) vec2¶
Refract this vector through a surface with a given index of refraction
- lerp(v, t) vec2¶
Interpolate this vector with another by a given factor
- abs() vec2¶
Return a copy of this vector with component-wise absolute values
- Returns:
A copy with all components made positive
- Return type:
- unpack() number, number¶
Unpack this vector as individual number values
- Returns:
The x and y components as separate values
- cross(v) number¶
Compute the 2D cross product (perp dot product) with another vector
- Parameters:
v (
vec2) – The other vector- Returns:
The scalar cross product result
- Return type:
number
- rotate(angleRadians) vec2¶
Rotate this vector by a given angle in radians
- Parameters:
angleRadians (
number) – The angle of rotation in radians- Returns:
The rotated vector
- Return type:
3D Vectors¶
- class vec3¶
- static vec3(x, y, z)¶
Create a new
vec3by setting each component individually- Parameters:
x (
number) – The x componenty (
number) – The y componentz (
number) – The z component
- static vec3()
Create a new
vec3by setting all components to zero
- static vec3(v)
Create a new
vec3by setting all components to the same value- Parameters:
v (
number) – The x, y and z values
- static min(v1, v2)¶
Return a
vec3containing the component-wise minimum of two vectors
- static max(v1, v2)¶
Return a
vec3containing the component-wise maximum of two vectors
- x: number¶
The x component of this vector
- y: number¶
The y component of this vector
- z: number¶
The z component of this vector
- length: number¶
The length of this vector
- length2: number¶
The squared length of this vector
- normalize()¶
Normalize this vector in-place
- normalized() vec3¶
Return a normalized copy of this vector
- Returns:
A normalized copy of this vector
- Return type:
- dot(v) number¶
Perform a scalar dot product with another vector and return the result
- Parameters:
v (
vec3) – The other vector- Returns:
The scalar dot product
- Return type:
number
- cross(v) vec3¶
Perform a cross product with another
vec3and return the result
- distance(v) number¶
Calculate the Euclidean distance to another vector
- Parameters:
v (
vec3) – The other vector- Returns:
The distance between the two vectors
- Return type:
number
- distance2(v) number¶
Calculate the squared Euclidean distance to another vector
- Parameters:
v (
vec3) – The other vector- Returns:
The squared distance between the two vectors
- Return type:
number
- reflect(normal) vec3¶
Reflect this vector about a normal
- refract(normal, ior) vec3¶
Refract this vector through a surface with a given index of refraction
- lerp(v, t) vec3¶
Interpolate this vector with another by a given factor
- abs() vec3¶
Return a copy of this vector with component-wise absolute values
- Returns:
A copy with all components made positive
- Return type:
- unpack() number, number, number¶
Unpack this vector as individual number values
- Returns:
The x, y and z components as separate values
4D Vectors¶
- class vec4¶
- static vec4(x)¶
- static vec4(x, y, z, w)
Create a new
vec4by setting all components to the same value, or each one individually- Parameters:
x (
number) – The x component (also used for y, z and w when called with a single argument)y (
number) – The y componentz (
number) – The z componentw (
number) – The w component
- static min(v1, v2)¶
Return a
vec4containing the component-wise minimum of two vectors
- static max(v1, v2)¶
Return a
vec4containing the component-wise maximum of two vectors
- x: number¶
The x component of this vector
- y: number¶
The y component of this vector
- z: number¶
The z component of this vector
- w: number¶
The w component of this vector
- length: number¶
The length of this vector
- length2: number¶
The squared length of this vector
- normalize()¶
Normalize this vector in-place
- normalized() vec4¶
Return a normalized copy of this vector
- Returns:
A normalized copy of this vector
- Return type:
- dot(v) number¶
Perform a scalar dot product with another vector and return the result
- Parameters:
v (
vec4) – The other vector- Returns:
The scalar dot product
- Return type:
number
- distance(v) number¶
Calculate the Euclidean distance to another vector
- Parameters:
v (
vec4) – The other vector- Returns:
The distance between the two vectors
- Return type:
number
- distance2(v) number¶
Calculate the squared Euclidean distance to another vector
- Parameters:
v (
vec4) – The other vector- Returns:
The squared distance between the two vectors
- Return type:
number
- reflect(normal) vec4¶
Reflect this vector about a normal
- refract(normal, ior) vec4¶
Refract this vector through a surface with a given index of refraction
- lerp(v, t) vec4¶
Interpolate this vector with another by a given factor
- abs() vec4¶
Return a copy of this vector with component-wise absolute values
- Returns:
A copy with all components made positive
- Return type:
- unpack() number, number, number, number¶
Unpack this vector as individual number values
- Returns:
The x, y, z and w components as separate values
Quaternions¶
- class quat¶
- static quat()¶
- static quat(w, x, y, z)
Create a new
quat- Parameters:
w (
number) – The scalar (real) componentx (
number) – The x imaginary componenty (
number) – The y imaginary componentz (
number) – The z imaginary component
- static lookRotation(forward, up)¶
Create a rotation that points in the
forwarddirection, oriented usingup
- static fromToRotation(from, to)¶
Create a rotation that rotates from one direction to another
- static angleAxis(angle, axis)¶
Create a rotation of
angledegrees around anaxis
- static eulerAngles(x, y, z)¶
Create a rotation from euler angles (yaw, pitch, roll) in degrees
- Parameters:
x (
number) – The amount of rotation about the x axis (pitch) in degreesy (
number) – The amount of rotation about the y axis (yaw) in degreesz (
number) – The amount of rotation about the z axis (roll) in degrees
- Returns:
A new
quatrepresenting the combined rotation- Return type:
- x: number¶
The x imaginary component
- y: number¶
The y imaginary component
- z: number¶
The z imaginary component
- w: number¶
The scalar (real) component
- angles: vec3¶
A set of euler angles (in degrees) that produces the same rotation as this quaternion
Note: euler angles derived from a quaternion are ambiguous and should not be relied upon for smooth interpolation
- slerp(q, t) quat¶
Spherically interpolate between this quaternion and another
- conjugate() quat¶
Return the conjugate of this quaternion
- Returns:
A new
quatcontaining the conjugate (inverse rotation)- Return type:
- normalize()¶
Normalize this quaternion in-place
2x2 Matrix¶
- class mat2¶
A simple 2x2 matrix. Individual entries can be accessed via a 1-based index
m = mat2(1) -- init with diagonals set to 1 print(m[1]) -- prints '1.0'
- static mat2()¶
- static mat2(s)
- static mat2(v1, v2)
- static mat2(m11, m12, m21, m22)
Create a new
mat2: default (identity), diagonal scalar, twovec2columns, or all 4 entries
- inverse() mat2¶
Return the inverse of this matrix
- Returns:
The inverse of this matrix
- Return type:
- transpose() mat2¶
Return the transpose of this matrix
- Returns:
The transpose of this matrix
- Return type:
- determinant() number¶
Return the determinant of this matrix
- Returns:
The determinant
- Return type:
number
- row(index) vec2¶
Return the row at a given index
- Parameters:
index (
number) – The 1-based row index- Returns:
The row at the given index
- Return type:
3x3 Matrix¶
- class mat3¶
A simple 3x3 matrix. Individual entries can be accessed via a 1-based index
m = mat3(1) -- init with diagonals set to 1 print(m[1]) -- prints '1.0'
- static mat3()¶
- static mat3(s)
- static mat3(v1, v2, v3)
- static mat3(m11, m12, m31, ..., m33)
Create a new
mat3: default (identity), diagonal scalar, threevec3columns, or all 9 entries
- inverse() mat3¶
Return the inverse of this matrix
- Returns:
The inverse of this matrix
- Return type:
- transpose() mat3¶
Return the transpose of this matrix
- Returns:
The transpose of this matrix
- Return type:
- determinant() number¶
Return the determinant of this matrix
- Returns:
The determinant
- Return type:
number
- row(index) vec3¶
Return the row at a given index
- Parameters:
index (
number) – The 1-based row index- Returns:
The row at the given index
- Return type:
4x4 Matrix¶
- class mat4¶
A 4x4 matrix typically used for 3D homogeneous transformations. Individual entries can be accessed via a 1-based index
m = mat4(1) -- init with diagonals set to 1 print(m[1]) -- prints '1.0'
- static mat4()¶
- static mat4(s)
- static mat4(v1, v2, v3, v4)
- static mat4(m11, m12, m31, m41, ..., m44)
Create a new
mat4: default (identity), diagonal scalar, fourvec4columns, or all 16 entries
- static lookAt(eye, center, up)¶
Create a look-at view matrix
- static lookAt(matrix, eye, center, up)
Apply a look-at transform to an existing matrix
- static orbit(origin, distance, x, y)¶
Create an orbit view matrix centered on a point
- static orbit(matrix, origin, distance, x, y)
Apply an orbit transform to an existing matrix
- Parameters:
- Returns:
The transformed matrix
- Return type:
- static ortho(left, right, top, bottom[, near, far])¶
Create an orthographic projection matrix
- Parameters:
left (
number) – The left clipping planeright (
number) – The right clipping planetop (
number) – The top clipping planebottom (
number) – The bottom clipping planenear (
number) – The near clipping plane (optional)far (
number) – The far clipping plane (optional)
- Returns:
An orthographic projection matrix
- Return type:
- static perspective(fovy, aspect, near, far)¶
Create a perspective projection matrix
- Parameters:
fovy (
number) – The vertical field of view in degreesaspect (
number) – The aspect ratio (width / height)near (
number) – The near clipping plane distancefar (
number) – The far clipping plane distance
- Returns:
A perspective projection matrix
- Return type:
- static rotate(angle, axis)¶
Create a rotation matrix
- static rotate(matrix, angle, axis)
Apply a rotation transform to an existing matrix
- inverse() mat4¶
Return the inverse of this matrix
- Returns:
The inverse of this matrix
- Return type:
- transpose() mat4¶
Return the transpose of this matrix
- Returns:
The transpose of this matrix
- Return type:
- determinant() number¶
Return the determinant of this matrix
- Returns:
The determinant
- Return type:
number
- row(index) vec4¶
Return the row at a given index
- Parameters:
index (
number) – The 1-based row index- Returns:
The row at the given index
- Return type:
Axis-Aligned Bounding Box (AABB)¶
- class bounds.aabb¶
An axis-aligned bounding box defined by minimum and maximum corner points
- Parameters:
- Syntax:
b = bounds.aabb(vec3(-1, -1, -1), vec3(1, 1, 1))
- min: vec3¶
The minimum corner of this bounding box
- max: vec3¶
The maximum corner of this bounding box
- size: vec3¶
The size (width, height, depth) of this bounding box
- center: vec3¶
The center point of this bounding box
- set(min, max)¶
Set the minimum and maximum corners of this bounding box