vec_math.lua - Outerra/anteworld GitHub Wiki

This is tutorial on functions available in vecmath.js, that can be used when the library is included into a lua script.

It is located in bin/lib/lua folder.

Vec3 functions

vec3.add(v1, v2)

Description: Adds two 3D vectors component-wise.

Parameters:

  • v1 (vec3): The first vector.
  • v2 (vec3): The second vector.

Returns: A new vector representing the sum of v1 and v2.


vec3.sub(v1, v2)

Description: Subtracts one 3D vector from another component-wise.

Parameters:

  • v1 (vec3): The first vector.
  • v2 (vec3): The second vector.

Returns: A new vector representing the difference between v1 and v2.


vec3.mul(v, s)

Description: Multiplies a 3D vector by a scalar.

Parameters:

  • v (vec3): The vector.
  • s (number): The scalar.

Returns: A new vector representing the scaled version of v.


vec3.div(v, s)

Description: Divides a 3D vector by a scalar.

Parameters:

  • v (vec3): The vector.
  • s (number): The scalar.

Returns: A new vector representing the scaled-down version of v.


vec3.dot(v1, v2)

Description: Computes the dot product of two 3D vectors.

Parameters:

  • v1 (vec3): The first vector.
  • v2 (vec3): The second vector.

Returns: A number representing the dot product of v1 and v2.


vec3.len2(v)

Description: Computes the squared length (magnitude) of a 3D vector.

Parameters:

  • v (vec3): The vector.

Returns: A number representing the squared magnitude of v.


vec3.len(v)

Description: Computes the length (magnitude) of a 3D vector.

Parameters:

  • v (vec3): The vector.

Returns: A number representing the magnitude of v.


vec3.normalize(v)

Description: Normalizes a 3D vector, making its length 1 while maintaining its direction.

Parameters:

  • v (vec3): The vector.

Returns: A new unit vector in the same direction as v.


vec3.cross(v1, v2)

Description: Computes the cross product of two 3D vectors.

Parameters:

  • v1 (vec3): The first vector.
  • v2 (vec3): The second vector.

Returns: A new vector representing the cross product of v1 and v2.


vec3.to_string(v)

Description: Returns a string representation of a 3D vector.

Parameters:

  • v (vec3): The vector.

Returns: A string formatted as "vec3(x = X, y = Y, z = Z)".

Quaternion functions

quat.mul(q1, q2)

Description: Multiplies two quaternions.

Parameters:

  • q1 (Quaternion): The first quaternion.
  • q2 (Quaternion): The second quaternion.

Returns: A new quaternion representing the product of q1 and q2.


quat.mul_scal(q, s)

Description: Scales a quaternion by a scalar value.

Parameters:

  • q (Quaternion): The quaternion.
  • s (number): The scalar value.

Returns: A new quaternion with each component of q scaled by s.


quat.div_scal(q, s)

Description: Scales a quaternion by the inverse of a scalar value.

Parameters:

  • q (Quaternion): The quaternion.
  • s (number): The scalar value.

Returns: A new quaternion with each component of q divided by s.


quat.add(q1, q2)

Description: Adds two quaternions component-wise.

Parameters:

  • q1 (Quaternion): The first quaternion.
  • q2 (Quaternion): The second quaternion.

Returns: A new quaternion representing the sum of q1 and q2.


quat.sub(q1, q2)

Description: Subtracts one quaternion from another component-wise.

Parameters:

  • q1 (Quaternion): The first quaternion.
  • q2 (Quaternion): The second quaternion.

Returns: A new quaternion representing the subtraction of q1 and q2.


quat.negate(q)

Description: Negates all components of a quaternion.

Parameters:

  • q (Quaternion): The quaternion.

Returns: A new quaternion with all components inverted.


quat.dot(q1, q2)

Description: Computes the dot product of two quaternions.

Parameters:

  • q1 (Quaternion): The first quaternion.
  • q2 (Quaternion): The second quaternion.

Returns: A number representing the dot product of q1 and q2.


quat.rotate(v, q)

Description: Rotates a vector using a quaternion.

Parameters:

  • v (vec3): The vector to rotate, represented as {x, y, z}.
  • q (Quaternion): The quaternion representing the rotation.

Returns: A new vector {x, y, z} after applying the rotation.


quat.len(q)

Description: Computes the magnitude of a quaternion.

Parameters:

  • q (Quaternion): The quaternion.

Returns: The length of q.


quat.conjugate(q)

Description: Computes the conjugate of a quaternion.

Parameters:

  • q (Quaternion): The quaternion.

Returns: A new quaternion representing the conjugate of q.


quat.normalize(q)

Description: Returns a unit quaternion with the same orientation as q.

Parameters:

  • q (Quaternion): The quaternion.

Returns: A new quaternion that is normalized.


quat.inverse(q)

Description: Computes the inverse of a quaternion.

Parameters:

  • q (Quaternion): The quaternion.

Returns: A new quaternion representing the inverse of q.


quat.to_string(q)

Description: Converts a quaternion into a readable string format.

Parameters:

  • q (Quaternion): The quaternion.

Returns: A string representation of the quaternion.