Module vec3
vec3: A simple 3-component vector
Functions
vec3 (x, y, z) | Create a new vector with components x, y, z: (Can also be used to duplicate a vector: vec3(v)) |
copy (v) | Create a copy of a vector: |
fromvec2 (v, z) | Create a copy of a vec2 vector: |
addnew (a, b) | Add two vectors (or numbers) to create a new vector |
subnew (a, b) | Subtract two vectors (or numbers) to create a new vector |
mulnew (a, b) | Multiply two vectors (or numbers) to create a new vector |
divnew (a, b) | Divide two vectors (or numbers) to create a new vector |
pownew (a, b) | Raise to power two vectors (or numbers) to create a new vector |
modnew (a, b) | Calculate modulo two vectors (or numbers) to create a new vector |
minnew (a, b) | Calculate minimum of elements to create a new vector |
maxnew (a, b) | Calculate maximum of elements to create a new vector |
lerpnew (a, b, f) | create a vector from the linear interpolation of two vectors: |
random (mag) | Create a new vector in a uniformly random direction: |
dot (a, b) | return the dot product of two vectors: |
cross (a, b) | return the cross product of two vectors: |
anglebetween (a, b) | The angle between two vectors (two points) (The relative angle from self to v) |
Class vec3
vec3:set (x, y, z) | Set the components of a vector: |
vec3:add (v) | Add a vector (or number) to self (in-place) |
vec3:sub (v) | Subtract a vector (or number) to self (in-place) |
vec3:mul (v) | Multiply a vector (or number) to self (in-place) |
vec3:div (v) | Divide a vector (or number) to self (in-place) |
vec3:pow (v) | Raise to power a vector (or number) to self (in-place) |
vec3:mod (v) | Calculate modulo a vector (or number) to self (in-place) |
vec3:floor () | Apply math.floor to all elements: |
vec3:min (v) | Calculate minimum of elements (in-place) |
vec3:max (v) | Calculate maximum of elements (in-place) |
vec3:clip (lo, hi) | Constrain vector to range to create a new vector |
vec3:relativewrap (dimx, dimy, dimz) | Determine shortest relative vector in a toroidal space |
vec3:relativewrapnew (dimx, dimy) | Create new vector as shortest relative vector in a toroidal space |
vec3:lerp (v, f) | interpolate from self to v by factor of f |
vec3:normalize () | set the length of the vector to 1 (unit vector) (randomized direction if self length was zero) |
vec3:normalizenew () | return a normalized copy of the vector (randomized direction if self length was zero) |
vec3:limit (maximum) | Impose a maximum magnitude Rescales vector if greater than maximum |
vec3:limitnew (maximum) | Create a copy of a vector, limited to a maximum magnitude Rescales vector if greater than maximum |
vec3:setmag (m) | Rescale a vector to a specific magnitude: |
vec3:setmagnew (m) | Return a vector copy rescaled to a specific magnitude: |
vec3:rotateXnew (angle) | Create a vector by rotating a vector by an angle |
vec3:rotateYnew (angle) | Create a vector by rotating a vector by an angle |
vec3:rotateZnew (angle) | Create a vector by rotating a vector by an angle |
vec3:randomize (mag) | Set to a vector of magnitude 1 in a uniformly random direction: |
vec3:length () | return the length of a vector (Can also use #vector) |
vec3:magSqr () | return the squared length of a vector |
vec3:distance (p) | The distance between two vectors (two points) (The relative distance from self to p) |
vec3:unpack () | Return the three components of the vector as numbers: |
Functions
- vec3 (x, y, z)
-
Create a new vector with components x, y, z:
(Can also be used to duplicate a vector: vec3(v))
Parameters:
- x number or vector (optional, default 0)
- y number (optional, default 0)
- z number (optional, default 0)
- copy (v)
-
Create a copy of a vector:
Parameters:
- v vector
- fromvec2 (v, z)
-
Create a copy of a vec2 vector:
Parameters:
- v vec3
- z number
- addnew (a, b)
-
Add two vectors (or numbers) to create a new vector
Parameters:
- a vector or number
- b vector or number
Returns:
-
new vector
- subnew (a, b)
-
Subtract two vectors (or numbers) to create a new vector
Parameters:
- a vector or number
- b vector or number
Returns:
-
new vector
- mulnew (a, b)
-
Multiply two vectors (or numbers) to create a new vector
Parameters:
- a vector or number
- b vector or number
Returns:
-
new vector
- divnew (a, b)
-
Divide two vectors (or numbers) to create a new vector
Parameters:
- a vector or number
- b vector or number
Returns:
-
new vector
- pownew (a, b)
-
Raise to power two vectors (or numbers) to create a new vector
Parameters:
- a vector or number
- b vector or number
Returns:
-
new vector
- modnew (a, b)
-
Calculate modulo two vectors (or numbers) to create a new vector
Parameters:
- a vector or number
- b vector or number
Returns:
-
new vector
- minnew (a, b)
-
Calculate minimum of elements to create a new vector
Parameters:
- a vector or number
- b vector or number
Returns:
-
new vector
- maxnew (a, b)
-
Calculate maximum of elements to create a new vector
Parameters:
- a vector or number
- b vector or number
Returns:
-
new vector
- lerpnew (a, b, f)
-
create a vector from the linear interpolation of two vectors:
Parameters:
- a vector
- b vector
- f interpolation factor from a to b (0 = none, 1 = full)
Returns:
-
new vector
- random (mag)
-
Create a new vector in a uniformly random direction:
Parameters:
- mag magnitude (optional, default 1)
Returns:
-
new vector
- dot (a, b)
-
return the dot product of two vectors:
Parameters:
- a vector
- b vector
Returns:
-
dot product
- cross (a, b)
-
return the cross product of two vectors:
Parameters:
- a vector
- b vector
Returns:
-
cross product
- anglebetween (a, b)
-
The angle between two vectors (two points)
(The relative angle from self to v)
Parameters:
- a vector to measure angle between
- b vector to measure angle between
Returns:
-
distance
Class vec3
A 3 component vector- vec3:set (x, y, z)
-
Set the components of a vector:
Parameters:
- x component
- y component
- z component
Returns:
-
self
- vec3:add (v)
-
Add a vector (or number) to self (in-place)
Parameters:
- v number or vector to add
Returns:
-
self
- vec3:sub (v)
-
Subtract a vector (or number) to self (in-place)
Parameters:
- v number or vector to sub
Returns:
-
self
- vec3:mul (v)
-
Multiply a vector (or number) to self (in-place)
Parameters:
- v number or vector to mul
Returns:
-
self
- vec3:div (v)
-
Divide a vector (or number) to self (in-place)
Parameters:
- v number or vector to div
Returns:
-
self
- vec3:pow (v)
-
Raise to power a vector (or number) to self (in-place)
Parameters:
- v number or vector to pow
Returns:
-
self
- vec3:mod (v)
-
Calculate modulo a vector (or number) to self (in-place)
Parameters:
- v number or vector to mod
Returns:
-
self
- vec3:floor ()
-
Apply math.floor to all elements:
Returns:
-
self
- vec3:min (v)
-
Calculate minimum of elements (in-place)
Parameters:
- v number or vector limit
Returns:
-
self
- vec3:max (v)
-
Calculate maximum of elements (in-place)
Parameters:
- v number or vector limit
Returns:
-
self
- vec3:clip (lo, hi)
-
Constrain vector to range to create a new vector
Parameters:
- lo vector or number minimum value
- hi vector or number minimum value
Returns:
-
new vector
- vec3:relativewrap (dimx, dimy, dimz)
-
Determine shortest relative vector in a toroidal space
Parameters:
- dimx width of space (optional, default 1)
- dimy height of space (optional, default dimx)
- dimz depth of space (optional, default dimx)
Returns:
-
self
- vec3:relativewrapnew (dimx, dimy)
-
Create new vector as shortest relative vector in a toroidal space
Parameters:
- dimx width of space (optional, default 1)
- dimy height of space (optional, default dimx)
Returns:
-
new vector
- vec3:lerp (v, f)
-
interpolate from self to v by factor of f
Parameters:
- v vector
- f interpolation factor from self to v (0 = none, 1 = full)
Returns:
-
self
- vec3:normalize ()
-
set the length of the vector to 1 (unit vector)
(randomized direction if self length was zero)
Returns:
-
self
- vec3:normalizenew ()
-
return a normalized copy of the vector
(randomized direction if self length was zero)
Returns:
-
vector of length 1 (unit vector)
- vec3:limit (maximum)
-
Impose a maximum magnitude
Rescales vector if greater than maximum
Parameters:
- maximum maximum magnitude of vector
Returns:
-
self
- vec3:limitnew (maximum)
-
Create a copy of a vector, limited to a maximum magnitude
Rescales vector if greater than maximum
Parameters:
- maximum maximum magnitude of vector
Returns:
-
new vector
- vec3:setmag (m)
-
Rescale a vector to a specific magnitude:
Parameters:
- m new magnitude
Returns:
-
self
- vec3:setmagnew (m)
-
Return a vector copy rescaled to a specific magnitude:
Parameters:
- m new magnitude
Returns:
-
new vector
- vec3:rotateXnew (angle)
-
Create a vector by rotating a vector by an angle
Parameters:
- angle in radians
Returns:
-
new vector
- vec3:rotateYnew (angle)
-
Create a vector by rotating a vector by an angle
Parameters:
- angle in radians
Returns:
-
new vector
- vec3:rotateZnew (angle)
-
Create a vector by rotating a vector by an angle
Parameters:
- angle in radians
Returns:
-
new vector
- vec3:randomize (mag)
-
Set to a vector of magnitude 1 in a uniformly random direction:
Parameters:
- mag magnitude (optional, default 1)
Returns:
-
self
- vec3:length ()
-
return the length of a vector
(Can also use #vector)
Returns:
-
length
- vec3:magSqr ()
-
return the squared length of a vector
Returns:
-
length
- vec3:distance (p)
-
The distance between two vectors (two points)
(The relative distance from self to p)
Parameters:
- p target to measure distance to
Returns:
-
distance
- vec3:unpack ()
- Return the three components of the vector as numbers: