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
base/content/landmarks/hq/hq/headquarter.script.tl:10
base/content/gui/gui/node_editor/layer_nodes.script.tl:313
base/content/stations/rail/rail/modular_station/modular_station.script.lua:631
… 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
base/content/climates/climates/gen/rivermapgenutil.tl:154
sub(v1 : Vec2, v2 : Vec2) : Vec2 base/tealdef/scripts/vec2.d.tl:26
Call as vec2.sub
Notes
Returns v1 - v2.
Returns Vec2
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
base/content/climates/climates/gen/rivermapgenutil.tl:147
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
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
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
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
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
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
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
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
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
base/content/gui/gui/node_editor/layer_nodes.script.tl:241
base/content/gui/gui/map_editor/map_editor.tl:1721
base/content/scripts/scripts/vec2.tl:3
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