-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathworld.lua
333 lines (299 loc) · 9.6 KB
/
world.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
-- breezefield: World.lua
--[[
World: has access to all the functions of love.physics.world
additionally stores all Collider objects assigned to it in
self.colliders (as key-value pairs)
can draw all its Colliders
by default, calls :collide on any colliders in it for postSolve
or for beginContact if the colliders are sensors
--]]
-- TODO make updating work from here too
-- TODO: update test and tutorial
local Collider = require((...):gsub('world', '') .. 'collider')
local set_funcs, lp, lg, COLLIDER_TYPES = unpack(
require((...):gsub('world', '') .. '/utils'))
local World = {}
World.__index = World
function World:new(...)
-- create a new physics world
--[[
inputs: (same as love.physics.newWorld)
xg: float, gravity in x direction
yg: float, gravity in y direction
sleep: boolean, whether bodies can sleep
outputs:
w: bf.World, the created world
]]--
local w = {}
setmetatable(w, self)
w._world = lp.newWorld(...)
set_funcs(w, w._world)
w.update = nil -- to use our custom update
w.colliders = {}
-- some functions defined here to use w without being passed it
function w.collide(obja, objb, coll_type, ...)
-- collision event for two Colliders
local function run_coll(obj1, obj2, ...)
if obj1[coll_type] ~= nil then
local e = obj1[coll_type](obj1, obj2, ...)
if type(e) == 'function' then
w.collide_events[#w.collide_events+1] = e
end
end
end
if obja ~= nil and objb ~= nil then
run_coll(obja, objb, ...)
run_coll(objb, obja, ...)
end
end
function w.enter(a, b, ...)
return w.collision(a, b, 'enter', ...)
end
function w.exit(a, b, ...)
return w.collision(a, b, 'exit', ...)
end
function w.preSolve(a, b, ...)
return w.collision(a, b, 'preSolve', ...)
end
function w.postSolve(a, b, ...)
return w.collision(a, b, 'postSolve', ...)
end
function w.collision(a, b, ...)
-- objects that hit one another can have collide methods
-- by default used as postSolve callback
local obja = a:getUserData(a)
local objb = b:getUserData(b)
w.collide(obja, objb, ...)
end
w:setCallbacks(w.enter, w.exit, w.preSolve, w.postSolve)
w.collide_events = {}
return w
end
function World:_remove(collider)
-- remove collider from table of tracked colliders (does NOT run proper destructors)
--[[
collider: collider to untrack
--]]
for i, col in ipairs(self.colliders) do
if col == collider then
table.remove(self.colliders, i)
break
end
end
self.colliders[collider] = nil
end
function World:draw(alpha, draw_over)
-- draw the world
--[[
alpha: sets the alpha of the drawing, defaults to 1
draw_over: draws the collision objects shapes even if their
.draw method is overwritten
--]]
local color = {love.graphics.getColor()}
if self._draw_order_changed then
table.sort(
self.colliders,
function(a, b) return a:getDrawOrder() < b:getDrawOrder() end
)
self._draw_order_changed = false
end
for _, c in ipairs(self.colliders) do
love.graphics.setColor(1, 1, 1, alpha or 1)
c:draw(alpha)
if draw_over then
love.graphics.setColor(1, 1, 1, alpha or 1)
c:__draw__()
end
end
love.graphics.setColor(color)
end
function World:queryRectangleArea(x1, y1, x2, y2)
-- query a bounding-box aligned area for colliders
--[[
inputs:
x1, y1, x2, y2: floats, the x and y coordinates of two points
outputs:
colls: table, all colliders in bounding box
--]]
local colls = {}
local callback = function(fixture)
table.insert(colls, fixture:getUserData())
return true
end
self:queryBoundingBox(x1, y1, x2, y2, callback)
return colls
end
local function check_vertices(vertices)
if #vertices % 2 ~= 0 then
error('vertices must be a multiple of 2')
elseif #vertices < 4 then
error('must have at least 2 vertices with x and y each')
end
end
local function is_edgy(colltype)
return colltype == COLLIDER_TYPES.POLY
or colltype == COLLIDER_TYPES.RECT
or colltype == COLLIDER_TYPES.EDGE
or colltype == COLLIDER_TYPES.CHAIN
end
local function any_intersections(coll1, coll2)
local vertices = {coll1:getSpatialIdentity()}
for i=1,#vertices-3,2 do
local x1, y1 = vertices[i], vertices[i+1]
local x2, y2 = vertices[i+2], vertices[i+3]
if (coll2:rayCast(x1, y1, x2, y2, 1) ~= nil)
or coll2:testPoint(x1, y1)
or coll2:testPoint(x2, y2)
then
return true
end
end
end
local function poly_circle_intersect(poly, circle)
if any_intersections(poly, circle) then
return true
end
return poly:testPoint(circle:getPosition())
or circle:testPoint(poly:getMassData())
end
local function poly_poly_intersect(poly1, poly2)
return any_intersections(poly1, poly2)
or any_intersections(poly2, poly1)
or poly1:testPoint(poly2:getMassData()) -- poly2 in poly1
or poly2:testPoint(poly2:getMassData()) -- poly1 in poly2
end
local function are_touching(coll1, coll2)
if coll1.collider_type == COLLIDER_TYPES.CIRCLE and is_edgy(coll2.collider_type) then
return are_touching(coll2, coll1)
end
if is_edgy(coll1.collider_type) and coll2.collider_type == COLLIDER_TYPES.CIRCLE then
return poly_circle_intersect(coll1, coll2)
end
if is_edgy(coll1.collider_type) and is_edgy(coll2.collider_type) then
return poly_poly_intersect(coll1, coll2)
end
if coll1.collider_type == COLLIDER_TYPES.CIRCLE and coll2.collider_type == COLLIDER_TYPES.CIRCLE then
return ((coll1:getX() - coll2:getX())^2 + (coll1:getY() - coll2:getY())) <=
coll1:getRadius() + coll2:getRadius()
end
error("collider types not recognized ".. tostring(coll1.collider_type)..', '..tostring(coll2.collider_type))
end
local function query_region(world, coll_type, args)
local collider = world:newCollider(coll_type, args)
collider:setSensor(true)
local colls = {}
local function callback(fixture)
local coll = fixture:getUserData()
if coll ~= collider then
if are_touching(collider, coll) then
table.insert(colls, coll)
end
end
return true
end
local ax, ay, bx, by = collider:getBoundingBox()
local in_bounding_box = world:queryBoundingBox(
ax, ay, bx, by, callback)
collider:destroy()
return colls
end
function World:_disable_callbacks()
self._callbacks = {self._world:getCallbacks()}
self._world:setCallbacks()
end
function World:_enable_callbacks()
self._world:setCallbacks(unpack(self._callbacks))
end
function World:queryPolygonArea(...)
-- query an area enclosed by the lines connecting a series of points
--[[
inputs:
x1, y1, x2, y2, ... floats, the x and y positions defining polygon
outputs:
colls: table, all Colliders intersecting the area
--]]
local vertices = {...}
if type(vertices[1]) == 'table' then
vertices = vertices[1]
end
check_vertices(vertices)
return query_region(self, COLLIDER_TYPES.POLYGON, vertices)
end
function World:queryCircleArea(x, y, r)
-- get all colliders in a circle are
--[[
inputs:
x, y, r: floats, x, y and radius of circle
outputs:
colls: table: colliders in area
]]--
return query_region(self, COLLIDER_TYPES.CIRCLE, {x, y, r})
end
function World:queryEdgeArea(...)
-- get all colliders along a (series of) line(s)
--[[
inputs:
x1, y1, x2, y2, ... floats, the x and y positions defining lines
outpts:
colls: table: colliders intersecting these lines
--]]
local vertices = {...}
if type(vertices[1]) == 'table' then
vertices = vertices[1]
end
check_vertices(vertices)
return query_region(self, 'Edge', vertices)
end
function World:update(dt)
-- update physics world
self._world:update(dt)
for i, v in pairs(self.collide_events) do
v()
self.collide_events[i] = nil
end
end
--[[
create a new collider in this world
args:
collider_type (string): the type of the collider (not case seinsitive). any of:
circle, rectangle, polygon, edge, chain.
shape_arguments (table): arguments required to instantiate shape.
circle: {x, y, radius}
rectangle: {x, y, width height}
polygon/edge/chain: {x1, y1, x2, y2, ...}
table_to_use (optional, table): table to generate as the collider
]]--
function World:newCollider(collider_type, shape_arguments, table_to_use)
local o = table_to_use or {}
setmetatable(o, Collider)
-- note that you will need to set static vs dynamic later
local _collider_type = COLLIDER_TYPES[collider_type:upper()]
assert(_collider_type ~= nil, "unknown collider type: "..collider_type)
collider_type = _collider_type
if collider_type == COLLIDER_TYPES.CIRCLE then
local x, y, r = unpack(shape_arguments)
o.body = lp.newBody(self._world, x, y, "dynamic")
o.shape = lp.newCircleShape(r)
elseif collider_type == "Rectangle" then
local x, y, w, h = unpack(shape_arguments)
o.body = lp.newBody(self._world, x, y, "dynamic")
o.shape = lp.newRectangleShape(w, h)
collider_type = "Polygon"
else
o.body = lp.newBody(self._world, 0, 0, "dynamic")
o.shape = lp['new'..collider_type..'Shape'](unpack(shape_arguments))
end
o.collider_type = collider_type
o.fixture = lp.newFixture(o.body, o.shape, 1)
o.fixture:setUserData(o)
set_funcs(o, o.body)
set_funcs(o, o.shape)
set_funcs(o, o.fixture)
-- index by self for now
o._world = self
table.insert(self.colliders, o)
self.colliders[o] = o
o:setDrawOrder(0)
return o
end
return World