-
Notifications
You must be signed in to change notification settings - Fork 2
/
physics.lua
627 lines (565 loc) · 15.9 KB
/
physics.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
--[[
PHYSICS LIBRARY THING
WRITTEN BY MAURICE GUÉGAN FOR MARI0
DON'T STEAL MY SHIT
]]--
function physicsupdate(dt)
local lobjects = objects
for j, w in pairs(lobjects) do
if j ~= "tile" and (not timefrozen or j == "player") then
for i, v in pairs(w) do
if v.static == false and v.active then
--GRAVITY
v.speedy = v.speedy + (v.gravity or yacceleration)*dt
if v.speedy > maxyspeed then
v.speedy = maxyspeed
end
--Standard conversion!
if v.gravitydirection and v.gravitydirection ~= math.pi/2 then
v.speedx, v.speedy = convertfromstandard(v, v.speedx, v.speedy)
end
--COLLISIONS ROFL
local horcollision = false
local vercollision = false
--VS OTHER OBJECTS --but not: portalwall, castlefirefire
for h, u in pairs(lobjects) do
local hor, ver = handlegroup(i, h, u, v, j, dt, passed)
if hor then
horcollision = true
end
if ver then
vercollision = true
end
end
--VS TILES (Because I only wanna check close ones)
local xstart = math.floor(v.x+v.speedx*dt-2/16)+1
local ystart = math.floor(v.y+v.speedy*dt-2/16)+1
local xfrom = xstart
local xto = xstart+math.ceil(v.width)
local dir = 1
if v.speedx < 0 then
xfrom, xto = xto, xfrom
dir = -1
end
for x = xfrom, xto, dir do
for y = ystart, ystart+math.ceil(v.height) do
--check if invisible block
if inmap(x, y) then
local t = lobjects["tile"][x .. "-" .. y]
if t then
-- Same object Active Not masked
if (i ~= g or j ~= h) and t.active and v.mask[t.category] ~= true then
local collision1, collision2 = checkcollision(v, t, "tile", x .. "-" .. y, j, i, dt, passed)
if collision1 then
horcollision = true
elseif collision2 then
vercollision = true
end
end
end
end
end
end
--Move the object
if vercollision == false then
v.y = v.y + v.speedy*dt
end
if horcollision == false then
v.x = v.x + v.speedx*dt
end
if v.gravitydirection and v.gravitydirection ~= math.pi/2 then
v.speedx, v.speedy = converttostandard(v, v.speedx, v.speedy)
end
if v.gravity then
if math.abs(v.speedy-v.gravity*dt)<0.00001 and v.speedy ~= 0 and v.startfall then
v:startfall(i)
end
else
if math.abs(v.speedy-yacceleration*dt)<0.00001 and v.speedy ~= 0 and v.startfall then
v:startfall(i)
end
end
end
end
end
end
end
function handlegroup(i, h, u, v, j, dt, passed)
local horcollision = false
local vercollision = false
for g, t in pairs(u) do
-- Same object? Active Not masked
if (i ~= g or j ~= h) and t.active and (v.mask == nil or v.mask[t.category] ~= true) and (t.mask == nil or t.mask[v.category] ~= true) then
local collision1, collision2 = checkcollision(v, t, h, g, j, i, dt, passed)
if collision1 then
horcollision = true
elseif collision2 then
vercollision = true
end
end
end
return horcollision, vercollision
end
function checkcollision(v, t, h, g, j, i, dt, passed) --v: b1table | t: b2table | h: b2type | g: b2id | j: b1type | i: b1id
local hadhorcollision = false
local hadvercollision = false
if math.abs(v.x-t.x) < math.max(v.width, t.width)+1 and math.abs(v.y-t.y) < math.max(v.height, t.height)+1 then
--check if it's a passive collision (Object is colliding anyway)
if not passed and aabb(v.x, v.y, v.width, v.height, t.x, t.y, t.width, t.height) then --passive collision! (oh noes!)
if passivecollision(v, t, h, g, j, i, dt) then
hadvercollision = true
end
elseif aabb(v.x + v.speedx*dt, v.y + v.speedy*dt, v.width, v.height, t.x, t.y, t.width, t.height) then
if aabb(v.x + v.speedx*dt, v.y, v.width, v.height, t.x, t.y, t.width, t.height) then --Collision is horizontal!
if horcollision(v, t, h, g, j, i, dt) then
hadhorcollision = true
end
elseif aabb(v.x, v.y+v.speedy*dt, v.width, v.height, t.x, t.y, t.width, t.height) then --Collision is vertical!
if vercollision(v, t, h, g, j, i, dt) then
hadvercollision = true
end
else
--We're fucked, it's a diagonal collision! run!
--Okay actually let's take this slow okay. Let's just see if we're moving faster horizontally than vertically, aight?
local grav = yacceleration
if self and self.gravity then
grav = self.gravity
end
if math.abs(v.speedy-grav*dt) < math.abs(v.speedx) then
--vertical collision it is.
if vercollision(v, t, h, g, j, i, dt) then
hadvercollision = true
end
else
--okay so we're moving mainly vertically, so let's just pretend it was a horizontal collision? aight cool.
if horcollision(v, t, h, g, j, i, dt) then
hadhorcollision = true
end
end
end
end
end
return hadhorcollision, hadvercollision
end
function passivecollision(v, t, h, g, j, i, dt)
if v.passivecollide then
v:passivecollide(h, t, i, g)
if t.passivecollide then
t:passivecollide(j, v, i, g)
end
else
if v.floorcollide then
if v:floorcollide(h, t, i, g) ~= false then
if v.speedy > 0 then
v.speedy = 0
end
v.y = t.y - v.height
return true
end
else
if v.speedy > 0 then
v.speedy = 0
end
v.y = t.y - v.height
return true
end
end
return false
end
function horcollision(v, t, h, g, j, i, dt)
if v.speedx < 0 then
--move object RIGHT (because it was moving left)
if collisionexists("right", t) then
if callcollision("right", t, j, v, g, i) ~= false then
if t.speedx and t.speedx > 0 then
t.speedx = 0
end
end
else
if t.speedx and t.speedx > 0 then
t.speedx = 0
end
end
if collisionexists("left", v) then
if callcollision("left", v, h, t, i, g) ~= false then
if v.speedx < 0 then
v.speedx = 0
end
v.x = t.x + t.width
return true
end
else
if v.speedx < 0 then
v.speedx = 0
end
v.x = t.x + t.width
return true
end
else
--move object LEFT (because it was moving right)
if collisionexists("left", t) then
if callcollision("left", t, j, v, g, i) ~= false then
if t.speedx and t.speedx < 0 then
t.speedx = 0
end
end
else
if t.speedx and t.speedx < 0 then
t.speedx = 0
end
end
if collisionexists("right", v) then
if callcollision("right", v, h, t, i, g) ~= false then
if v.speedx > 0 then
v.speedx = 0
end
v.x = t.x - v.width
return true
end
else
if v.speedx > 0 then
v.speedx = 0
end
v.x = t.x - v.width
return true
end
end
return false
end
function vercollision(v, t, h, g, j, i, dt)
if v.speedy < 0 then
--move object DOWN (because it was moving up)
if collisionexists("floor", t) then
if callcollision("floor", t, j, v, g, i) ~= false then
if t.speedy and t.speedy > 0 then
t.speedy = 0
end
end
else
if t.speedy and t.speedy > 0 then
t.speedy = 0
end
end
if collisionexists("ceil", v) then
if callcollision("ceil", v, h, t, i, g) ~= false then
if v.speedy < 0 then
v.speedy = 0
end
v.y = t.y + t.height
return true
end
else
if v.speedy < 0 then
v.speedy = 0
end
v.y = t.y + t.height
return true
end
else
if collisionexists("ceil", t) then
if callcollision("ceil", t, j, v, g, i) ~= false then
if t.speedy and t.speedy < 0 then
t.speedy = 0
end
end
else
if t.speedy and t.speedy < 0 then
t.speedy = 0
end
end
if collisionexists("floor", v) then
if callcollision("floor", v, h, t, i, g) ~= false then
if v.speedy > 0 then
v.speedy = 0
end
v.y = t.y - v.height
return true
end
else
if v.speedy > 0 then
v.speedy = 0
end
v.y = t.y - v.height
return true
end
end
return false
end
function collisionscalls(dir, obj, a, b, c, d)
local r
if obj.gravitydirection > math.pi/4*1 and obj.gravitydirection <= math.pi/4*3 then
if dir == "floor" then
if obj.floorcollide then
r = obj:floorcollide(a, b, c, d)
end
elseif dir == "left" then
if obj.leftcollide then
r = obj:leftcollide(a, b, c, d)
end
elseif dir == "ceil" then
if obj.ceilcollide then
r = obj:ceilcollide(a, b, c, d)
end
elseif dir == "right" then
if obj.rightcollide then
r = obj:rightcollide(a, b, c, d)
end
end
elseif obj.gravitydirection > math.pi/4*3 and obj.gravitydirection <= math.pi/4*5 then
if dir == "floor" then
if obj.rightcollide then
r = obj:rightcollide(a, b, c, d)
end
elseif dir == "left" then
if obj.floorcollide then
r = obj:floorcollide(a, b, c, d)
end
elseif dir == "ceil" then
if obj.leftcollide then
r = obj:leftcollide(a, b, c, d)
end
elseif dir == "right" then
if obj.ceilcollide then
r = obj:ceilcollide(a, b, c, d)
end
end
elseif obj.gravitydirection > math.pi/4*5 and obj.gravitydirection <= math.pi/4*7 then
if dir == "floor" then
if obj.ceilcollide then
r = obj:ceilcollide(a, b, c, d)
end
elseif dir == "left" then
if obj.rightcollide then
r = obj:rightcollide(a, b, c, d)
end
elseif dir == "ceil" then
if obj.floorcollide then
r = obj:floorcollide(a, b, c, d)
end
elseif dir == "right" then
if obj.leftcollide then
r = obj:leftcollide(a, b, c, d)
end
end
else
if dir == "floor" then
if obj.leftcollide then
r = obj:leftcollide(a, b, c, d)
end
elseif dir == "left" then
if obj.ceilcollide then
r = obj:ceilcollide(a, b, c, d)
end
elseif dir == "ceil" then
if obj.rightcollide then
r = obj:rightcollide(a, b, c, d)
end
elseif dir == "right" then
if obj.floorcollide then
r = obj:floorcollide(a, b, c, d)
end
end
end
return r
end
function callcollision(dir, obj, a, b, c, d)
if not obj.gravitydirection then
if dir == "floor" then
return obj:floorcollide(a, b, c, d)
elseif dir == "left" then
return obj:leftcollide(a, b, c, d)
elseif dir == "ceil" then
return obj:ceilcollide(a, b, c, d)
elseif dir == "right" then
return obj:rightcollide(a, b, c, d)
end
end
obj.speedx, obj.speedy = converttostandard(obj, obj.speedx, obj.speedy)
local r
r = collisionscalls(dir, obj, a, b, c, d)
obj.speedx, obj.speedy = convertfromstandard(obj, obj.speedx, obj.speedy)
return r
end
function collisionexists(dir, obj)
if not obj.gravitydirection or (obj.gravitydirection > math.pi/4*1 and obj.gravitydirection <= math.pi/4*3) then
if dir == "floor" then
return obj.floorcollide
elseif dir == "left" then
return obj.leftcollide
elseif dir == "ceil" then
return obj.ceilcollide
elseif dir == "right" then
return obj.rightcollide
end
elseif obj.gravitydirection > math.pi/4*3 and obj.gravitydirection <= math.pi/4*5 then
if dir == "floor" then
return obj.rightcollide
elseif dir == "left" then
return obj.floorcollide
elseif dir == "ceil" then
return obj.leftcollide
elseif dir == "right" then
return obj.ceilcollide
end
elseif obj.gravitydirection > math.pi/4*5 and obj.gravitydirection <= math.pi/4*7 then
if dir == "floor" then
return obj.ceilcollide
elseif dir == "left" then
return obj.rightcollide
elseif dir == "ceil" then
return obj.floorcollide
elseif dir == "right" then
return obj.leftcollide
end
else
if dir == "floor" then
return obj.leftcollide
elseif dir == "left" then
return obj.ceilcollide
elseif dir == "ceil" then
return obj.rightcollide
elseif dir == "right" then
return obj.floorcollide
end
end
end
function adjustcollside(side, gravitydirection)
if side == "left" then
if gravitydirection > math.pi/4*1 and gravitydirection <= math.pi/4*3 then --down
return "left"
elseif gravitydirection > math.pi/4*3 and gravitydirection <= math.pi/4*5 then --left
return "up"
elseif gravitydirection > math.pi/4*5 and gravitydirection <= math.pi/4*7 then --up
return "right"
else --right
return "down"
end
elseif side == "up" then
if gravitydirection > math.pi/4*1 and gravitydirection <= math.pi/4*3 then --down
return "up"
elseif gravitydirection > math.pi/4*3 and gravitydirection <= math.pi/4*5 then --left
return "right"
elseif gravitydirection > math.pi/4*5 and gravitydirection <= math.pi/4*7 then --up
return "down"
else --right
return "left"
end
elseif side == "right" then
if gravitydirection > math.pi/4*1 and gravitydirection <= math.pi/4*3 then --down
return "right"
elseif gravitydirection > math.pi/4*3 and gravitydirection <= math.pi/4*5 then --left
return "down"
elseif gravitydirection > math.pi/4*5 and gravitydirection <= math.pi/4*7 then --up
return "left"
else --right
return "up"
end
elseif side == "down" then
if gravitydirection > math.pi/4*1 and gravitydirection <= math.pi/4*3 then --down
return "down"
elseif gravitydirection > math.pi/4*3 and gravitydirection <= math.pi/4*5 then --left
return "left"
elseif gravitydirection > math.pi/4*5 and gravitydirection <= math.pi/4*7 then --up
return "up"
else --right
return "right"
end
end
end
function aabb(ax, ay, awidth, aheight, bx, by, bwidth, bheight)
return ax+awidth > bx and ax < bx+bwidth and ay+aheight > by and ay < by+bheight
end
function checkrect(x, y, width, height, list, statics)
local out = {}
local inobj
if type(list) == "table" and list[1] == "exclude" then
inobj = list[2]
list = "all"
end
for i, v in pairs(objects) do
local contains = false
if list and list ~= "all" then
for j = 1, #list do
if list[j] == i then
contains = true
end
end
end
if list == "all" or contains then
for j, w in pairs(v) do
if statics or w.static ~= true or list ~= "all" then
local skip = false
if inobj then
if w.x == inobj.x and w.y == inobj.y then
skip = true
end
--masktable
if (inobj.mask ~= nil and inobj.mask[w.category] == true) or (w.mask ~= nil and w.mask[inobj.category] == true) then
skip = true
end
end
if not skip then
if w.active then
if aabb(x, y, width, height, w.x, w.y, w.width, w.height) then
table.insert(out, i)
table.insert(out, j)
end
end
end
end
end
end
end
return out
end
function converttostandard(obj, speedx, speedy)
--Convert speedx and speedy to horizontal values
local speed = math.sqrt(speedx^2+speedy^2)
local speeddir = math.atan2(speedy, speedx)
local speedx, speedy = math.cos(speeddir-obj.gravitydirection+math.pi/2)*speed, math.sin(speeddir-obj.gravitydirection+math.pi/2)*speed
if math.abs(speedy) < 0.00001 then
speedy = 0
end
if math.abs(speedx) < 0.00001 then
speedx = 0
end
return speedx, speedy
end
function convertfromstandard(obj, speedx, speedy)
--reconvert speedx and speedy to actual directions
local speed = math.sqrt(speedx^2+speedy^2)
local speeddir = math.atan2(speedy, speedx)
local speedx, speedy = math.cos(speeddir+obj.gravitydirection-math.pi/2)*speed, math.sin(speeddir+obj.gravitydirection-math.pi/2)*speed
if math.abs(speedy) < 0.00001 then
speedy = 0
end
if math.abs(speedx) < 0.00001 then
speedx = 0
end
return speedx, speedy
end
function unrotate(rotation, gravitydirection, dt)
--rotate back to gravitydirection (portals)
rotation = math.fmod(rotation, math.pi*2)
if rotation < -math.pi then
rotation = rotation + math.pi*2
elseif rotation > math.pi then
rotation = rotation - math.pi*2
end
if rotation == math.pi and gravitydirection == 0 then
rotation = rotation + portalrotationalignmentspeed*dt
elseif rotation <= -math.pi/2 and gravitydirection == math.pi*1.5 then
rotation = rotation - portalrotationalignmentspeed*dt
elseif rotation > (gravitydirection-math.pi/2) then
rotation = rotation - portalrotationalignmentspeed*dt
if rotation < (gravitydirection-math.pi/2) then
rotation = (gravitydirection-math.pi/2)
end
elseif rotation < (gravitydirection-math.pi/2) then
rotation = rotation + portalrotationalignmentspeed*dt
if rotation > (gravitydirection-math.pi/2) then
rotation = (gravitydirection-math.pi/2)
end
end
return rotation
end