Skip to content

vec3

A three-dimensional vector containing x, y, and z floating-point values. This type is used throughout the API for world positions, directions, and 3D physics.

Initialize a vector with specific values, or create a copy of an existing one.

-- create a vector at 0,0,0
local v1 = vec3()
-- create a vector with specific coordinates
local v2 = vec3(10, 5, -2)
-- create a vector from another vec3 (cloning)
local v3 = vec3(v2)

The vec3 type is used extensively throughout the API to represent physical locations and orientations in the simulation.

CSP uses a right-handed coordinate system, where Y is typically the vertical (up) axis and X and Z form the ground plane. The type supports operator overloading for +, -, *, /, unary negation, and the # operator for vector length. Most methods accept an optional out parameter, allowing results to be written to an existing vector instead of allocating a new one.

PropertyTypeDescription
xnumberThe horizontal component.
ynumberThe vertical (up) component.
znumberThe depth component.
MethodDescription
:set(x, y, z)Updates the vector’s components in place.
:normalize(out)Scales the vector to a length of 1.
:dot(other)Calculates the dot product (scalar) of two vectors.
:cross(other, out)Calculates the cross product (perpendicular vector).
:distance(other)Returns the distance between two points in meters.
:lerp(other, mix, out)Linearly interpolates toward another vector.

Check the distance between the player and a specific point in the track.

local carPos = ac.getCar(0).position
local markerPos = vec3(100, 0, 200)
local dist = carPos:distance(markerPos)
if dist < 50 then
ac.log("The car is near the marker")
end