-
Notifications
You must be signed in to change notification settings - Fork 0
/
mymath.lua
68 lines (50 loc) · 1.61 KB
/
mymath.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
local mymath = {}
local dx, dy, v
function mymath.average_angles(...)
dx, dy = 0,0
for i=1,select('#',...) do local a= select(i,...) x, y = x+math.cos(a), y+math.sin(a) end
return math.atan2(y, x)
end
function mymath.angle_difference(source, target)
local a = target - source
if (a > math.pi) then
a = a - math.pi * 2
elseif (a < -math.pi) then
a = a + math.pi * 2
end
return a
end
function mymath.clamp(low, n, high) return math.min(math.max(low, n), high) end
function mymath.vector_length(x, y) return (x^2+y^2)^0.5 end
function mymath.dist(x1,y1, x2,y2) return ((x2-x1)^2+(y2-y1)^2)^0.5 end
function mymath.collision(a, b)
return a.x < b.x+b.w and
b.x < a.x+a.w and
a.y < b.y+b.h and
b.y < a.y+a.h
end
local window_border = 32
function mymath.in_window(x, y)
return (camera.x - window_border <= x and x < (camera.x + window.w + window_border)
and camera.y - window_border <= y and y < (camera.y + window.h + window_border))
end
function mymath.abs_subtract(a, d)
-- moves towards zero by d, d>=0
if d >= math.abs(a) then return 0
elseif a>0 then return a-d
else return a+d
end
end
function mymath.sign(n) return n>0 and 1 or n<0 and -1 or 0 end
function mymath.round(n) return math.floor(n + 0.5) end
function mymath.abs_floor(n)
if n >= 0 then return math.floor(n)
else return math.ceil(n) end
end
function mymath.abs_ceil(n)
if n >= 0 then return math.ceil(n)
else return math.floor(n) end
end
function mymath.one_chance_in(n) return love.math.random(1,n) == 1 end
function mymath.random_spread(angle, spread) return angle + (spread * (2 * love.math.random() - 1)) end
return mymath