Lumberyard
Developer Guide (Version 1.12)

 AWS Documentation » Amazon Lumberyard » Developer Guide » Legacy Lumberyard Programming Content » Legacy Lua Scripting Reference » Lua Vector and Math Functions

# Lua Vector and Math Functions

 CryLua is deprecated and will be removed in a future version of Lumberyard.

This topic describes the commonly used math global vectors, constants, and functions.

• File location: `Game/Scripts/Utils/Math.lua`

• Loaded from: `Game/Scripts/common.lua`

## Global Vectors

The following globals should be used to avoid temporary Lua memory allocations:

Global Name

Description

g_Vectors.v000 Basic zero vector.
g_Vectors.v001 Positive z-axis direction vector.
g_Vectors.v010 Positive y-axis direction vector.
g_Vectors.v100 Positive x-axis direction vector.
g_Vectors.v101 The x and z-axis direction vector.
g_Vectors.v110 The x and y-axis direction vector.
g_Vectors.v111 The x, y and z-axis vector.
g_Vectors.up Positive z-axis direction vector.
g_Vectors.down Negative z-axis direction vector.
g_Vectors.temp Temporary zero vector.
g_Vectors.tempColor Temporary zero vector. Commonly used for passing rgb color values.
g_Vectors.temp_v1 Temporary zero vector.
g_Vectors.temp_v2 Temporary zero vector.
g_Vectors.temp_v3 Temporary zero vector.
g_Vectors.temp_v4 Temporary zero vector.
g_Vectors.vecMathTemp1 Temporary zero vector.
g_Vectors.vecMathTemp2 Temporary zero vector.

## Constants

Constant Name

Description

g_Pi Basic Pi constant based on `math.pi`.
g_2Pi Basic double-Pi constant based on `math.pi`.
g_Pi2 Basic half-Pi constant based on `math.pi`.

## IsNullVector()

Checks whether or not all components of a specified vector are null.

Parameter Description
a Vector to check.

## IsNotNullVector()

Checks whether or not any components of a specified vector is not null.

Parameter Description
a Vector to check.

## LengthSqVector()

Retrieves the squared length of a specified vector.

Parameter Description
a Vector to retrieve length for.

## LengthVector()

Retrieves the length of a specified vector.

Parameter Description
a Vector to retrieve length for.

## DistanceSqVectors()

Retrieves the squared distance between two vectors.

Parameter Description
a First vector.
b Second vector.

## DistanceSqVectors2d()

Retrieves the squared distance between two vectors in 2D space (without z-component).

Parameter Description
a First vector.
b Second vector.

## DistanceVectors()

Retrieves the distance between two vectors.

Parameter Description
a First vector.
b Second vector.

## dotproduct3d()

Retrieves the dot product between two vectors.

Parameter Description
a First vector.
b Second vector.

## dotproduct2d()

Retrieves the dot product between two vectors in 2D space (without z-component).

Parameter Description
a First vector.
b Second vector.

## LogVec()

Logs a specified vector to console.

Parameter Description
name Descriptive name of the vector.
v Vector to log.

### Example

``LogVec("Local Actor Position", g_localActor:GetPos())``

Console output:

``<Lua> Local Actor Position = (1104.018066 1983.247925 112.769440)``

## ZeroVector()

Sets all components of a specified vector to zero.

Parameter Description
dest Vector to zero out.

## CopyVector()

Copies the components of one vector to another.

Parameter Description
dest Destination vector.
src Source vector.

## SumVectors()

Adds up the components of two vectors.

Parameter Description
a First vector.
b Second vector.

## NegVector()

Negates all components of a specified vector.

Parameter Description
a Vector to negate.

## SubVectors()

Copies the componentwise subtraction of two vectors to a destination vector.

Parameter Description
dest Destination vector.
a First vector.
b Second vector.

## FastSumVectors()

Copies the componentwise addition of two vectors to a destination vector.

Parameter Description
dest Destination vector.
a First vector.
b Second vector.

## DifferenceVectors()

Retrieves the difference between two vectors.

Parameter Description
a First vector.
b Second vector.

## FastDifferenceVectors()

Copies the componentwise difference between two vectors to a destination vector.

Parameter Description
dest Destination vector.
a First vector.
b Second vector.

## ProductVectors()

Retrieves the product of two vectors.

Parameter Description
a First vector.
b Second vector.

## FastProductVectors()

Copies the product of two vectors to a destination vector.

Parameter Description
dest Destination vector.
a First vector.
b Second vector.

## ScaleVector()

Scales a specified vector a by a factor of b.

Parameter Description
a Vector.
b Scalar.

## ScaleVectorInPlace(a,b)

Retrieves a new vector based on a copy of vector a scaled by a factor b.

Parameter Description
a First vector.
b Scalar.

## ScaleVectorInPlace(dest,a,b)

Copies vector a scaled by the factor of b to a destination vector.

Parameter Description
dest Destination vector.
a First vector.
b Scalar.

## NormalizeVector()

Normalizes a specified vector.

Parameter Description
a Vector to normalize.

## VecRotate90_Z()

Rotates a specified vector by 90 degree around the z-axis.

Parameter Description
v Vector to rotate.

## VecRotateMinus90_Z()

Rotates a specified vector by -90 degree around the z-axis.

Parameter Description
v Vector to rotate.

## crossproduct3d()

Copies the result of the cross product between two vectors to a destination vector.

Parameter Description
dest Destination vector.
p First vector.
q Second vector.

## RotateVectorAroundR()

Copies to a destination vector the result of the vector rotation of vector p around vector r by a specified angle.

Parameter Description
dest Destination vector.
p First vector.
r Second vector.
angle Rotation angle.

## ProjectVector()

Copies to a destination vector the result of the vector projection of vector P to the surface with a specified normal N.

Parameter Description
dest Destination vector.
P Vector to project.
N Surface normal.

## DistanceLineAndPoint()

Retrieves the distance between point a and the line between p and q.

Parameter Description
a Point to measure from.
p Vector p.
q Vector q.

## LerpColors()

Performs linear interpolation between two color/vectors with a factor of k.

Parameter Description
a Color/vector a.
b Color/vector b.
k Factor k.

## Lerp()

Performs linear interpolation between two scalars with a factor of k.

Parameter Description
a Scalar a.
b Scalar b.
k Factor k.

## __max()

Retrieves the maximum of two scalars.

Parameter Description
a Scalar a.
b Scalar b.

## __min()

Retrieves the minimum of two scalars.

Parameter Description
a Scalar a.
b Scalar b.

## clamp()

Clamps a specified number between minimum and maximum.

Parameter Description
_n Number to clamp.
_min Lower limit.
_max Upper limit.

## Interpolate()

Interpolates a number to a specified goal by a specified speed.

Parameter Description
actual Number to interpolate.
goal Goal.
speed Interpolation speed.

## sgn()

Retrieves the sign of a specified number (0 returns 0).

Parameter Description
a Number to get sign for.

## sgnnz()

Retrieves the sign of a specified number (0 returns 1).

Parameter Description
a Number to get sign for.

## sqr()

Retrieves the square of a specified number.

Parameter Description
a Number to square.

## randomF()

Retrieves a random float value between two specified numbers.

Parameter Description
a First number.
b Second number.

## iff()

Checks the condition of a test value and returns one of two other values depending on whether the test value is `nil` or not.

Parameter Description
c Test value.
a Return value if test value is not nil.
b Return value if test value is nil.