CS2 Lua API

vector2

A two-dimensional vector: coordinates and sizes on the screen. Fields x, y.

vector2(x?: number, y?: number)#

Creates a vector. Components that are not set are 0. vector2(v) is a copy.

Parameter Type Description
x, y number components, default 0

Returns: vector2.

local pos  = vector2(100, 50)
local size = vector2(120, 30)
local center = pos + size / 2
print(center)   -- (160, 65)

Operators#

+, -, *, / (with a number or component-wise), unary -, # (length) and == — as for vector.

local p = vector2(100, 50) + vector2(10, 10) * 2
print(p)   -- (120, 70)

v:unpack()#

Returns: number, number — x, y.

local w, h = render.screen_size():unpack()
print(w, "x", h)

v:init(...), src:copy(dst: vector2)#

init re-initializes the vector in place. copy copies src into dst.

Parameter Type Description
dst vector2 where to copy

Returns: nothing.

local p = vector2()
p:init(5, 5)

v:length(), v:lengthsqr()#

The length and its square.

Returns: number.

print(vector2(3, 4):length())   --> 5

a:dist(b: vector2), a:distsqr(b: vector2)#

The distance between points and its square.

Parameter Type Description
b vector2 the second point

Returns: number.

-- The enemy closest to the screen center
events.render:set(function()
    local center = render.screen_size() / 2
    local best, best_d = nil, math.huge
    for _, pawn in ipairs(entity.pawns(true)) do
        local p = render.world_to_screen(pawn:eye_position())
        if p and p:dist(center) < best_d then best, best_d = p, p:dist(center) end
    end
    if best then render.line(center, best, color(255, 255, 0, 255)) end
end)

a:dot(b: vector2)#

Dot product.

Parameter Type Description
b vector2 the second vector

Returns: number.

print(vector2(1, 0):dot(vector2(0, 1)))   --> 0

a:cross(b: vector2)#

The 2D cross product a.x * b.y - a.y * b.x.

Parameter Type Description
b vector2 the second vector

Returns: number.

print(vector2(1, 0):cross(vector2(0, 1)))   --> 1

a:lerp(b: vector2, t: number)#

Linear interpolation.

Parameter Type Description
b vector2 the second point
t number fraction, 0–1

Returns: vector2.

-- Smooth movement of a plate towards a target
local pos = vector2(0, 0)

events.render:set(function()
    pos = pos:lerp(render.screen_size() / 2, 0.1)
    render.rect(pos, vector2(10, 10), color(255, 255, 255, 255))
end)

v:normalize(), v:normalized()#

normalize makes the vector unit length in place and returns the previous length; normalized returns a unit copy.

Returns: normalize — number; normalized — vector2.

print(vector2(10, 0):normalized())   -- (1, 0)

v:scale(s: number), v:scaled(s: number)#

Scale in place and a scaled copy.

Parameter Type Description
s number multiplier

Returns: scale — nothing; scaled — vector2.

print(vector2(2, 3):scaled(10))   -- (20, 30)

v:as_vector(z?: number)#

Converts to a three-dimensional vector.

Parameter Type Description
z number the third component, default 0

Returns: vector.

print(vector2(120, 70):as_vector(5))   -- (120, 70, 5)