aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/init.lua2
-rw-r--r--src/utils.lua55
2 files changed, 56 insertions, 1 deletions
diff --git a/src/init.lua b/src/init.lua
index c18f175..fd13bcb 100644
--- a/src/init.lua
+++ b/src/init.lua
@@ -35,7 +35,7 @@ end
function main_init()
lg.setDefaultFilter("nearest", "nearest")
- Viewport = lg.newCanvas(SCR_WIDTH, SCR_HEIGHT)
+ viewport = lg.newCanvas(SCR_WIDTH, SCR_HEIGHT)
load_textures_from()
diff --git a/src/utils.lua b/src/utils.lua
index c3e4bea..b2186cc 100644
--- a/src/utils.lua
+++ b/src/utils.lua
@@ -37,3 +37,58 @@ function dist(x1, y1, x2, y2)
local dy = y1 - y2
return (dx*dx + dy*dy)^0.5
end
+
+mathx = {}
+
+mathx.tau = math.pi * 2
+
+function mathx.sign(x)
+ if x == 0 then
+ return 0
+ end
+ return x < 0 and -1 or 1
+end
+
+function round(x)
+ return math.floor(x + 0.5)
+end
+
+function clamp(a, min, max)
+ return math.min(max, math.max(a, min))
+end
+
+function snap(x, step)
+ return math.floor(x / step) * step
+end
+
+function frac(x)
+ return x - math.floor(x)
+end
+
+local function angle_diff(a, b)
+ local diff = (b - a) % (math.pi * 2)
+ return (2 * diff) % (math.pi * 2) - diff
+end
+
+function lerp_angle(a, b, t)
+ return a + angle_diff(a, b) * (1 - 0.5^t)
+end
+
+function dot(x, y, xx, yy)
+ return x*xx + y*yy
+end
+
+function vec_sqlen(x, y)
+ return x*x + y*y
+end
+
+function vec_len(x, y)
+ return math.sqrt(x*x + y*y)
+end
+
+function rotate_vec(x, y, r)
+ local angle = math.atan2(y, x) + r
+ local len = mathx.vec_len(x, y)
+ return math.cos(angle) * len, math.sin(angle) * len
+end
+