util.math.rotation_2d

Shared

Calculates the angle from one 2D point to another, in degrees


Syntax

local result = util.math.rotation_2d(
    a,
    b
)

Parameters

TypeNameDescription
vector2aOrigin point
vector2bTarget point

Returns

TypeNameDescription
floatresultAngle from a to b in degrees, normalized to the range 0-360

Examples

Angle pointing directly right
local result = util.math.rotation_2d({0, 0}, {10, 0})

core.engine.print("info", result) -- 0.0
Angle pointing straight down (screen space, +y is down)
local result = util.math.rotation_2d({0, 0}, {0, 10})

core.engine.print("info", result) -- 90.0
Negative angles wrap around to stay in range 0-360
local result = util.math.rotation_2d({0, 0}, {0, -10})

core.engine.print("info", result) -- 270.0 (not -90.0)
Diagonal angle
local result = util.math.rotation_2d({0, 0}, {10, 10})

core.engine.print("info", result) -- 45.0
Make an entity face a target
local entity_pos = {100, 100}
local target_pos = {200, 50}
local facing_angle = util.math.rotation_2d(entity_pos, target_pos)

core.engine.print("info", facing_angle) -- 333.43494...

On this page