Skip to content

vec2

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

Notes

Types of the plain-Lua 2D vector library /scripts/vec2.tl (Vec2Serialized). Unlike the engine's api.type.Vec2f, these vectors are ordinary Lua tables {x = ..., y = ...} with arithmetic metamethods, so they can be stored in script state and construction results. Load it with local vec2 = ug_require "/scripts/vec2.tl" as Vec2Serialized.

Vec2iSerialized

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

global record Vec2iSerialized

Notes

Namespace of the integer 2D vector type Vec2i.

Vec2iSerialized.Vec2i

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

record Vec2i

Notes

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

Fields

Name Type Description
x integer X component.
y integer Y component.

Vec2Serialized

record global base/tealdef/scripts/vec2.d.tl:10

global record Vec2Serialized

Load: local vec2 = ug_require "/scripts/vec2.tl"

Notes

The 2D vector library returned by ug_require "/scripts/vec2.tl", and the namespace of its Vec2 type.

Functions

new(x : number, y : number) : Vec2 base/tealdef/scripts/vec2.d.tl:23

Call as vec2.new

Notes

Creates a 2D vector with arithmetic operators attached.

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

Returns Vec2: The new vector.

Used in the base game: 27 times in 7 files

base/content/climates/climates/gen/mapgenutil.tl:11

local newStart = vec2.new(math_util.randf(bounds.min.x, bounds.max.x), math_util.randf(bounds.min.y, bounds.max.y))
base/content/climates/climates/gen/rivermapgenutil.tl:147
local rot = vec2.mul(math_util.mapClamp(math.abs(theta), 0, 2.3, 1, 5.0), vec2.new(tg.x * math.cos(theta) - tg.y * math.sin(theta), tg.x * math.sin(theta) + tg.
base/content/warehouses/warehouses/warehouse.script.lua:11
local gridSize = vec2.new(8, 8)
base/content/landmarks/hq/hq/headquarter.script.tl:10
local GridSize = vec2.new(8, 8)
base/content/gui/gui/node_editor/layer_nodes.script.tl:313
return vec2.new(s1 * inputs.lakeWidth.value * 180, s2 * inputs.lakeWidth.value * 180), s * 50
base/content/stations/rail/rail/modular_station/modular_station.script.lua:631
local mainBuildingSize = vec2.new(5, 12)
… and 1 more files.

add(v1 : Vec2, v2 : Vec2) : Vec2 base/tealdef/scripts/vec2.d.tl:25

Call as vec2.add

Notes

Returns v1 + v2.

Returns Vec2

Used in the base game: 12 times in 2 files

base/content/climates/climates/gen/mapgenutil.tl:22

local secondPos = vec2.add(newStart, vec2.mul(samplePointDistance, dirVec))
base/content/climates/climates/gen/rivermapgenutil.tl:154
nrml = vec2.add(vec2.new(nrml.x, nrml.y), vec2.mul(of, vec2.new(tg.x, tg.y)))

sub(v1 : Vec2, v2 : Vec2) : Vec2 base/tealdef/scripts/vec2.d.tl:26

Call as vec2.sub

Notes

Returns v1 - v2.

Returns Vec2

Used in the base game: 5 times in 1 file

base/content/climates/climates/gen/mapgenutil.tl:52

local dist = vec2.sub(center, start.pos )

mul(f : number, v : Vec2) : Vec2 base/tealdef/scripts/vec2.d.tl:27

Call as vec2.mul

Notes

Returns the vector v scaled by the number f.

Returns Vec2

Used in the base game: 14 times in 2 files

base/content/climates/climates/gen/mapgenutil.tl:22

local secondPos = vec2.add(newStart, vec2.mul(samplePointDistance, dirVec))
base/content/climates/climates/gen/rivermapgenutil.tl:147
local rot = vec2.mul(math_util.mapClamp(math.abs(theta), 0, 2.3, 1, 5.0), vec2.new(tg.x * math.cos(theta) - tg.y * math.sin(theta), tg.x * math.sin(theta) + tg.

div(v : Vec2, f : number) : Vec2 base/tealdef/scripts/vec2.d.tl:28

Call as vec2.div

Notes

Returns the vector v with each component divided by f.

Returns Vec2

Used in the base game: 5 times in 2 files

base/content/climates/climates/gen/mapgenutil.tl:341

local subdivVec = vec2.div(segmentVec, subdiv)
base/content/climates/climates/gen/rivermapgenutil.tl:29
table.insert(newRiver.tangents, vec2.div(river.tangents[i], nSegments))

componentwiseMul(v1 : Vec2, v2 : Vec2) : Vec2 base/tealdef/scripts/vec2.d.tl:29

Call as vec2.componentwiseMul

Notes

Returns {x = v1.x * v2.x, y = v1.y * v2.y}.

Returns Vec2

Details

The result is a plain table without the operator metatable; wrap it in new if you need +/-.

componentwiseDiv(v1 : Vec2, v2 : Vec2) : Vec2 base/tealdef/scripts/vec2.d.tl:30

Call as vec2.componentwiseDiv

Notes

Returns {x = v1.x / v2.x, y = v1.y / v2.y}.

Returns Vec2

Details

The result is a plain table without the operator metatable; wrap it in new if you need +/-.

length(v : Vec2) : number base/tealdef/scripts/vec2.d.tl:35

Call as vec2.length

Get the length (magnitude) of the vector.

Parameter Type Description
v Vec2 vector

Returns number: length of the vector

Used in the base game: 2 times in 1 file

base/content/climates/climates/gen/mapgenutil.tl:151

table.insert(valley.tangents, vec2.mul(vec2.length(vec2.sub(position, oldPosition)), vec2.fromAngle(newDir + randomAngle)))

distance(v1 : Vec2, v2 : Vec2) : number base/tealdef/scripts/vec2.d.tl:40

Call as vec2.distance

Get the distance between two vectors.

Parameter Type Description
v1 Vec2 first vector
v2 Vec2 second vector

Returns number: distance between the vectors

Used in the base game: 3 times in 1 file

base/content/climates/climates/gen/mapgenutil.tl:156

local numSegments = math.ceil(vec2.distance(positionToCheck, oldPosition) / distanceCheck) * 4

dot(v1 : Vec2, v2 : Vec2) : number base/tealdef/scripts/vec2.d.tl:45

Call as vec2.dot

Compute the dot product of two vectors.

Parameter Type Description
v1 Vec2 first vector
v2 Vec2 second vector

Returns number: dot product

angle(v1 : Vec2, v2 : Vec2) : number base/tealdef/scripts/vec2.d.tl:50

Call as vec2.angle

Get the angle between two vectors.

Parameter Type Description
v1 Vec2 first vector
v2 Vec2 second vector

Returns number: angle in radians

normalize(v : Vec2) : Vec2 base/tealdef/scripts/vec2.d.tl:55

Call as vec2.normalize

Normalize the vector (make it unit length).

Parameter Type Description
v Vec2 vector to normalize

Returns Vec2: normalized vector

rotate90(v : Vec2) : Vec2 base/tealdef/scripts/vec2.d.tl:59

Call as vec2.rotate90

Rotate the vector by 90 degrees.

Parameter Type Description
v Vec2 vector to rotate

Returns Vec2: rotated vector

lerp(v1 : Vec2, v2 : Vec2, s : number) : Vec2 base/tealdef/scripts/vec2.d.tl:65

Call as vec2.lerp

Linear interpolation between two vectors.

Parameter Type Description
v1 Vec2 first vector
v2 Vec2 second vector
s number interpolation factor [0, 1]

Returns Vec2: interpolated vector

Used in the base game: 2 times in 1 file

base/content/climates/climates/gen/rivermapgenutil.tl:44

local newWidth = vec2.lerp(river.widths[i], river.widths[i+1], h)

herp(p0 : Vec2, p1 : Vec2, t0 : Vec2, t1 : Vec2, s : number) : Vec2 base/tealdef/scripts/vec2.d.tl:73

Call as vec2.herp

Hermite interpolation between two points with tangents.

Parameter Type Description
p0 Vec2 start point
p1 Vec2 end point
t0 Vec2 start tangent
t1 Vec2 end tangent
s number interpolation factor [0, 1]

Returns Vec2: interpolated vector

Used in the base game: 1 time in 1 file

base/content/climates/climates/gen/rivermapgenutil.tl:40

local newPoint = vec2.herp(river.points[i], river.points[i+1], river.tangents[i], river.tangents[i+1], h)

herpPrime(p0 : Vec2, p1 : Vec2, t0 : Vec2, t1 : Vec2, s : number) : Vec2 base/tealdef/scripts/vec2.d.tl:81

Call as vec2.herpPrime

Derivative of Hermite interpolation.

Parameter Type Description
p0 Vec2 start point
p1 Vec2 end point
t0 Vec2 start tangent
t1 Vec2 end tangent
s number interpolation factor [0, 1]

Returns Vec2: derivative vector

Used in the base game: 1 time in 1 file

base/content/climates/climates/gen/rivermapgenutil.tl:41

local newTangent = vec2.div(vec2.herpPrime(river.points[i], river.points[i+1], river.tangents[i], river.tangents[i+1], h), nSegments)

fromAngle(a : number) : Vec2 base/tealdef/scripts/vec2.d.tl:85

Call as vec2.fromAngle

Create a unit vector from an angle.

Parameter Type Description
a number angle in radians

Returns Vec2: unit vector

Used in the base game: 5 times in 1 file

base/content/climates/climates/gen/mapgenutil.tl:20

local dirVec = vec2.fromAngle(angle)

toArray(v : Vec2) : Table2 base/tealdef/scripts/vec2.d.tl:90

Call as vec2.toArray

Convert vector to an array of two numbers.

Parameter Type Description
v Vec2 vector

Returns Table2: array with x and y components

fromArray(t : Table2) : Vec2 base/tealdef/scripts/vec2.d.tl:94

Call as vec2.fromArray

Create vector from an array of two numbers.

Parameter Type Description
t Table2 array with x and y components

Returns Vec2: vector

print(v : Vec2) : string base/tealdef/scripts/vec2.d.tl:99

Call as vec2.print

Convert vector to a string representation.

Parameter Type Description
v Vec2 vector

Returns string: string representation

Vec2Serialized.Table2

record base/tealdef/scripts/vec2.d.tl:11

record Table2 is {number}

of lenght 2

Used in the base game: 2 times in 1 file

base/content/scripts/scripts/vec2.tl:116

vec2_util.toArray = function(v : Vec2Serialized.Vec2) : Vec2Serialized.Table2

Vec2Serialized.Vec2

record base/tealdef/scripts/vec2.d.tl:13

record Vec2

Notes

2D vector as a plain table {x = ..., y = ...}. 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: 96 times in 6 files

base/content/climates/climates/gen/mapgenutil.tl:59

function data.makeRivers(valleys : {RiverResult}, config : RiverBuilderConfig, startLength : number, startPosition : Vec2Serialized.Vec2,
base/content/landmarks/hq/hq/headquarter.script.tl:86
local encodeSlotId = function(coord : Vec2Serialized.Vec2, slotType : string, orientation? : integer, variant? : integer) : integer
base/content/landmarks/hq/hq/headquarter_addon.script.tl:70
local offset = result.rotate(connector.offset as Vec2Serialized.Vec2, orientation)
base/content/gui/gui/node_editor/layer_nodes.script.tl:241
getStartWeight = function(pos : Vec2Serialized.Vec2) : number
base/content/gui/gui/map_editor/map_editor.tl:1721
position : Vec2Serialized.Vec2
base/content/scripts/scripts/vec2.tl:3
vec2_util.new = function(x : number, y : number) : Vec2Serialized.Vec2

Fields

Name Type Description
x number X component.
y number Y component.

Functions

metamethod __add(v1 : Vec2, v2 : Vec2) : Vec2 base/tealdef/scripts/vec2.d.tl:17

Notes

Component-wise sum v1 + v2.

Returns Vec2

metamethod __sub(v1 : Vec2, v2 : Vec2) : Vec2 base/tealdef/scripts/vec2.d.tl:18

Notes

Component-wise difference v1 - v2.

Returns Vec2

metamethod __mul(f : number, v : Vec2) : Vec2 base/tealdef/scripts/vec2.d.tl:19

Notes

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

Returns Vec2

metamethod __div(v : Vec2, d : number) : Vec2 base/tealdef/scripts/vec2.d.tl:20

Notes

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

Returns Vec2