Module quat
quat: A simple 2-component vector
Functions
quat (x, y, z, w) | Create a new quat with components x, y, z, w: (Can also be used to duplicate: quat(v)) |
copy (v) | Create a copy of a vector: |
quat:set (x, y, z, w) | Set the components of a vector: |
fromRotor (v1, v2) | derive quaternion as absolute difference between two unit vectors v1 and v2 must be normalized. |
quat:mul (q) | Multiply a quaternion If q is a scalar, it scales all components. |
quat:mulnew (q) | Multiply two quaternions If q is a scalar, it scales all components. |
divnew (a, b) | Multiply two quaternions If q is a scalar, it scales all components. |
quat:normalize () | set the length of the vector to 1 (unit vector) (randomized direction if self length was zero) |
quat:normalizenew () | return a normalized copy of the vector (randomized direction if self length was zero) |
quat:length () | return the length of a vector (Can also use #vector) |
quat:dot (v) | return the dot product of two quaternions: |
rotate (q, v) | Rotate a vector: q must be a normalized quaternion |
unrotate (q, v) | Rotate a vector out of a quaternion: equiv. |
Functions
- quat (x, y, z, w)
-
Create a new quat with components x, y, z, w:
(Can also be used to duplicate: quat(v))
Parameters:
- x number or vector (optional, default 0)
- y number (optional, default 0)
- z number (optional, default 0)
- w number (optional, default 1)
- copy (v)
-
Create a copy of a vector:
Parameters:
- v vector
- quat:set (x, y, z, w)
-
Set the components of a vector:
Parameters:
- x component
- y component
- z component
- w component
Returns:
-
self
- fromRotor (v1, v2)
-
derive quaternion as absolute difference between two unit vectors
v1 and v2 must be normalized. the order of v1,v2 is not important;
v1 and v2 define a plane orthogonal to a rotational axis
the rotation around this axis increases as v1 and v2 diverge
alternatively expressed as Q = (1+gp(v1, v2))/sqrt(2*(1+dot(b, a)))
Parameters:
- v1
- v2 get the normal to the plane (i.e. the unit bivector containing the v1 and v2) normalize because the cross product can get slightly denormalized
- quat:mul (q)
-
Multiply a quaternion
If q is a scalar, it scales all components. If q is a quaternion, it applies quaternion rotation.
Parameters:
- q number or quaternion to mul
Returns:
-
self
- quat:mulnew (q)
-
Multiply two quaternions
If q is a scalar, it scales all components. If q is a quaternion, it applies quaternion rotation.
Parameters:
- q number or quaternion to mul
Returns:
-
new quaternion
- divnew (a, b)
-
Multiply two quaternions
If q is a scalar, it scales all components. If q is a quaternion, it applies conjugate quaternion rotation.
Parameters:
- a number or quaternion to divide
- b number or quaternion to divide by
Returns:
-
new quaternion
- quat:normalize ()
-
set the length of the vector to 1 (unit vector)
(randomized direction if self length was zero)
Returns:
-
self
- quat:normalizenew ()
-
return a normalized copy of the vector
(randomized direction if self length was zero)
Returns:
-
vector of length 1 (unit vector)
- quat:length ()
-
return the length of a vector
(Can also use #vector)
Returns:
-
length
- quat:dot (v)
-
return the dot product of two quaternions:
Parameters:
- v quaternion argument
Returns:
-
dot product
- rotate (q, v)
-
Rotate a vector:
q must be a normalized quaternion
Parameters:
- q qv = vec4(v, 0) -- 'pure quaternion' derived from vector return ((q * qv) * q^-1).xyz reduced to 24 multiplies and 17 additions
- v
- unrotate (q, v)
-
Rotate a vector out of a quaternion:
equiv. quat_rotate(quat_conj(q), v):
q must be a normalized quaternion
Parameters:
- q reduced
- v