Skip to content

Commit 26cdee3

Browse files
gran4Grant Hur
andauthored
Clean _check_for_collision (#1702)
* . * Over looked some things * Add comment and revert some stuff We should explain why it is unpythonic. Reverted that part --------- Co-authored-by: Grant Hur <22hurg@sjchrisitan.org>
1 parent 5b6680a commit 26cdee3

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

arcade/sprite_list/collision.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,32 +93,36 @@ def _check_for_collision(sprite1: SpriteType, sprite2: SpriteType) -> bool:
9393
:returns: True if sprites overlap.
9494
:rtype: bool
9595
"""
96+
97+
#NOTE: for speed becuase attribute look ups are slow.
9698
sprite1_position = sprite1._position
9799
sprite1_width = sprite1._width
98100
sprite1_height = sprite1._height
99101
sprite2_position = sprite2._position
100102
sprite2_width = sprite2._width
101103
sprite2_height = sprite2._height
104+
102105
radius_sum = (
103106
(sprite1_width if sprite1_width > sprite1_height else sprite1_height)
104107
+ (sprite2_width if sprite2_width > sprite2_height else sprite2_height)
105108
)
109+
106110
# Multiply by half of the theoretical max diagonal length for an estimation of distance
107111
radius_sum *= 0.71 # 1.42 / 2
108-
radius_sum_x2 = radius_sum * radius_sum
112+
radius_sum_sq = radius_sum * radius_sum
109113

110114
diff_x = sprite1_position[0] - sprite2_position[0]
111-
diff_x2 = diff_x * diff_x
112-
if diff_x2 > radius_sum_x2:
115+
diff_x_sq = diff_x * diff_x
116+
if diff_x_sq > radius_sum_sq:
113117
return False
114118

115119
diff_y = sprite1_position[1] - sprite2_position[1]
116-
diff_y2 = diff_y * diff_y
117-
if diff_y2 > radius_sum_x2:
120+
diff_y_sq = diff_y * diff_y
121+
if diff_y_sq > radius_sum_sq:
118122
return False
119123

120-
distance = diff_x2 + diff_y2
121-
if distance > radius_sum_x2:
124+
distance = diff_x_sq + diff_y_sq
125+
if distance > radius_sum_sq:
122126
return False
123127

124128
return are_polygons_intersecting(

0 commit comments

Comments
 (0)