-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgzhelper.py
1392 lines (1131 loc) · 36.1 KB
/
pgzhelper.py
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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# from __future__ import annotations
import math
import pygame
from pgzero.actor import Actor, POS_TOPLEFT, ANCHOR_CENTER, transform_anchor
from pgzero import game, loaders
import sys
import time
from typing import Sequence, Tuple, Union
from pygame import Vector2
_Coordinate = Union[Tuple[float, float], Sequence[float], Vector2]
_fullscreen = False
def set_fullscreen():
global _fullscreen
mod = sys.modules['__main__']
mod.screen.surface = pygame.display.set_mode((mod.WIDTH, mod.HEIGHT), pygame.FULLSCREEN)
_fullscreen = True
def set_windowed():
global _fullscreen
mod = sys.modules['__main__']
mod.screen.surface = pygame.display.set_mode((mod.WIDTH, mod.HEIGHT))
_fullscreen = False
def toggle_fullscreen():
if _fullscreen:
set_windowed()
else:
set_fullscreen()
def hide_mouse():
pygame.mouse.set_visible(False)
def show_mouse():
pygame.mouse.set_visible(True)
def distance_to(from_x, from_y, to_x, to_y):
dx = to_x - from_x
dy = to_y - from_y
return math.sqrt(dx**2 + dy**2)
def distance_to_squared(from_x, from_y, to_x, to_y):
dx = to_x - from_x
dy = to_y - from_y
return dx**2 + dy**2
def direction_to(from_x, from_y, to_x, to_y):
dx = to_x - from_x
dy = from_y - to_y
angle = math.degrees(math.atan2(dy, dx))
if angle > 0:
return angle
return 360 + angle
def get_move(direction, distance):
angle = math.radians(direction)
dx = distance * math.cos(angle)
dy = -distance * math.sin(angle)
return (dx, dy)
def move(x, y, direction, distance):
dx, dy = get_move(direction, distance)
return (x + dx, y + dy)
class Collide():
@staticmethod
def line_line(l1x1, l1y1, l1x2, l1y2, l2x1, l2y1, l2x2, l2y2):
l1x2_l1x1 = l1x2-l1x1
l1y2_l1y1 = l1y2-l1y1
determinant = (l2y2-l2y1)*l1x2_l1x1 - (l2x2-l2x1)*l1y2_l1y1
# Simplify: Parallel lines are never considered to be intersecting
if determinant == 0:
return False
uA = ((l2x2-l2x1)*(l1y1-l2y1) - (l2y2-l2y1)*(l1x1-l2x1)) / determinant
if uA < 0 or uA > 1:
return False
uB = (l1x2_l1x1*(l1y1-l2y1) - l1y2_l1y1*(l1x1-l2x1)) / determinant
if uB < 0 or uB > 1:
return False
return True
@staticmethod
def line_lines(l1x1, l1y1, l1x2, l1y2, l2):
l1x2_l1x1 = l1x2-l1x1
l1y2_l1y1 = l1y2-l1y1
i = 0
for l in l2:
determinant = (l[3]-l[1])*l1x2_l1x1 - (l[2]-l[0])*l1y2_l1y1
# Simplify: Parallel lines are never considered to be intersecting
if determinant == 0:
i += 1
continue
uA = ((l[2]-l[0])*(l1y1-l[1]) - (l[3]-l[1])*(l1x1-l[0])) / determinant
uB = (l1x2_l1x1*(l1y1-l[1]) - l1y2_l1y1*(l1x1-l[0])) / determinant
if 0 <= uA <= 1 and 0 <= uB <= 1:
return i
i += 1
return -1
@staticmethod
def line_line_XY(l1x1, l1y1, l1x2, l1y2, l2x1, l2y1, l2x2, l2y2):
determinant = (l2y2-l2y1)*(l1x2-l1x1) - (l2x2-l2x1)*(l1y2-l1y1)
# Simplify: Parallel lines are never considered to be intersecting
if determinant == 0:
return (None, None)
uA = ((l2x2-l2x1)*(l1y1-l2y1) - (l2y2-l2y1)*(l1x1-l2x1)) / determinant
uB = ((l1x2-l1x1)*(l1y1-l2y1) - (l1y2-l1y1)*(l1x1-l2x1)) / determinant
if 0 <= uA <= 1 and 0 <= uB <= 1:
ix = l1x1 + uA * (l1x2 - l1x1)
iy = l1y1 + uA * (l1y2 - l1y1)
return (ix, iy)
return (None, None)
@staticmethod
def line_line_dist(l1x1, l1y1, l1x2, l1y2, l2x1, l2y1, l2x2, l2y2):
ix, iy = Collide.line_line_XY(l1x1, l1y1, l1x2, l1y2, l2x1, l2y1, l2x2, l2y2)
if ix is not None:
return distance_to(l1x1, l1y1, ix, iy)
return None
@staticmethod
def line_line_dist_squared(l1x1, l1y1, l1x2, l1y2, l2x1, l2y1, l2x2, l2y2):
ix, iy = Collide.line_line_XY(l1x1, l1y1, l1x2, l1y2, l2x1, l2y1, l2x2, l2y2)
if ix is not None:
return distance_to_squared(l1x1, l1y1, ix, iy)
return None
@staticmethod
def line_circle(x1, y1, x2, y2, cx, cy, radius):
r_sq = radius ** 2
dist_sq = (x1 - cx) ** 2 + (y1 - cy) ** 2
if dist_sq <= r_sq:
return True
dist_sq = (x2 - cx) ** 2 + (y2 - cy) ** 2
if dist_sq <= r_sq:
return True
dx = x2 - x1
dy = y2 - y1
l_sq = dx ** 2 + dy ** 2
dot = (((cx - x1) * dx) + ((cy - y1) * dy)) / l_sq
ix = x1 + dot * dx
if (dx!=0) and (ix < x1) == (ix < x2):
return False
iy = y1 + dot * dy
if (dy!=0) and (iy < y1) == (iy < y2):
return False
dist_sq = (ix - cx) ** 2 + (iy - cy) ** 2
if dist_sq <= r_sq:
return True
return False
@staticmethod
def line_circle_XY(x1, y1, x2, y2, cx, cy, radius):
if Collide.circle_point(cx, cy, radius, x1, y1):
return (x1, y1)
x1 -= cx
y1 -= cy
x2 -= cx
y2 -= cy
if x2 < x1:
x_min, x_max = x2, x1
else:
x_min, x_max = x1, x2
if y2 < y1:
y_min, y_max = y2, y1
else:
y_min, y_max = y1, y2
# Coefficients of circle
c_r2 = radius ** 2
# Simplify if dx == 0: Vertical line
dx = x2 - x1
if dx == 0:
d = c_r2 - x1**2
if d < 0:
return (None, None)
elif d == 0:
i = 0
else:
i = math.sqrt(d)
iy = None
if y_min <= i <= y_max:
iy = i
if y_min <= -i <= y_max:
if iy is None or abs(i - y1) > abs(-i - y1):
iy = -i
if iy:
return (x1 + cx, iy + cy)
return (None, None)
# Gradient of line
l_m = (y2 - y1) / dx
# Simplify if l_m == 0: Horizontal line
if l_m == 0:
d = c_r2 - y1**2
if d < 0:
return (None, None)
elif d == 0:
i = 0
else:
i = math.sqrt(d)
ix = None
if x_min <= i <= x_max:
ix = i
if x_min <= -i <= x_max:
if ix is None or abs(i - x1) > abs(-i - x1):
ix = -i
if ix:
return (ix + cx, y1 + cy)
return (None, None)
# y intercept
l_c = y1 - l_m * x1
# Coefficients of quadratic
a = 1 + l_m**2
b = 2 * l_c * l_m
c = l_c**2 - c_r2
# Calculate discriminant and solve quadratic
discriminant = b**2 - 4 * a * c
if discriminant < 0:
return (None, None)
if discriminant == 0:
d_root = 0
else:
d_root = math.sqrt(discriminant)
ix = None
i1 = (-b + d_root) / (2 * a)
if x_min <= i1 <= x_max:
ix = i1
i2 = (-b - d_root) / (2 * a)
if x_min <= i2 <= x_max:
if ix is None or abs(i1 - x1) > abs(i2 - x1):
ix = i2
if ix:
return (ix + cx, l_m * ix + l_c + cy)
return (None, None)
@staticmethod
def line_circle_dist(x1, y1, x2, y2, cx, cy, radius):
ix, iy = Collide.line_circle_XY(x1, y1, x2, y2, cx, cy, radius)
if ix is not None:
return distance_to(x1, y1, ix, iy)
return None
@staticmethod
def line_circle_dist_squared(x1, y1, x2, y2, cx, cy, radius):
ix, iy = Collide.line_circle_XY(x1, y1, x2, y2, cx, cy, radius)
if ix is not None:
return distance_to_squared(x1, y1, ix, iy)
return None
@staticmethod
def line_rect(x1, y1, x2, y2, rx, ry, w, h):
if Collide.rect_points(rx, ry, w, h, [(x1, y1), (x2, y2)]) != -1:
return True
half_w = w / 2
half_h = h / 2
rect_lines = [
[rx - half_w, ry - half_h, rx - half_w, ry + half_h],
[rx - half_w, ry - half_h, rx + half_w, ry - half_h],
[rx + half_w, ry + half_h, rx - half_w, ry + half_h],
[rx + half_w, ry + half_h, rx + half_w, ry - half_h],
]
if Collide.line_lines(x1, y1, x2, y2, rect_lines) != -1:
return True
return False
@staticmethod
def line_rect_XY(x1, y1, x2, y2, rx, ry, w, h):
if Collide.rect_point(rx, ry, w, h, x1, y1):
return (x1, y1)
half_w = w / 2
half_h = h / 2
rect_lines = [
[rx - half_w, ry - half_h, rx - half_w, ry + half_h],
[rx - half_w, ry - half_h, rx + half_w, ry - half_h],
[rx + half_w, ry + half_h, rx - half_w, ry + half_h],
[rx + half_w, ry + half_h, rx + half_w, ry - half_h],
]
XYs = []
for l in rect_lines:
ix, iy = Collide.line_line_XY(x1, y1, x2, y2, l[0], l[1], l[2], l[3])
if ix is not None:
XYs.append((ix, iy))
length = len(XYs)
if length == 0:
return (None, None)
elif length == 1:
return XYs[0]
ix, iy = XYs[0]
shortest_dist = (ix - x1) ** 2 + (iy - y1) ** 2
for XY in XYs:
dist = (XY[0] - x1) ** 2 + (XY[1] - y1) ** 2
if dist < shortest_dist:
ix, iy = XY
shortest_dist = dist
return (ix, iy)
@staticmethod
def line_rect_dist(x1, y1, x2, y2, rx, ry, w, h):
ix, iy = Collide.line_rect_XY(x1, y1, x2, y2, rx, ry, w, h)
if ix is not None:
return distance_to(x1, y1, ix, iy)
return None
@staticmethod
def line_rect_dist_squared(x1, y1, x2, y2, rx, ry, w, h):
ix, iy = Collide.line_rect_XY(x1, y1, x2, y2, rx, ry, w, h)
if ix is not None:
return distance_to_squared(x1, y1, ix, iy)
return None
@staticmethod
def line_obb_XY(x1, y1, x2, y2, ox, oy, w, h, angle):
half_width = w / 2
half_height = h / 2
r_angle = math.radians(angle)
costheta = math.cos(r_angle)
sintheta = math.sin(r_angle)
tx = x1 - ox
ty = y1 - oy
rx = tx * costheta - ty * sintheta
ry = ty * costheta + tx * sintheta
if rx > -half_width and rx < half_width and ry > -half_height and ry < half_height:
return (x1, y1)
wc = half_width * costheta
hs = half_height * sintheta
hc = half_height * costheta
ws = half_width * sintheta
p = [
[ox + wc + hs, oy + hc - ws],
[ox - wc + hs, oy + hc + ws],
[ox + wc - hs, oy - hc - ws],
[ox - wc - hs, oy - hc + ws],
]
obb_lines = [
[p[0][0], p[0][1], p[1][0], p[1][1]],
[p[1][0], p[1][1], p[3][0], p[3][1]],
[p[3][0], p[3][1], p[2][0], p[2][1]],
[p[2][0], p[2][1], p[0][0], p[0][1]]
]
XYs = []
for l in obb_lines:
ix, iy = Collide.line_line_XY(x1, y1, x2, y2, l[0], l[1], l[2], l[3])
if ix is not None:
XYs.append((ix, iy))
length = len(XYs)
if length == 0:
return (None, None)
elif length == 1:
return XYs[0]
ix, iy = XYs[0]
shortest_dist = (ix - x1) ** 2 + (iy - y1) ** 2
for XY in XYs:
dist = (XY[0] - x1) ** 2 + (XY[1] - y1) ** 2
if dist < shortest_dist:
ix, iy = XY
shortest_dist = dist
return (ix, iy)
@staticmethod
def line_obb_dist(x1, y1, x2, y2, ox, oy, w, h, angle):
ix, iy = Collide.line_obb_XY(x1, y1, x2, y2, ox, oy, w, h, angle)
if ix is not None:
return distance_to(x1, y1, ix, iy)
return None
@staticmethod
def line_obb_dist_squared(x1, y1, x2, y2, ox, oy, w, h, angle):
ix, iy = Collide.obb_line_XY(x1, y1, x2, y2, ox, oy, w, h, angle)
if ix is not None:
return distance_to_squared(x1, y1, ix, iy)
return None
@staticmethod
def circle_point(x1, y1, radius, x2, y2):
rSquare = radius ** 2
dSquare = (x2 - x1)**2 + (y2 - y1)**2
if dSquare < rSquare:
return True
return False
@staticmethod
def circle_points(x, y, radius, points):
rSquare = radius ** 2
i = 0
for point in points:
try:
px = point[0]
py = point[1]
except (KeyError, TypeError):
px = point.x
py = point.y
dSquare = (px - x)**2 + (py - y)**2
if dSquare < rSquare:
return i
i += 1
return -1
@staticmethod
def circle_line(cx, cy, radius, x1, y1, x2, y2):
return Collide.line_circle(x1, y1, x2, y2, cx, cy, radius)
@staticmethod
def circle_circle(x1, y1, r1, x2, y2, r2):
rSquare = (r1 + r2) ** 2
dSquare = (x2 - x1)**2 + (y2 - y1)**2
if dSquare < rSquare:
return True
return False
@staticmethod
def circle_rect(cx, cy, cr, rx, ry, rw, rh):
h_w = rw / 2
h_h = rh / 2
rect_l = rx - h_w
rect_t = ry - h_h
if cx < rect_l:
dx2 = (cx - rect_l) ** 2
elif cx > (rect_l + rw):
dx2 = (cx - rect_l - rw) ** 2
else:
dx2 = 0
if cy < rect_t:
dy2 = (cy - rect_t) ** 2
elif cy > (rect_t + rh):
dy2 = (cy - rect_t - rh) ** 2
else:
dy2 = 0
dist2 = dx2 + dy2
if dist2 < (cr ** 2):
return True
return False
@staticmethod
def rect_point(x, y, w, h, px, py):
half_w = w / 2
half_h = h / 2
if (
px < x - half_w
or px > x + half_w
or py < y - half_h
or py > y + half_h
):
return False
return True
@staticmethod
def rect_points(x, y, w, h, points):
half_w = w / 2
half_h = h / 2
min_x = x - half_w
max_x = x + half_w
min_y = y - half_h
max_y = y + half_h
i = 0
for point in points:
try:
px = point[0]
py = point[1]
except (KeyError, TypeError):
px = point.x
py = point.y
if (
px >= min_x
and px <= max_x
and py >= min_y
and py <= max_y
):
return i
i += 1
return -1
@staticmethod
def rect_line(x, y, w, h, lx1, ly1, lx2, ly2):
return Collide.line_rect(lx1, ly1, lx2, ly2, x, y, w, h)
@staticmethod
def rect_circle(rx, ry, rw, rh, cx, cy, cr):
return Collide.circle_rect(cx, cy, cr, rx, ry, rw, rh)
@staticmethod
def rect_rect(x1, y1, w1, h1, x2, y2, w2, h2):
h_w1 = w1 / 2
h_h1 = h1 / 2
h_w2 = w2 / 2
h_h2 = h2 / 2
if (
x2 - h_w2 > x1 + h_w1
or x2 + h_w2 < x1 - h_w1
or y2 - h_h2 > y1 + h_h1
or y2 + h_h2 < y1 - h_h1
):
return False
return True
@staticmethod
def obb_point(x, y, w, h, angle, px, py):
half_width = w / 2
half_height = h / 2
b_radius_sq = half_width ** 2 + half_height ** 2
tx = px - x
ty = py - y
if tx ** 2 + ty ** 2 > b_radius_sq:
return False
r_angle = math.radians(angle)
costheta = math.cos(r_angle)
sintheta = math.sin(r_angle)
rx = tx * costheta - ty * sintheta
ry = ty * costheta + tx * sintheta
if rx > -half_width and rx < half_width and ry > -half_height and ry < half_height:
return True
return False
@staticmethod
def obb_points(x, y, w, h, angle, points):
half_width = w / 2
half_height = h / 2
r_angle = math.radians(angle)
costheta = math.cos(r_angle)
sintheta = math.sin(r_angle)
i = 0
for point in points:
try:
px = point[0]
py = point[1]
except (KeyError, TypeError):
px = point.x
py = point.y
tx = px - x
ty = py - y
rx = tx * costheta - ty * sintheta
ry = ty * costheta + tx * sintheta
if rx > -half_width and rx < half_width and ry > -half_height and ry < half_height:
return i
i += 1
return -1
@staticmethod
def obb_line(x, y, w, h, angle, lx1, ly1, lx2, ly2):
half_width = w / 2
half_height = h / 2
r_angle = math.radians(angle)
costheta = math.cos(r_angle)
sintheta = math.sin(r_angle)
tx = lx1 - x
ty = ly1 - y
rx = tx * costheta - ty * sintheta
ry = ty * costheta + tx * sintheta
if rx > -half_width and rx < half_width and ry > -half_height and ry < half_height:
return True
tx = lx2 - x
ty = ly2 - y
rx = tx * costheta - ty * sintheta
ry = ty * costheta + tx * sintheta
if rx > -half_width and rx < half_width and ry > -half_height and ry < half_height:
return True
wc = half_width * costheta
hs = half_height * sintheta
hc = half_height * costheta
ws = half_width * sintheta
p = [
[x + wc + hs, y + hc - ws],
[x - wc + hs, y + hc + ws],
[x + wc - hs, y - hc - ws],
[x - wc - hs, y - hc + ws],
]
obb_lines = [
[p[0][0], p[0][1], p[1][0], p[1][1]],
[p[1][0], p[1][1], p[3][0], p[3][1]],
[p[3][0], p[3][1], p[2][0], p[2][1]],
[p[2][0], p[2][1], p[0][0], p[0][1]]
]
if Collide.line_lines(lx1, ly1, lx2, ly2, obb_lines) != -1:
return True
return False
@staticmethod
def obb_lines(x, y, w, h, angle, lines):
half_width = w / 2
half_height = h / 2
r_angle = math.radians(angle)
costheta = math.cos(r_angle)
sintheta = math.sin(r_angle)
wc = half_width * costheta
hs = half_height * sintheta
hc = half_height * costheta
ws = half_width * sintheta
p = [
[x + wc + hs, y + hc - ws],
[x - wc + hs, y + hc + ws],
[x + wc - hs, y - hc - ws],
[x - wc - hs, y - hc + ws],
]
obb_lines = [
[p[0][0], p[0][1], p[1][0], p[1][1]],
[p[1][0], p[1][1], p[3][0], p[3][1]],
[p[3][0], p[3][1], p[2][0], p[2][1]],
[p[2][0], p[2][1], p[0][0], p[0][1]]
]
i = 0
for l in lines:
tx = l[0] - x
ty = l[1] - y
rx = tx * costheta - ty * sintheta
ry = ty * costheta + tx * sintheta
if rx > -half_width and rx < half_width and ry > -half_height and ry < half_height:
return i
tx = l[2] - x
ty = l[3] - y
rx = tx * costheta - ty * sintheta
ry = ty * costheta + tx * sintheta
if rx > -half_width and rx < half_width and ry > -half_height and ry < half_height:
return i
if Collide.line_lines(l[0], l[1], l[2], l[3], obb_lines) != -1:
return i
i += 1
return -1
@staticmethod
def obb_circle(x, y, w, h, angle, cx, cy, radius):
half_width = w / 2
half_height = h / 2
tx = cx - x
ty = cy - y
if tx ** 2 + ty ** 2 > (half_height + half_width + radius) ** 2:
return False
r_angle = math.radians(angle)
costheta = math.cos(r_angle)
sintheta = math.sin(r_angle)
rx = tx * costheta - ty * sintheta
ry = ty * costheta + tx * sintheta
if (rx < -half_width - radius
or rx > half_width + radius
or ry < -half_height - radius
or ry > half_height + radius
):
return False
if (rx <= half_width and rx >= -half_width) or (ry <= half_height and ry >= -half_height):
return True
dx = abs(rx) - half_width
dy = abs(ry) - half_height
dist_squared = dx ** 2 + dy ** 2
if dist_squared > radius ** 2:
return False
return True
@staticmethod
def obb_circles(x, y, w, h, angle, circles):
half_width = w / 2
half_height = h / 2
r_angle = math.radians(angle)
costheta = math.cos(r_angle)
sintheta = math.sin(r_angle)
i = 0
for circle in circles:
tx = circle[0] - x
ty = circle[1] - y
rx = tx * costheta - ty * sintheta
ry = ty * costheta + tx * sintheta
if (rx < -half_width - circle[2]
or rx > half_width + circle[2]
or ry < -half_height - circle[2]
or ry > half_height + circle[2]
):
i += 1
continue
if (rx <= half_width and rx >= -half_width) or (ry <= half_height and ry >= -half_height):
return i
dx = abs(rx) - half_width
dy = abs(ry) - half_height
dist_squared = dx ** 2 + dy ** 2
if dist_squared > circle[2] ** 2:
i += 1
continue
return i
return -1
@staticmethod
def obb_rect(x, y, w, h, angle, rx, ry, rw, rh):
half_width = w / 2
half_height = h / 2
tx = rx - x
ty = ry - y
if tx ** 2 + ty ** 2 > (half_height + half_width + rw + rh) ** 2:
return False
r_angle = math.radians(angle)
costheta = math.cos(r_angle)
sintheta = math.sin(r_angle)
tx2 = tx * costheta - ty * sintheta
ty2 = ty * costheta + tx * sintheta
if tx2 > -half_width and tx2 < half_width and ty2 > -half_height and ty2 < half_height:
return True
wc = half_width * costheta
hs = half_height * sintheta
hc = half_height * costheta
ws = half_width * sintheta
p = [
[wc + hs, hc - ws],
[-wc + hs, hc + ws],
[wc - hs, -hc - ws],
[-wc - hs, -hc + ws],
]
obb_lines = [
[p[0][0], p[0][1], p[1][0], p[1][1]],
[p[1][0], p[1][1], p[3][0], p[3][1]],
[p[3][0], p[3][1], p[2][0], p[2][1]],
[p[2][0], p[2][1], p[0][0], p[0][1]]
]
h_rw = rw / 2
h_rh = rh / 2
rect_lines = [
[tx - h_rw, ty - h_rh, tx - h_rw, ty + h_rh],
[tx + h_rw, ty - h_rh, tx + h_rw, ty + h_rh],
[tx - h_rw, ty - h_rh, tx + h_rw, ty - h_rh],
[tx - h_rw, ty + h_rh, tx + h_rw, ty + h_rh]
]
for obb_p in p:
if obb_p[0] > tx - h_rw and obb_p[0] < tx + h_rw and obb_p[1] > ty - h_rh and obb_p[1] < ty + h_rh:
return True
for obb_line in obb_lines:
l1x1 = obb_line[0]
l1y1 = obb_line[1]
l1x2 = obb_line[2]
l1y2 = obb_line[3]
l1x2_l1x1 = l1x2-l1x1
l1y2_l1y1 = l1y2-l1y1
for rect_line in rect_lines:
l2x1 = rect_line[0]
l2y1 = rect_line[1]
l2x2 = rect_line[2]
l2y2 = rect_line[3]
determinant = (l2y2-l2y1)*l1x2_l1x1 - (l2x2-l2x1)*l1y2_l1y1
# Simplify: Parallel lines are never considered to be intersecting
if determinant == 0:
continue
uA = ((l2x2-l2x1)*(l1y1-l2y1) - (l2y2-l2y1)*(l1x1-l2x1)) / determinant
if uA < 0 or uA > 1:
continue
uB = (l1x2_l1x1*(l1y1-l2y1) - l1y2_l1y1*(l1x1-l2x1)) / determinant
if uB < 0 or uB > 1:
continue
return True
return False
@staticmethod
def obb_rects(x, y, w, h, angle, rects):
half_width = w / 2
half_height = h / 2
r_angle = math.radians(angle)
costheta = math.cos(r_angle)
sintheta = math.sin(r_angle)
i = 0
for rect in rects:
rx = rect[0]
ry = rect[1]
rw = rect[2]
rh = rect[3]
tx = rx - x
ty = ry - y
if tx ** 2 + ty ** 2 > (half_height + half_width + rw + rh) ** 2:
i += 1
continue
tx2 = tx * costheta - ty * sintheta
ty2 = ty * costheta + tx * sintheta
if tx2 > -half_width and tx2 < half_width and ty2 > -half_height and ty2 < half_height:
return i
wc = half_width * costheta
hs = half_height * sintheta
hc = half_height * costheta
ws = half_width * sintheta
p = [
[wc + hs, hc - ws],
[-wc + hs, hc + ws],
[wc - hs, -hc - ws],
[-wc - hs, -hc + ws],
]
obb_lines = [
[p[0][0], p[0][1], p[1][0], p[1][1]],
[p[1][0], p[1][1], p[3][0], p[3][1]],
[p[3][0], p[3][1], p[2][0], p[2][1]],
[p[2][0], p[2][1], p[0][0], p[0][1]]
]
h_rw = rw / 2
h_rh = rh / 2
rect_lines = [
[tx - h_rw, ty - h_rh, tx - h_rw, ty + h_rh],
[tx + h_rw, ty - h_rh, tx + h_rw, ty + h_rh],
[tx - h_rw, ty - h_rh, tx + h_rw, ty - h_rh],
[tx - h_rw, ty + h_rh, tx + h_rw, ty + h_rh]
]
for obb_p in p:
if obb_p[0] > tx - h_rw and obb_p[0] < tx + h_rw and obb_p[1] > ty - h_rh and obb_p[1] < ty + h_rh:
return i
for obb_line in obb_lines:
l1x1 = obb_line[0]
l1y1 = obb_line[1]
l1x2 = obb_line[2]
l1y2 = obb_line[3]
l1x2_l1x1 = l1x2-l1x1
l1y2_l1y1 = l1y2-l1y1
for rect_line in rect_lines:
l2x1 = rect_line[0]
l2y1 = rect_line[1]
l2x2 = rect_line[2]
l2y2 = rect_line[3]
determinant = (l2y2-l2y1)*l1x2_l1x1 - (l2x2-l2x1)*l1y2_l1y1
# Simplify: Parallel lines are never considered to be intersecting
if determinant == 0:
continue
uA = ((l2x2-l2x1)*(l1y1-l2y1) - (l2y2-l2y1)*(l1x1-l2x1)) / determinant
if uA < 0 or uA > 1:
continue
uB = (l1x2_l1x1*(l1y1-l2y1) - l1y2_l1y1*(l1x1-l2x1)) / determinant
if uB < 0 or uB > 1:
continue
return i
i += 1
return -1
def obb_obb(x, y, w, h, angle, x2, y2, w2, h2, angle2):
r_angle = math.radians(angle)
costheta = math.cos(r_angle)
sintheta = math.sin(r_angle)
tx2 = x2 - x
ty2 = y2 - y
rx2 = tx2 * costheta - ty2 * sintheta
ry2 = ty2 * costheta + tx2 * sintheta
return Collide.obb_rect(rx2, ry2, w2, h2, angle2-angle, 0, 0, w, h)
def obb_obbs(x, y, w, h, angle, obbs):
r_angle = math.radians(angle)
costheta = math.cos(r_angle)
sintheta = math.sin(r_angle)
for obb in obbs:
x2, y2, w2, h2, angle2 = obb
tx2 = x2 - x
ty2 = y2 - y
rx2 = tx2 * costheta - ty2 * sintheta
ry2 = ty2 * costheta + tx2 * sintheta
return Collide.obb_rect(rx2, ry2, w2, h2, angle2-angle, 0, 0, w, h)
class Actor(Actor):
def __init__(self, image:Union[str, pygame.Surface], pos=POS_TOPLEFT, anchor=ANCHOR_CENTER, **kwargs):
self._flip_x = False
self._flip_y = False
self._scale = 1
self._mask = None
self._images = None
self._image_idx = 0
self._subrects = None
self._transform_cnt = 0
self._orig_surfs = {}
self._surfs = {}
self._animate_counter = 0
self._animate_run = False
self._radius = None