Skip to content

vec3

Source: base/tealdef/scripts/vec3.d.tl

Notes

Types of the plain-Lua 3D vector library /scripts/vec3.tl (Vec3Serialized): vectors are ordinary tables {x = ..., y = ..., z = ...} with +, -, * (number times vector) and / (vector by number). Construction scripts use it together with /scripts/mat4.tl. Load it with local vec3 = ug_require "/scripts/vec3.tl" as Vec3Serialized.

Vec3iSerialized

record global base/tealdef/scripts/vec3.d.tl:3

global record Vec3iSerialized

Notes

Namespace of the integer 3D vector type Vec3i.

Vec3iSerialized.Vec3i

record base/tealdef/scripts/vec3.d.tl:4

record Vec3i

Notes

3D vector with integer components, as a plain table {x = ..., y = ..., z = ...}.

Fields

Name Type Description
x integer X component.
y integer Y component.
z integer Z component (height in world coordinates).

Vec3Serialized

record global base/tealdef/scripts/vec3.d.tl:11

global record Vec3Serialized

Load: local vec3 = require "/scripts/vec3.tl"

Notes

The 3D vector library returned by ug_require "/scripts/vec3.tl", and the namespace of its Vec3 type.

Functions

new(x : number, y : number, z : number) : Vec3 base/tealdef/scripts/vec3.d.tl:25

Call as vec3.new

Notes

Creates a 3D vector with arithmetic operators attached.

Parameter Type Description
x number X component.
y number Y component.
z number Z component.

Returns Vec3: The new vector.

Example

-- from base/content/scripts/scripts/construction/street_util_serialized.tl
local normalizedXYTangent = vec3.normalize(vec3.new(p1.x - p0.x, p1.y - p0.y, 0))
Used in the base game: 1051 times in 85 files

base/content/industries/fieldutil.lua:265

local p = vec3.new(f.fieldContentExtend[2] + f.fieldContentSize[1] * math.random(),
base/content/industries/industryutil.lua:334
local length = vec3.length(vec3.new( curve[2][1] - curve[1][1], curve[2][2] - curve[1][2], curve[2][3] - curve[1][3] ))
base/content/landmarks/landmark_util_serialized.tl:156
-- transform = mat4.transl( vec3.new(0, 0, -2)) -- counter hack for IsWaterConstructionHACK)
base/content/warehouses/warehouses/warehouse.script.lua:109
coll.transf = transf.mul(transf2, transf.transl(vec3.new(0, 0, 11)))
base/content/warehouses/warehouses/modules.script.lua:28
local center = vec3.new(coord.x * cell.x, coord.y * cell.y, 0)
base/content/landmarks/hq/hq/headquarter.script.tl:332
local center = vec3.new(coord.x * GridSize.y + shift.x, coord.y * GridSize.y + shift.y, shift.z)
… and 79 more files.

degToRad(dx : number, dy : number, dz : number) : Vec3 base/tealdef/scripts/vec3.d.tl:26

Call as vec3.degToRad

Notes

Converts three angles from degrees to radians and returns them as a vector.

Parameter Type Description
dx number Angle in degrees, stored in x.
dy number Angle in degrees, stored in y.
dz number Angle in degrees, stored in z.

Returns Vec3: Vector of the angles in radians.

Used in the base game: 34 times in 7 files

base/content/warehouses/warehouses/warehouse.script.lua:233

transf = transf.rotZYXTransl(vec3.degToRad(0, 0, 0), center),
base/content/warehouses/warehouses/modules.script.lua:29
local transf2 = transf.rotZYXTransl(vec3.degToRad(0, 0, 0), center)
base/content/landmarks/hq/hq/headquarter.script.tl:337
transf = mat4.rotZYXTransl(vec3.degToRad(orientation and orientation * 90 or 0, 0, 0), center),
base/content/stations/rail/rail/modular_station/modular_station.script.lua:534
transf = transf.rotZYXTransl(vec3.degToRad(0.0, 0.0, 0.0), vec3.new(i * platformWidth + offset, j * platformLength, 0)),
base/content/stations/air/air/airfield.script.lua:261
transf = transf.rotZYXTransl(vec3.degToRad(0.0, 0.0, 0.0),  vec3.new( 140, 0, 0))
base/content/stations/air/air/airport.script.lua:172
transf = transf.rotZYXTransl(vec3.degToRad(0, 0, 0), vec3.new(airportLimitEnd - 5, runway1YCoord, 0 )),
… and 1 more files.

add(v1 : Vec3, v2 : Vec3) : Vec3 base/tealdef/scripts/vec3.d.tl:28

Call as vec3.add

Notes

Returns v1 + v2.

Returns Vec3

Used in the base game: 14 times in 5 files

base/content/infrastructure/street_construction/street_construction/street_constructions.script.lua:47

local pm = vec3.add(
base/content/mission/mission/mission_task_util.tl:194
local pos = vec3.mul(0.5, vec3.add(bbox.min, bbox.max))
base/content/stations/rail/rail/modular_station/modular_station.script.lua:692
local tf1 = transf.transl(vec3.add(pos, vec3.new(0, j * platformLength + k * 10 + 5, 0)))
base/content/stations/water/water/modules.script.lua:894
center = vec3.add(center, vec3.new(0.0, 0.0, zOffs))
base/content/scripts/scripts/construction/constructionutil.lua:475
local fence_point = vec3.sub(vec3.add(vec3.new(xStart, yStart, zStart), vec3.mul(build_fence, fenceVectorSegment)), vec3.mul(0.5, fenceVectorSegment))

sub(v1 : Vec3, v2 : Vec3) : Vec3 base/tealdef/scripts/vec3.d.tl:29

Call as vec3.sub

Notes

Returns v1 - v2.

Returns Vec3

Used in the base game: 7 times in 5 files

base/content/infrastructure/street_construction/street_construction/street_constructions.script.lua:49

vec3.mul(0.125, vec3.sub(q0, q1))
base/content/mission/mission/mission_task_util.tl:224
center = center + vec3.mul((r - radius) / d, vec3.sub(c2, center))
base/content/stations/rail/rail/modular_station/modular_station.script.lua:684
local pos = vec3.sub(mainBuildingPosition, vec3.new(minDist, 0, 0))
base/content/scripts/scripts/construction/constructionutil.lua:475
local fence_point = vec3.sub(vec3.add(vec3.new(xStart, yStart, zStart), vec3.mul(build_fence, fenceVectorSegment)), vec3.mul(0.5, fenceVectorSegment))
base/content/scripts/scripts/construction/street_util_serialized.tl:23
local q = vec3.sub(p1, p0)

mul(f : number, v : Vec3) : Vec3 base/tealdef/scripts/vec3.d.tl:30

Call as vec3.mul

Notes

Returns the vector v scaled by the number f.

Returns Vec3

Used in the base game: 20 times in 6 files

base/content/infrastructure/street_construction/street_construction/street_constructions.script.lua:43

q0 = vec3.mul(scale, q0)
base/content/mission/mission/mission_task_util.tl:194
local pos = vec3.mul(0.5, vec3.add(bbox.min, bbox.max))
base/content/scripts/scripts/construction/constructionutil.lua:475
local fence_point = vec3.sub(vec3.add(vec3.new(xStart, yStart, zStart), vec3.mul(build_fence, fenceVectorSegment)), vec3.mul(0.5, fenceVectorSegment))
base/content/scripts/scripts/construction/laneutil.lua:50
q0 = vec3.mul(scale, q0)
base/content/scripts/scripts/construction/street_util_serialized.tl:38
local q = vec3.mul(approxLen, normalizedXYTangent)
dlcs/urbangames_deluxe_upgrade_pack/content/animal/animal/animal.script.lua:15
local randomPos = vec3.mul(randomLen, vec)

div(v : Vec3, d : number) : Vec3 base/tealdef/scripts/vec3.d.tl:31

Call as vec3.div

Notes

Returns the vector v with each component divided by d.

Returns Vec3

dot(v1 : Vec3, v2 : Vec3) : number base/tealdef/scripts/vec3.d.tl:33

Call as vec3.dot

Notes

Dot product v1.x * v2.x + v1.y * v2.y + v1.z * v2.z.

Returns number

cross(v1 : Vec3, v2 : Vec3) : Vec3 base/tealdef/scripts/vec3.d.tl:34

Call as vec3.cross

Notes

Cross product v1 × v2 (right-handed).

Returns Vec3

length(v : Vec3) : number base/tealdef/scripts/vec3.d.tl:39

Call as vec3.length

Get the length (magnitude) of the vector.

Parameter Type Description
v Vec3 vector

Returns number: length of the vector

Used in the base game: 3 times in 3 files

base/content/industries/industryutil.lua:334

local length = vec3.length(vec3.new( curve[2][1] - curve[1][1], curve[2][2] - curve[1][2], curve[2][3] - curve[1][3] ))
base/content/infrastructure/street_construction/street_construction/street_constructions.script.lua:22
local length = vec3.length(dir)
base/content/scripts/scripts/construction/constructionutil.lua:466
local fenceVectorLength = vec3.length(fenceVector)

distance(v1 : Vec3, v2 : Vec3) : number base/tealdef/scripts/vec3.d.tl:44

Call as vec3.distance

Get the distance between two vectors.

Parameter Type Description
v1 Vec3 first vector
v2 Vec3 second vector

Returns number: distance between the vectors

Used in the base game: 7 times in 5 files

base/content/industries/fieldutil.lua:269

if vec3.distance(p, positions[j]) < 4 then
base/content/infrastructure/street_construction/street_construction/street_constructions.script.lua:38
local length = vec3.distance(p0, p1)
base/content/mission/mission/mission_task_util.tl:197
radius = 0.5 * vec3.distance(bbox.min, bbox.max) + ((radiusOffset ~= nil and radiusOffset) or 0)
base/content/scripts/scripts/construction/laneutil.lua:46
local length = vec3.distance(vec3.new(points[j+0][1], points[j+0][2], points[j+0][3]), vec3.new(points[j+3][1], points[j+3][2],points[j+3][3]))
base/content/scripts/scripts/construction/street_util_serialized.tl:36
local approxLen = vec3.distance(p0, p1)

distance2(v1 : Vec3, v2 : Vec3) : number base/tealdef/scripts/vec3.d.tl:49

Call as vec3.distance2

Get the squared distance between two vectors.

Parameter Type Description
v1 Vec3 first vector
v2 Vec3 second vector

Returns number: squared distance between the vectors

normalize(v : Vec3) : Vec3 base/tealdef/scripts/vec3.d.tl:54

Call as vec3.normalize

Normalize the vector (make it unit length).

Parameter Type Description
v Vec3 vector to normalize

Returns Vec3: normalized vector

Used in the base game: 7 times in 3 files

base/content/infrastructure/street_construction/street_construction/street_constructions.script.lua:35

local q0 = vec3.normalize(t0)
base/content/scripts/scripts/construction/laneutil.lua:44
local q0 = vec3.normalize(vec3.new(tangent1[1], tangent1[2], tangent1[3]))
base/content/scripts/scripts/construction/street_util_serialized.tl:37
local normalizedXYTangent = vec3.normalize(vec3.new(p1.x - p0.x, p1.y - p0.y, 0))

normalizeWithFallback(v : Vec3, f : Vec3) : Vec3 base/tealdef/scripts/vec3.d.tl:59

Call as vec3.normalizeWithFallback

Normalize the vector with a fallback if zero length.

Parameter Type Description
v Vec3 vector to normalize
f Vec3 fallback vector

Returns Vec3: normalized vector or fallback

Used in the base game: 2 times in 1 file

dlcs/urbangames_deluxe_upgrade_pack/content/animal/animal/animal.script.lua:13

vec = vec3.normalizeWithFallback(vec, vecFallback)

angleUnit(v1 : Vec3, v2 : Vec3) : number base/tealdef/scripts/vec3.d.tl:65

Call as vec3.angleUnit

Get the angle between two unit vectors.

Parameter Type Description
v1 Vec3 first unit vector
v2 Vec3 second unit vector

Returns number: angle in radians

Used in the base game: 3 times in 3 files

base/content/infrastructure/street_construction/street_construction/street_constructions.script.lua:39

local angle = vec3.angleUnit(q0, q1)
base/content/scripts/scripts/construction/laneutil.lua:47
local angle = vec3.angleUnit(q0, q1)
base/content/scripts/scripts/construction/street_util_serialized.tl:59
local angle = vec3.angleUnit(q0, q1)

angle(v1 : Vec3, v2 : Vec3) : number base/tealdef/scripts/vec3.d.tl:70

Call as vec3.angle

Get the angle between two vectors.

Parameter Type Description
v1 Vec3 first vector
v2 Vec3 second vector

Returns number: angle in radians

xyAngle(v : Vec3) : number base/tealdef/scripts/vec3.d.tl:74

Call as vec3.xyAngle

Get the angle in the XY plane.

Parameter Type Description
v Vec3 vector

Returns number: angle in radians

Used in the base game: 2 times in 1 file

base/content/scripts/scripts/construction/constructionutil.lua:479

transf = transf.rotZTransl(vec3.xyAngle(fenceVector) + (angle or 0), fence_point),

toArray(v : Vec3) : Table3 base/tealdef/scripts/vec3.d.tl:79

Call as vec3.toArray

Convert vector to an array of three numbers.

Parameter Type Description
v Vec3 vector

Returns Table3: array with x, y, z components

Used in the base game: 16 times in 2 files

base/content/scripts/scripts/construction/street_util_serialized.tl:21

local s0 = vec3.toArray(p0)
base/content/scripts/scripts/construction/collider_util_serialized.tl:17
result.params.halfExtents = vec3.toArray(halfExtents)

fromArray(t : Table3) : Vec3 base/tealdef/scripts/vec3.d.tl:83

Call as vec3.fromArray

Create vector from an array of three numbers.

Parameter Type Description
t Table3 array with x, y, z components

Returns Vec3: vector

Used in the base game: 1 time in 1 file

base/content/scripts/scripts/construction/constructionutil.lua:336

points[#points + 1] = vec3.fromArray(point)

print(v : Vec3) : string base/tealdef/scripts/vec3.d.tl:88

Call as vec3.print

Convert vector to a string representation.

Parameter Type Description
v Vec3 vector

Returns string: string representation

Vec3Serialized.Table3

record base/tealdef/scripts/vec3.d.tl:12

record Table3 is {number}

of lenght 3

Used in the base game: 2 times in 1 file

base/content/scripts/scripts/vec3.tl:95

vec3_util.toArray = function(v : Vec3Serialized.Vec3) : Vec3Serialized.Table3

Vec3Serialized.Vec3

record base/tealdef/scripts/vec3.d.tl:14

record Vec3

Notes

3D vector as a plain table {x = ..., y = ..., z = ...}. Vectors made by new (and by the operators) carry a metatable, so v1 + v2, v1 - v2, f * v and v / d work.

Used in the base game: 100 times in 7 files

base/content/landmarks/hq/hq/headquarter.script.tl:314

slotHeight : number, orientation : integer, shift : Vec3Serialized.Vec3, variant : integer)
base/content/mission/mission/mission_task_util.tl:159
min : Vec3Serialized.Vec3
base/content/scripts/scripts/vec3.tl:3
vec3_util.new = function(x : number, y : number, z : number) : Vec3Serialized.Vec3
base/content/scripts/scripts/mat4.tl:94
mat4_util.transl = function(t : Vec3Serialized.Vec3) : Mat4Serialized.Mat4
base/content/scripts/scripts/construction/street_util_serialized.tl:19
p0 : Vec3Serialized.Vec3, p1 : Vec3Serialized.Vec3,
base/content/scripts/scripts/construction/collider_util_serialized.tl:6
collider_util_serialized.createBox = function(center : Vec3Serialized.Vec3, halfExtents : Vec3Serialized.Vec3) : ColliderUtilSerialized.Collider
… and 1 more files.

Fields

Name Type Description
x number X component.
y number Y component.
z number Z component (up in world and model coordinates).

Functions

metamethod __add(v1 : Vec3, v2 : Vec3) : Vec3 base/tealdef/scripts/vec3.d.tl:19

Notes

Component-wise sum v1 + v2.

Returns Vec3

metamethod __sub(v1 : Vec3, v2 : Vec3) : Vec3 base/tealdef/scripts/vec3.d.tl:20

Notes

Component-wise difference v1 - v2.

Returns Vec3

metamethod __mul(f : number, v : Vec3) : Vec3 base/tealdef/scripts/vec3.d.tl:21

Notes

Scalar product f * v; the number must be the left operand.

Returns Vec3

metamethod __div(v : Vec3, d : number) : Vec3 base/tealdef/scripts/vec3.d.tl:22

Notes

Division of every component by a number, v / d.

Returns Vec3