-
Notifications
You must be signed in to change notification settings - Fork 1
/
Strike.lua
162 lines (143 loc) · 4.53 KB
/
Strike.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
local Vec = _Require_relative( ... , 'lib.DeWallua.vector-light')
local Shapes = _Require_relative( ... , 'shapes')
local Colliders = _Require_relative( ... , 'colliders')
local Collider = _Require_relative( ... , 'colliders.Collider')
---@type MTV
local MTV = _Require_relative(..., 'classes.MTV')
local contact = _Require_relative(..., 'algs.contact')
local SAT = _Require_relative(..., 'algs.SAT')
-- [[--- Broadphase Functions ---]] --
---Determine if two circles are colliding using their coordinates and radii
---@param collider1 Collider
---@param collider2 Collider
---@return boolean
local function circle_circle(collider1, collider2)
local x1,y1 = collider1.centroid.x, collider1.centroid.y
local x2,y2 = collider2.centroid.x, collider2.centroid.y
local r1,r2 = collider1.radius, collider2.radius
return (x2 - x1)^2 + (y2 - y1)^2 <= (r1 + r2)^2
end
---Returns true if two bounding boxes overlap
---@param collider1 Collider
---@param collider2 Collider
---@return boolean
local function aabb_aabb(collider1, collider2)
local rect_1_x, rect_1_y, rect_1_w, rect_1_h = collider1:getBbox()
local rect_2_x, rect_2_y, rect_2_w, rect_2_h = collider2:getBbox()
return (
rect_1_x < rect_2_x + rect_2_w and
rect_1_x + rect_1_w > rect_2_x and
rect_1_y < rect_2_y + rect_2_h and
rect_1_y + rect_1_h > rect_2_y
)
end
---Check if two colliders are intersecting
---@param collider1 Collider
---@param collider2 Collider
---@return MTV | boolean
local function striking(collider1, collider2)
local collision, max_mtv = false, MTV(0, 0)
local mtv
for shape1 in collider1:elems() do
for shape2 in collider2:elems() do
mtv = SAT(shape1, shape2)
if not mtv.separating and mtv:mag2() > max_mtv:mag2() then
max_mtv = mtv
collision = true
end
end
end
return collision and max_mtv or not true
end
---Translate both colliders (contained in MTV) equally away from each other
---@param mtv MTV
local function settle(mtv)
local collider, collided = mtv.colliderShape:getRoot(), mtv.collidedShape:getRoot()
collider:translate( Vec.mul(-.5, mtv.x, mtv.y))
collided:translate( Vec.mul(0.5, mtv.x, mtv.y))
end
---Translate mtv.collided by full mtv (good for edge collision)
---@param mtv MTV
local function shove(mtv)
local collided = mtv.collidedShape:getRoot()
collided:translate( mtv.x, mtv.y )
end
---Draw an mtv w/ LOVE
---@param mtv MTV
local function show_mtv(mtv)
if mtv.separating then
return
end
local cx, cy = mtv.colliderShape:getCentroid()
local ox, oy = mtv.collidedShape:getCentroid()
local edge = mtv.colliderShape:getEdge(mtv.edgeIndex)
local ex, ey = (edge[1] + edge[3])/2, (edge[2] + edge[4])/2
love.graphics.setColor(.5,.5,1)
love.graphics.line(cx, cy, ex+mtv.x, ey+mtv.y)
love.graphics.setColor(1,.5,.5)
love.graphics.line(cx, cy, ox, oy)
love.graphics.setColor(1,0,.5)
love.graphics.line(unpack(edge))
love.graphics.setColor(1,1,1)
end
---Draw Collider normals
---@param collider Collider
---@param len number length of lines to draw @ normals
local function show_norms(collider, len)
len = len or 15
for _, shape in collider:ipairs() do
for _, edge in shape:ipairs() do
local x, y = Vec.div(2, Vec.add(edge[1], edge[2], edge[3], edge[4]) )
local nx, ny = Vec.mul(len, Vec.normalize(Vec.sub(edge[3], edge[4], edge[1], edge[2])))
nx, ny = ny, -nx
love.graphics.line(x, y, x+nx, y+ny)
end
end
end
---Project mtv's collided field on normal
---@param mtv MTV
local function show_proj(mtv)
for _, shape in mtv.collided:ipairs() do
local cx, cy = shape:getCentroid()
local nmx, nmy = Vec.normalize(mtv.x, mtv.y)
local smin, smax = shape:project(nmx, nmy)
love.graphics.setColor(.5,.5,.1)
love.graphics.line(cx + nmx*smin, cy + nmy*smin, cx + nmx*smax, cy + nmy*smax)
love.graphics.setColor(1,1,1)
end
end
-- [[---------------------]] Strike API Table [[---------------------]] --
-- Strike table
local S = {}
-- Add shapes
S.hapes = Shapes
-- Add collider definition for each shape to Colliders
for name, shape in pairs(Shapes) do
Colliders[name] = function(...)
return Collider( shape(...) )
end
end
-- Add colliders
S.trikers = Colliders
-- Broadphase functions
S.aabb = aabb_aabb
S.ircle = circle_circle
-- Add collison functions
S.AT = SAT
S.triking = striking
S.ettle = settle
S.hove = shove
S.ite = contact
-- Functions to draw collision info
S.howMTV = show_mtv
S.howNorms = show_norms
S.howProj = show_proj
-- Config
function S.eePoolSize()
return MTV:getPoolSize()
end
function S.etPoolSize(limit)
MTV:setPoolSize(limit)
end
-- Actually return Strike!
return S