-
Notifications
You must be signed in to change notification settings - Fork 0
/
AABBPhysics.lua
346 lines (281 loc) · 10.2 KB
/
AABBPhysics.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
334
335
336
337
338
339
340
341
342
343
344
345
346
--Imports
local love = love
local math = math
local type = type
local table = table
local pairs = pairs
local print = print
local Class = require "Class"
local Vector2D = require "Vector2D"
local FloatCompare = require "FloatCompare"
local AABBPhysics = {}
setfenv(1, AABBPhysics)
local AABBBody = Class{}
function AABBBody:init(world, width, height)
self.world = world
self.width = width
self.height = height
self.position = Vector2D(0, 0)
self.lastPosition = Vector2D(0, 0)
self.speed = Vector2D(0, 0)
self.maxSpeed = Vector2D(-1, -1)
self.acceleration = Vector2D(0, 0)
self.immovable = false
self.overlapCallbackList = {}
self.collisionCallbackList = {}
end
function AABBBody:addOverlapCallback(collisionGroup, callback)
self.overlapCallbackList[collisionGroup] = callback
end
function AABBBody:addCollisionCallback(collisionGroup, callback)
self.collisionCallbackList[collisionGroup] = callback
end
local function limitValue (val, limit)
if limit >= 0 then
if val < 0 then
val = math.max(val, -limit)
else
val = math.min(val, limit)
end
end
return val
end
function AABBBody:update(dt)
self.lastPosition.x, self.lastPosition.y = self.position.x, self.position.y
self.position.x, self.position.y = self.position.x + self.speed.x * dt + self.acceleration.x * 0.5 * dt * dt,
self.position.y + self.speed.y * dt + self.acceleration.y * 0.5 * dt * dt
self.speed.x, self.speed.y = limitValue(self.speed.x + self.acceleration.x * dt, self.maxSpeed.x),
limitValue(self.speed.y + self.acceleration.y * dt, self.maxSpeed.y)
end
function AABBBody:destroy()
self.world:removeBody(self)
end
local AABBWorld = Class{}
local function getNewHandle(world)
if #world.freeHandles > 0 then
return table.remove(world.freeHandles)
else
local handle = world.nextHandle
world.nextHandle = world.nextHandle + 1
return handle
end
end
function AABBWorld:init()
self.nextHandle = 1
self.freeHandles = {}
self.bodyList = {}
self.collisionGroups = {}
end
function AABBWorld:createBody(width, height, collisionGroup)
local newBody = AABBBody(self, width, height)
newBody.handle = getNewHandle(self)
self.bodyList[newBody.handle] = newBody
if collisionGroup then
if not self.collisionGroups[collisionGroup] then
self.collisionGroups[collisionGroup] = {}
end
self.collisionGroups[collisionGroup][newBody.handle] = newBody
newBody.collisionGroup = collisionGroup
end
return newBody
end
function AABBWorld:queryBody(group, origin, radius)
local minDistance = math.huge
local target = nil
if self.collisionGroups[group] then
for _, body in pairs(self.collisionGroups[group]) do
if minDistance > (body.position.x - origin.x) ^ 2 + (body.position.y - origin.y) ^ 2 then
minDistance = (body.position.x - origin.x) ^ 2 + (body.position.y - origin.y) ^ 2
if minDistance < radius ^ 2 then
target = body
end
end
end
end
return target
end
function AABBWorld:raycast(origin, destiny, collisionGroup)
-- Makes destiny relative to origin
destiny:sub(origin)
local len = destiny:len()
local tMaxX = 0
local tMaxY = 0
if destiny.x > 0 then
tMaxX = (32 - origin.x % 32) / math.abs(destiny.x)
else
tMaxX = origin.x % 32 / math.abs(destiny.x)
end
if destiny.y > 0 then
tMaxY = (32 - origin.y % 32) / math.abs(destiny.y)
else
tMaxY = origin.y % 32 / math.abs(destiny.y)
end
local tDeltaX = 32 / math.abs(destiny.x)
local tDeltaY = 32 / math.abs(destiny.y)
local x = math.floor(origin.x / 32)
local y = math.floor(origin.y / 32)
local stepX = destiny.x / math.abs(destiny.x)
local stepY = destiny.y / math.abs(destiny.y)
while tMaxY < 1 or tMaxX < 1 do
if tMaxX < tMaxY then
tMaxX = tMaxX + tDeltaX
x = x + stepX
if self.tilemap.tiles[x + y * self.tilemap.widthInTiles + 1] > 1 then
return (tMaxX - tDeltaX) * len
end
else
tMaxY = tMaxY + tDeltaY
y = y + stepY
if self.tilemap.tiles[x + y * self.tilemap.widthInTiles + 1] > 1 then
return (tMaxY - tDeltaY) * len
end
end
end
return len
end
function AABBWorld:removeBody(body)
self.bodyList[body.handle] = nil
self.collisionGroups[body.collisionGroup][body.handle] = nil
end
function AABBWorld:loadTilemap(tilemap, collidableTiles, wrap)
self.tilemap = tilemap
for i = 1, #tilemap.tiles do
if collidableTiles[tilemap.tiles[i]] then
local row = math.floor((i - 1) / tilemap.widthInTiles)
local col = (i - 1) % tilemap.widthInTiles
local body = self:createBody(tilemap.tileWidth, tilemap.tileHeight, "tilemap")
body.position.x = col * tilemap.tileWidth
body.position.y = row * tilemap.tileHeight
body.immovable = true
end
end
if wrap then
for i = 0, tilemap.widthInTiles - 1 do
local body = self:createBody(tilemap.tileWidth, tilemap.tileHeight, "tilemap")
body.position.x = i * tilemap.tileWidth
body.position.y = -tilemap.tileHeight
body = self:createBody(tilemap.tileWidth, tilemap.tileHeight, "tilemap")
body.position.x = i * tilemap.tileWidth
body.position.y = 1080
end
for i = 0, tilemap.heightInTiles - 1 do
local body = self:createBody(tilemap.tileWidth, tilemap.tileHeight, "tilemap")
body.position.x = -tilemap.tileWidth
body.position.y = i * tilemap.tileHeight
body = self:createBody(tilemap.tileWidth, tilemap.tileHeight, "tilemap")
body.position.x = tilemap.widthInTiles * tilemap.tileWidth
body.position.y = i * tilemap.tileHeight
end
end
end
function AABBWorld:update(dt)
for handle, body in pairs(self.bodyList) do
body:update(dt)
end
end
function AABBWorld:draw()
for handle, body in pairs(self.bodyList) do
love.graphics.reset()
if not body.debug then
love.graphics.setColor(0, 255, 0, 255)
else
love.graphics.setColor(255, 0, 0, 255)
body.debug = false
end
love.graphics.rectangle("line", body.position.x, body.position.y, body.width, body.height)
end
end
local fc = FloatCompare()
local function overlapHitboxes (h1, h2)
if fc.lt(h1.position.x, h2.position.x + h2.width) and fc.gt(h1.position.x + h1.width, h2.position.x) and
fc.lt(h1.position.y, h2.position.y + h2.height) and fc.gt(h1.position.y + h1.height, h2.position.y) then
if h1.overlapCallbackList[h2.collisionGroup] then h1.overlapCallbackList[h2.collisionGroup](h1, h2) end
if h2.overlapCallbackList[h1.collisionGroup] then h2.overlapCallbackList[h1.collisionGroup](h2, h1) end
return true
else
return false
end
end
function AABBWorld:overlap (h1, h2)
if type(h1) == "string" then
if self.collisionGroups[h1] then
for _, hitbox in pairs(self.collisionGroups[h1]) do
self:overlap(hitbox, h2)
end
end
elseif type(h2) == "string" then
if self.collisionGroups[h2] then
for _, hitbox in pairs(self.collisionGroups[h2]) do
overlapHitboxes(h1, hitbox)
end
end
else
overlapHitboxes(h1, h2)
end
end
local delta = Vector2D(0, 0)
local entry = Vector2D(0, 0)
local entryDistance = Vector2D(0, 0)
local function collideHitboxes (h1, h2)
if overlapHitboxes(h1, h2) then
delta.x = (h1.position.x - h1.lastPosition.x) - (h2.position.x - h2.lastPosition.x)
delta.y = (h1.position.y - h1.lastPosition.y) - (h2.position.y - h2.lastPosition.y)
if not fc.eq(delta.x, 0) then
if fc.gt(delta.x, 0) then
entryDistance.x = h2.lastPosition.x - h1.lastPosition.x - h1.width
elseif fc.lt(delta.x, 0) then
entryDistance.x = h2.lastPosition.x + h2.width - h1.lastPosition.x
end
entry.x = entryDistance.x / delta.x
else
entry.x = -math.huge
end
if not fc.eq(delta.y, 0) then
if fc.gt(delta.y, 0) then
entryDistance.y = h2.lastPosition.y - h1.lastPosition.y - h1.height
elseif fc.lt(delta.y, 0) then
entryDistance.y = h2.lastPosition.y + h2.height - h1.lastPosition.y
end
entry.y = entryDistance.y / delta.y
else
entry.y = -math.huge
end
local entryTime = math.max(entry.x, entry.y)
if fc.ge(entryTime, 0) and fc.le(entryTime, 1) then
h1.position.x = h1.lastPosition.x + (h1.position.x - h1.lastPosition.x) * entryTime
h1.position.y = h1.lastPosition.y + (h1.position.y - h1.lastPosition.y) * entryTime
h2.position.x = h2.lastPosition.x + (h2.position.x - h2.lastPosition.x) * entryTime
h2.position.y = h2.lastPosition.y + (h2.position.y - h2.lastPosition.y) * entryTime
if fc.eq(entryTime, entry.x) then
h1.speed.x = 0
h2.speed.x = 0
else
h1.speed.y = 0
h2.speed.y = 0
end
if h1.collisionCallbackList[h2.collisionGroup] then h1.collisionCallbackList[h2.collisionGroup](h1, h2) end
if h2.collisionCallbackList[h1.collisionGroup] then h2.collisionCallbackList[h1.collisionGroup](h2, h1) end
end
end
end
function AABBWorld:collide (h1, h2)
if type(h1) == "string" then
if self.collisionGroups[h1] then
for _, hitbox in pairs(self.collisionGroups[h1]) do
self:collide(hitbox, h2)
end
end
elseif type(h2) == "string" then
if self.collisionGroups[h2] then
for _, hitbox in pairs(self.collisionGroups[h2]) do
collideHitboxes(h1, hitbox)
end
end
else
collideHitboxes(h1, h2)
end
end
function createWorld ()
return AABBWorld()
end
return AABBPhysics