-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.lua
80 lines (44 loc) · 1.52 KB
/
util.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
69
70
71
72
73
74
75
76
77
78
79
80
math.clamp = function (a, x, b)
return a > x and a or
b < x and b or x
end
util = {}
util.distance = function (x1, y1, x2, y2)
return math.sqrt((x1 - x2) ^ 2 + (y1 - y2) ^ 2)
end
util.screenToWorld = function (x, y)
x = x - game.camerax / game.zoom - love.window:getWidth() / 2
y = y - game.cameray / game.zoom - love.window:getHeight() / 2
return x, y
end
util.randomAngle = function ()
return math.random(1, 360) / 180 * math.pi
end
util.pointInRectangle = function (rx, ry, rw, rh, px, py)
return px >= rx and py >= ry and px <= rx + rw and py <= ry + rh
end
util.pointInPolygon = function (poly, px, py) -- poly in the form of {x1, y1, x2, y2, x3, y3, ...}
local collide = 0
for i = 1, #poly, 2 do
local x1, y1 = poly[i], poly[i+1]
local x2, y2 = poly[(i+2-1) % #poly + 1], poly[(i+3-1) % #poly + 1]
local minx, maxx = math.min(x1, x2), math.max(x1, x2)
local miny, maxy = math.min(y1, y2), math.max(y1, y2)
if py > miny and py <= maxy then
if px > maxx then
collide = collide + 1
elseif px >= minx then
if x2 < x1 then
x1, x2 = x2, x1
y1, y2 = y2, y1
end
local xi = (px - x1) / (x2 - x1)
local yi = (py - y1) / (y2 - y1)
if xi > yi then
collide = collide + 1
end
end
end
end
return collide % 2 == 1
end