-
Notifications
You must be signed in to change notification settings - Fork 107
/
physics.lua
935 lines (792 loc) · 25.5 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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
--[[
PHYSICS LIBRARY THING
WRITTEN BY MAURICE GUÉGAN FOR MARI0
DON'T STEAL MY SHIT
Licensed under the same license as the game itself.
]]--
--MASK REFERENCE LIST
---1: *ALWAYS NOT COLLIDE*
---2: WORLD
---3: MARIO
---4: GOOMBA
---5: KOOPA
---6: MUSHROOM/ONEUP/FLOWER/STAR
---7: GEL DISPENSER
---8: GEL
---9: BOX
--10: SCREENBOUNDARIES
--11: BULLETBILL
--12: PORTALWALLS
--13: FIREBALLS
--14: HAMMERS
--15: PLATFORMS/SEESAWS
--16: BOWSER
--17: FIRE
--18: VINE
--19: SPRING
--20: HAMMERBROS
--21: LAKITO
--22: BUTTON --Not used anymore
--23: CASTLEFIRE
--24: CHEEP CHEEP
--25: DOOR
--26: FAITHPLATE
--27: FLYINGFISH
--28: LIGHTBRIDGE
--29: PLANT
--30: SQUID
--31: UPFIRE
function physicsupdate(dt)
local lobjects = objects
for j, w in pairs(lobjects) do
if j ~= "tile" 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
--PORTALS LOL
local passed = false
if v.portalable ~= false then
if not checkportalVER(v, v.x+v.speedx*dt) then
if checkportalHOR(v, v.y+v.speedy*dt) then
passed = true
end
else
passed = true
end
if passed and j == "player" then
playsound(portalentersound)
end
end
--COLLISIONS ROFL
local horcollision = false
local vercollision = false
--VS OTHER OBJECTS --but not portalwall
for h, u in pairs(lobjects) do
if h ~= "tile" and h ~= "portalwall" then
local hor, ver = handlegroup(i, h, u, v, j, dt, passed)
if hor then
horcollision = true
end
if ver then
vercollision = true
end
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) and (not tilequads[map[x][y][1]].invisible or j == "player") 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
--VS PORTALWALL
local h = "portalwall"
local u = objects["portalwall"]
local hor, ver = handlegroup(i, h, u, v, j, dt, passed)
if hor then
horcollision = true
end
if ver then
vercollision = true
end
--Check for emancipation grill
if v.emancipatecheck then
for h, u in pairs(emancipationgrills) do
if u.dir == "hor" then
if inrange(v.x+6/16, u.startx-1, u.endx, true) and inrange(u.y-14/16, v.y, v.y+v.speedy*dt, true) then
v:emancipate(h)
end
else
if inrange(v.y+6/16, u.starty-1, u.endy, true) and inrange(u.x-14/16, v.x, v.x+v.speedx*dt, true) then
v:emancipate(h)
end
end
end
end
--Move the object
if vercollision == false then
v.y = v.y + v.speedy*dt
if v.gravity then
if v.speedy == v.gravity*dt and v.startfall then
v:startfall(i)
end
else
if v.speedy == yacceleration*dt and v.startfall then
v:startfall(i)
end
end
end
if horcollision == false then
v.x = v.x + v.speedx*dt
end
--check if object is inside portal
if v.portalable ~= false then
inportal(v)
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)
if t.passivecollide then
t:passivecollide(j, v)
end
else
if v.floorcollide then
if v:floorcollide(h, t, dt) ~= 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 t.rightcollide then
if t:rightcollide(j, v) ~= 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 v.leftcollide then
if v:leftcollide(h, t) ~= 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 t.leftcollide then
if t:leftcollide(j, v) ~= 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 v.rightcollide then
if v:rightcollide(h, t) ~= 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 t.floorcollide then
if t:floorcollide(j, v) ~= 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 v.ceilcollide then
if v:ceilcollide(h, t) ~= 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 t.ceilcollide then
if t:ceilcollide(j, v) ~= 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 v.floorcollide then
if v:floorcollide(h, t, dt) ~= 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 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 inportal(self)
if self.mask[2] then
return
end
for i, v in pairs(objects["player"]) do
if v.portal1X ~= false and v.portal2X ~= false then
local portal1xplus = 0
local portal2xplus = 0
local portal1Y = v.portal1Y
local portal2Y = v.portal2Y
local portal1yplus = 0
local portal2yplus = 0
local portal1X = v.portal1X
local portal2X = v.portal2X
--Get the extra block of each portal
if v.portal1facing == "up" then
portal1xplus = 1
elseif v.portal1facing == "down" then
portal1xplus = -1
end
if v.portal2facing == "up" then
portal2xplus = 1
elseif v.portal2facing == "down" then
portal2xplus = -1
end
if v.portal1facing == "right" then
portal1yplus = 1
elseif v.portal1facing == "left" then
portal1yplus = -1
end
if v.portal2facing == "right" then
portal2yplus = 1
elseif v.portal2facing == "left" then
portal2yplus = -1
end
local x = math.floor(self.x+self.width/2)+1
local y = math.floor(self.y+self.height/2)+1
if (x == portal1X or x == portal1X + portal1xplus) and (y == portal1Y or y == portal1Y + portal1yplus) then
local entryportalX = v.portal1X
local entryportalY = v.portal1Y
local entryportalfacing = v.portal1facing
local exitportalX = v.portal2X
local exitportalY = v.portal2Y
local exitportalfacing = v.portal2facing
self.x, self.y, self.speedx, self.speedy, self.rotation = portalcoords(self.x, self.y, self.speedx, self.speedy, self.width, self.height, self.rotation, self.animationdirection, entryportalX, entryportalY, entryportalfacing, exitportalX, exitportalY, exitportalfacing, self, true)
elseif (x == portal2X or x == portal2X + portal2xplus) and (y == portal2Y or y == portal2Y + portal2yplus) then
local entryportalX = v.portal2X
local entryportalY = v.portal2Y
local entryportalfacing = v.portal2facing
local exitportalX = v.portal1X
local exitportalY = v.portal1Y
local exitportalfacing = v.portal1facing
self.x, self.y, self.speedx, self.speedy, self.rotation = portalcoords(self.x, self.y, self.speedx, self.speedy, self.width, self.height, self.rotation, self.animationdirection, entryportalX, entryportalY, entryportalfacing, exitportalX, exitportalY, exitportalfacing, self)
end
end
end
return false
end
function checkportalHOR(self, nextY) --handles horizontal (up- and down facing) portal teleportation
for i, v in pairs(objects["player"]) do
if v.portal1X ~= false and v.portal2X ~= false then
local portal1xplus = 0
local portal2xplus = 0
local portal1Y = v.portal1Y
local portal2Y = v.portal2Y
--Get the extra block of each portal
if v.portal1facing == "up" then
portal1xplus = 1
portal1Y = portal1Y - 1
elseif v.portal1facing == "down" then
portal1xplus = -1
end
if v.portal2facing == "up" then
portal2xplus = 1
portal2Y = portal2Y - 1
elseif v.portal2facing == "down" then
portal2xplus = -1
end
--first part checks whether object is in the portal's x range, second part whether object just moved through the portal's Y value
if ((v.portal1X == math.floor(self.x+1) or v.portal1X+portal1xplus == math.floor(self.x+1)) and inrange(portal1Y, self.y+self.height/2, nextY+self.height/2))
or ((v.portal2X == math.floor(self.x+1) or v.portal2X+portal2xplus == math.floor(self.x+1)) and inrange(portal2Y, self.y+self.height/2, nextY+self.height/2)) then
--check which portal is entry
local entryportalX, entryportalY, entryportalfacing
local exitportalX, exitportalY, exitportalfacing
local entryportalxplus, entryportalyplus, exitportalxplus, exitportalyplus
if (v.portal1X == math.floor(self.x+1) or v.portal1X+portal1xplus == math.floor(self.x+1)) and inrange(portal1Y, self.y+self.height/2, nextY+self.height/2) then
entryportalX = v.portal1X
entryportalY = v.portal1Y
entryportalfacing = v.portal1facing
entryportalxplus = portal1xplus
exitportalX = v.portal2X
exitportalY = v.portal2Y
exitportalfacing = v.portal2facing
exitportalxplus = portal2xplus
else
entryportalX = v.portal2X
entryportalY = v.portal2Y
entryportalfacing = v.portal2facing
entryportalxplus = portal2xplus
exitportalX = v.portal1X
exitportalY = v.portal1Y
exitportalfacing = v.portal1facing
exitportalxplus = portal1xplus
end
--check if movement makes that portal even a possibility
if entryportalfacing == "up" then
if self.speedy < 0 then
return false
end
elseif entryportalfacing == "down" then
if self.speedy > 0 then
return false
end
end
if entryportalfacing == "left" or entryportalfacing == "right" then
return false
end
local testx, testy, testspeedx, testspeedy, testrotation = portalcoords(self.x, self.y, self.speedx, self.speedy, self.width, self.height, self.rotation, self.animationdirection, entryportalX, entryportalY, entryportalfacing, exitportalX, exitportalY, exitportalfacing, self, true)
if #checkrect(testx, testy, self.width, self.height, {"exclude", self}) == 0 then
self.x, self.y, self.speedx, self.speedy, self.rotation = testx, testy, testspeedx, testspeedy, testrotation
else
self.speedy = -self.speedy*0.95
if math.abs(self.speedy) < 2 then
if self.speedy > 0 then
self.speedy = 2
else
self.speedy = -2
end
end
end
if (entryportalfacing == "down" and exitportalfacing == "up") or (entryportalfacing == "up" and exitportalfacing == "down") then
else
self.jumping = false
self.falling = true
end
if self.portaled then
self:portaled(exitportalfacing)
end
return true
end
end
end
return false
end
function checkportalVER(self, nextX) --handles vertical (left- and right facing) portal teleportation
for i, v in pairs(objects["player"]) do
if v.portal1X ~= false and v.portal2X ~= false then
local portal1yplus = 0
local portal2yplus = 0
local portal1X = v.portal1X
local portal2X = v.portal2X
--Get the extra block of each portal
if v.portal1facing == "right" then
portal1yplus = 1
elseif v.portal1facing == "left" then
portal1yplus = -1
portal1X = portal1X - 1
end
if v.portal2facing == "right" then
portal2yplus = 1
elseif v.portal2facing == "left" then
portal2yplus = -1
portal2X = portal2X - 1
end
if ((v.portal1Y == math.floor(self.y+1) or v.portal1Y+portal1yplus == math.floor(self.y+1)) and inrange(portal1X, self.x+self.width/2, nextX+self.width/2))
or ((v.portal2Y == math.floor(self.y+1) or v.portal2Y+portal2yplus == math.floor(self.y+1)) and inrange(portal2X, self.x+self.width/2, nextX+self.width/2)) then
--check which portal is entry
local entryportalX, entryportalY, entryportalfacing
local exitportalX, exitportalY, exitportalfacing
local entryportalxplus, entryportalyplus, exitportalxplus, exitportalyplus
if (v.portal1Y == math.floor(self.y+1) or v.portal1Y+portal1yplus == math.floor(self.y+1)) and inrange(portal1X, self.x+self.width/2, nextX+self.width/2) then
entryportalX = v.portal1X
entryportalY = v.portal1Y
entryportalfacing = v.portal1facing
entryportalyplus = portal1yplus
exitportalX = v.portal2X
exitportalY = v.portal2Y
exitportalfacing = v.portal2facing
exitportalyplus = portal2yplus
else
entryportalX = v.portal2X
entryportalY = v.portal2Y
entryportalfacing = v.portal2facing
entryportalyplus = portal2yplus
exitportalX = v.portal1X
exitportalY = v.portal1Y
exitportalfacing = v.portal1facing
exitportalyplus = portal1yplus
end
--check if movement makes that portal even a possibility
if entryportalfacing == "right" then
if self.speedx > 0 then
return false
end
elseif entryportalfacing == "left" then
if self.speedx < 0 then
return false
end
end
if entryportalfacing == "up" or entryportalfacing == "down" then
return false
end
local testx, testy, testspeedx, testspeedy, testrotation = portalcoords(self.x, self.y, self.speedx, self.speedy, self.width, self.height, self.rotation, self.animationdirection, entryportalX, entryportalY, entryportalfacing, exitportalX, exitportalY, exitportalfacing, self, true)
if #checkrect(testx, testy, self.width, self.height, {"exclude", self}) == 0 then
self.x, self.y, self.speedx, self.speedy, self.rotation = testx, testy, testspeedx, testspeedy, testrotation
else
self.speedx = -self.speedx
end
self.jumping = false
self.falling = true
if self.portaled then
self:portaled(exitportalfacing)
end
return true
end
end
end
return false
end
function portalcoords(x, y, speedx, speedy, width, height, rotation, animationdirection, entryportalX, entryportalY, entryportalfacing, exitportalX, exitportalY, exitportalfacing, self, live)
--uuuuuuuuuuuuuh
--rewrite this so it takes the CENTER of shit and makes stuff according to that, also relative offsets
x = x + width/2
y = y + height/2
local directrange --vector orthogonal to portal vector T
local relativerange --vector symmetrical to portal vector =
if entryportalfacing == "up" then
directrange = entryportalY - y - 1
if width == 2 then
relativerange = 0
else
relativerange = ((x-width/2) - entryportalX + 1) / (2-width)
end
elseif entryportalfacing == "right" then
directrange = x - entryportalX
if height == 2 then
relativerange = 0
else
relativerange = ((y-height/2) - entryportalY + 1) / (2-height)
end
elseif entryportalfacing == "down" then
directrange = y - entryportalY
if width == 2 then
relativerange = 0
else
relativerange = ((x-width/2) - entryportalX + 2) / (2-width)
end
elseif entryportalfacing == "left" then
directrange = entryportalX - x - 1
if height == 2 then
relativerange = 0
else
relativerange = ((y-height/2) - entryportalY + 2) / (2-height)
end
end
if entryportalfacing == "up" and exitportalfacing == "up" then --up -> up
newx = x + (exitportalX - entryportalX)
newy = exitportalY + directrange - 1
speedy = -speedy
rotation = rotation - math.pi
if live then
local grav = yacceleration
if self and self.gravity then
grav = self.gravity
end
--keep it from bugging out by having a minimum exit speed
local minspeed = math.sqrt(2*grav*(height))
if speedy > -minspeed then
speedy = -minspeed
end
end
elseif (entryportalfacing == "down" and exitportalfacing == "down") then --down -> down
newx = x + (exitportalX - entryportalX)
newy = exitportalY - directrange
speedy = -speedy
rotation = rotation - math.pi
elseif entryportalfacing == "up" and exitportalfacing == "right" then --up -> right
newy = exitportalY - relativerange*(2-height) - height/2 + 1
newx = exitportalX - directrange
speedx, speedy = speedy, -speedx
rotation = rotation - math.pi/2
elseif entryportalfacing == "up" and exitportalfacing == "left" then --up -> left
newy = exitportalY + relativerange*(2-height) + height/2 - 2
newx = exitportalX + directrange - 1
speedx, speedy = -speedy, speedx
rotation = rotation + math.pi/2
elseif (entryportalfacing == "up" and exitportalfacing == "down") then --up -> down
newx = x + (exitportalX - entryportalX) - 1
newy = exitportalY - directrange
--prevent low-fps bugs in a cheap way:
if entryportalY > exitportalY then
while newy+.5 + speedy*gdt > entryportalY do
newy = newy - 0.01
end
while newy+.5 < exitportalY do
newy = newy + 0.01
end
end
--prevent porting into block by limiting X, yo
if newx <= exitportalX - 2 + width/2 then
newx = exitportalX - 2 + width/2
elseif newx > exitportalX - width/2 then
newx = exitportalX - width/2
end
elseif (entryportalfacing == "down" and exitportalfacing == "up") then --down -> up
newx = x + (exitportalX - entryportalX) + 1
newy = exitportalY + directrange - 1
elseif (entryportalfacing == "down" and exitportalfacing == "left") then --down -> left
newy = exitportalY - relativerange*(2-height) - height/2
newx = exitportalX + directrange - 1
speedx, speedy = speedy, -speedx
rotation = rotation - math.pi/2
elseif (entryportalfacing == "down" and exitportalfacing == "right") then --down -> right
newy = exitportalY + relativerange*(2-height) + height/2 - 1
newx = exitportalX - directrange
speedx, speedy = -speedy, speedx
rotation = rotation + math.pi/2
--LEFT/RIGHT CODE!
elseif (entryportalfacing == "left" and exitportalfacing == "right") then --left -> right
newx = exitportalX - directrange
newy = y + (exitportalY - entryportalY)+1
elseif (entryportalfacing == "right" and exitportalfacing == "left") then --right -> left
newx = exitportalX + directrange - 1
newy = y + (exitportalY - entryportalY)-1
elseif (entryportalfacing == "right" and exitportalfacing == "right") then --right -> right
newx = exitportalX - directrange
newy = y + (exitportalY - entryportalY)
speedx = -speedx
if animationdirection == "left" then
animationdirection = "right"
elseif animationdirection == "right" then
animationdirection = "left"
end
elseif (entryportalfacing == "left" and exitportalfacing == "left") then --left -> left
newx = exitportalX + directrange - 1
newy = y + (exitportalY - entryportalY)
speedx = -speedx
if animationdirection == "left" then
animationdirection = "right"
elseif animationdirection == "right" then
animationdirection = "left"
end
elseif (entryportalfacing == "left" and exitportalfacing == "up") then --left -> up
newx = exitportalX + relativerange*(2-width) + width/2 - 1
newy = exitportalY + directrange - 1
speedx, speedy = speedy, -speedx
rotation = rotation - math.pi/2
if live then
--keep it from bugging out by having a minimum exit speed
local grav = yacceleration
if self and self.gravity then
grav = self.gravity
end
local minspeed = math.sqrt(2*grav*(height))
if speedy > -minspeed then
speedy = -minspeed
end
end
elseif (entryportalfacing == "right" and exitportalfacing == "up") then --right -> up
newx = exitportalX - relativerange*(2-width) - width/2 + 1
newy = exitportalY + directrange - 1
speedx, speedy = -speedy, speedx
rotation = rotation + math.pi/2
if live then
--keep it from bugging out by having a minimum exit speed
local grav = yacceleration
if self and self.gravity then
grav = self.gravity
end
local minspeed = math.sqrt(2*grav*(height))
if speedy > -minspeed then
speedy = -minspeed
end
end
elseif (entryportalfacing == "left" and exitportalfacing == "down") then --left -> down
newx = exitportalX - relativerange*(2-width) - width/2
newy = exitportalY - directrange
speedx, speedy = -speedy, speedx
rotation = rotation + math.pi/2
elseif (entryportalfacing == "right" and exitportalfacing == "down") then --right -> down
newx = exitportalX + relativerange*(2-width) + width/2 - 2
newy = exitportalY - directrange
speedx, speedy = speedy, -speedx
rotation = rotation - math.pi/2
end
newx = newx - width/2
newy = newy - height/2
return newx, newy, speedx, speedy, rotation, animationdirection
end