Skip to content

Commit 7f4ae49

Browse files
authored
Add method 0 for auto-select to check_for_collision_with_lists (#2762)
* Add method 0 for auto-select to check_for_collision_with_lists * Update changelog * Reference PR
1 parent d1d3824 commit 7f4ae49

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ Arcade [PyPi Release History](https://pypi.org/project/arcade/#history) page.
55

66
## Unreleased
77

8+
- Fixes a bug with the `check_for_collision_with_lists` function. This function is intended to mimic the functionality of
9+
`check_for_collision_with_list` but allow passing multiple lists and looping the same behavior. The `lists` function however
10+
handled the collision method differently. Which resulted in only spatial hash being used if it was available, or GPU collision.
11+
It would never fallback to the pure CPU brute force approach, which is the best option for spritelists which don't have spatial hash
12+
and less than 1,500 sprites. Certain games may see a substantial performance improvement from this change. See [2762](https://github.com/pythonarcade/arcade/pull/2762)
13+
814
- PyInstaller
915
- Fixed an issue where imports for backends for the `arcade.gl` package could not be discovered by PyInstaller.
1016
Since 3.3.0 users have needed to add these hidden imports via the pyinstaller CLI in order for Arcade to work.

arcade/sprite_list/collision.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def check_for_collision_with_list(
196196
def check_for_collision_with_lists(
197197
sprite: BasicSprite,
198198
sprite_lists: Iterable[SpriteSequence[SpriteType]],
199-
method=1,
199+
method=0,
200200
) -> list[SpriteType]:
201201
"""
202202
Check for a collision between a Sprite, and a list of SpriteLists.
@@ -207,8 +207,16 @@ def check_for_collision_with_lists(
207207
sprite_lists:
208208
SpriteLists to check against
209209
method:
210-
Collision check method. 1 is Spatial Hashing if available,
211-
2 is GPU based, 3 is slow CPU-bound check-everything. Defaults to 1.
210+
Collision check method. Defaults to 0.
211+
212+
- 0: auto-select. (spatial if available, GPU if 1500+ sprites, else simple)
213+
- 1: Spatial Hashing if available,
214+
- 2: GPU based
215+
- 3: Simple check-everything.
216+
217+
Note that while the GPU method is very fast when you cannot use spatial hashing,
218+
it's also very slow if you are calling this function many times per frame.
219+
What method is the most appropriate depends entirely on your use case.
212220
213221
Returns:
214222
List of sprites colliding, or an empty list.
@@ -224,9 +232,10 @@ def check_for_collision_with_lists(
224232
sprites_to_check: Iterable[SpriteType]
225233

226234
for sprite_list in sprite_lists:
227-
if sprite_list.spatial_hash is not None and method == 1:
235+
# Spatial
236+
if sprite_list.spatial_hash is not None and (method == 1 or method == 0):
228237
sprites_to_check = sprite_list.spatial_hash.get_sprites_near_sprite(sprite)
229-
elif method == 3:
238+
elif method == 3 or (method == 0 and len(sprite_list) <= 1500):
230239
sprites_to_check = sprite_list
231240
else:
232241
# GPU transform

0 commit comments

Comments
 (0)