Skip to main content

Vec3

Associated namespace: sm.vec3

A userdata object representing a 3D vector.

Operations:
OperationDescription
Vec3 + Vec3Returns the sum of two vectors.
Vec3 / Vec3Returns the quotient of two vectors, dividing element by element.
Vec3 / numberReturns the quotient of a vector and a scalar.
Vec3 == Vec3Checks if two vectors are equal.
Vec3 < Vec3Returns whether the first vector is "less than" the second.
Vec3 * Vec3Returns the product of two vectors, multiplying element by element.
Vec3 * numberReturns the product of a vector and a scalar.
Vec3 - Vec3Returns the difference of two vectors.
tostring( Vec3 )Returns the string representation of a vector.
-Vec3Returns the negated vector.
Values:
  • x [ number ]

    • Get: The vector's X value.
    • Set: Sets the vector's X value.
  • y [ number ]

    • Get: The vector's Y value.
    • Set: Sets the vector's Y value.
  • z [ number ]

    • Get: The vector's Z value.
    • Set: Sets the vector's Z value.

Functions

cross

vec3:cross( vector )

Returns the cross product of two vectors.

Arguments:
  • vec3 [ Vec3 ]: The vector.
  • vector [ Vec3 ]: The second vector.
Returns:
  • [ Vec3 ]: The cross product.

dot

vec3:dot( vector )

Returns the dot product of two vectors.

Arguments:
  • vec3 [ Vec3 ]: The vector.
  • vector [ Vec3 ]: The second vector.
Returns:

length

vec3:length( vector )

Returns the length of the vector.

If you want the squared length, using length2 is faster than squaring the result of this function.

Arguments:
Returns:
  • [ number ]: The length.

length2

vec3:length2( vector )

Returns the squared length of the vector.

Arguments:
Returns:
  • [ number ]: The squared length.

max

vec3:max( vector )

Returns the maximum value between two vectors components.

Arguments:
  • vec3 [ Vec3 ]: The vector.
  • vector [ Vec3 ]: The second vector.
Returns:
  • [ Vec3 ]: Component wise maximum value vector.

min

vec3:min( vector )

Returns the minimum value between two vectors components.

Arguments:
  • vec3 [ Vec3 ]: The vector.
  • vector [ Vec3 ]: The second vector.
Returns:
  • [ Vec3 ]: Component wise minimum value vector.

normalize

vec3:normalize()

Normalizes the vector, ie. converts it to a unit vector of length 1.

Arguments:
Returns:
  • [ Vec3 ]: The normalized vector.

rotate

vec3:rotate( angle, normal )

Rotates the vector around an axis.

The angle value uses radians.

Arguments:
  • vec3 [ Vec3 ]: The vector.
  • angle [ number ]: The angle.
  • normal [ Vec3 ]: The axis to be rotated around.
Returns:
  • [ Vec3 ]: The rotated vector.

rotateX

vec3:rotateX( angle )

Rotates the vector around the X axis.

The angle value uses radians.

Arguments:
  • vec3 [ Vec3 ]: The vector.
  • angle [ number ]: The angle.
Returns:
  • [ Vec3 ]: The rotated vector.

rotateY

vec3:rotateY( angle )

Rotates the vector around the Y axis.

The angle value uses radians.

Arguments:
  • vec3 [ Vec3 ]: The vector.
  • angle [ number ]: The angle.
Returns:
  • [ Vec3 ]: The rotated vector.

rotateZ

vec3:rotateZ( angle )

Rotates the vector around the Z axis.

The angle value uses radians.

Arguments:
  • vec3 [ Vec3 ]: The vector.
  • angle [ number ]: The angle.
Returns:
  • [ Vec3 ]: The rotated vector.

safeNormalize

vec3:safeNormalize( fallback )

Normalizes a vector with safety, ie. converts to a unit vector of length 1.

Arguments:
  • vec3 [ Vec3 ]: The vector.
  • fallback [ Vec3 ]: The fallback vector.
Returns:
  • [ Vec3 ]: The normalized vector.