Skip to content

Add CollisionMethod enum for collision list methods - #2901

Merged
pvcraven merged 1 commit into
developmentfrom
feature/collision-method-enum
Oct 1, 2026
Merged

pvcraven merged 1 commit into
developmentfrom
feature/collision-method-enum

Conversation

@pvcraven

@pvcraven pvcraven commented Oct 1, 2026

Copy link
Copy Markdown
Member

Summary

check_for_collision_with_list and check_for_collision_with_lists took a bare method number from 0 to 3. This adds an enum for it:

arcade.check_for_collision_with_list(player, walls, method=arcade.CollisionMethod.SPATIAL)
Member Value Behavior (unchanged)
AUTO 0 Spatial hash if the list has one; otherwise check every sprite if there are ≤ 1500, or use the GPU if more
SPATIAL 1 Spatial hash; if the list has none, the GPU
GPU 2 GPU to find nearby sprites, then check those
SIMPLE 3 Check every sprite

On WebGL, anything that would use the GPU checks every sprite instead.

CollisionMethod is an IntEnum, so code passing 0–3 keeps working, and the enum compares equal to those numbers.

Changes

  • arcade.CollisionMethod in arcade/sprite_list/collision.py, exported from arcade and arcade.sprite_list. The choices are now documented on the enum's members, and both functions' docstrings point to it. The API doc generator picks it up automatically (checked with util/update_quick_index.py).
  • Both functions' method parameter is now typed CollisionMethod | int, with default CollisionMethod.AUTO. check_for_collision_with_lists previously had no type hint for it.
  • The two functions had duplicated the logic that picks which sprites to check. It's now one helper, _get_sprites_to_check(), which compares against the enum members.
  • No behavior change. That includes SPATIAL falling back to the GPU when the list has no spatial hash, which is arguably surprising. Changing it belongs in a separate PR.

Tests

  • test_collision_method_values: the members equal 0–3, and CollisionMethod(2) is CollisionMethod.GPU.
  • test_collision_method_paths: 12 cases covering every method, with and without a spatial hash, small and large (> 1500) lists, and the WebGL fallback. Each case runs with the enum member and with the plain number, through both functions, and checks which path is taken (spatial hash, every sprite, or GPU). This also covers the existing TODO: Check that the right collision function is called internally.
  • To show behavior is unchanged, I ran the same 12 cases (written with plain numbers) against the previous code. All passed.
  • Full unit suite: 1128 passed. The 3 failures are the render tests that only fail on my machine. Ruff is clean, and mypy reports no errors in the changed files.

Performance

The machine was noisy, so I loaded the old version, this version, and a version with the selection logic inlined into one process, and interleaved their timings (best of 400 rounds):

Case Old This PR
check_for_collision_with_list, 5 sprites 1.130 µs 1.165 µs
same, with spatial hash 1.196 µs 1.210 µs
check_for_collision_with_lists, 2 lists 2.123 µs 2.174 µs

The helper call costs about 15–50 ns per call (1–3%). Inlining it removed that, but I kept the helper to avoid duplicating the selection logic. Happy to inline it if you'd prefer.

🤖 Generated with Claude Code

check_for_collision_with_list and check_for_collision_with_lists took a
bare method number from 0 to 3. Add arcade.CollisionMethod (AUTO,
SPATIAL, GPU, SIMPLE) and document the choices on it. It's an IntEnum,
so existing code passing numbers keeps working.

The two functions duplicated the logic that picks which sprites to
check; move it into one helper, _get_sprites_to_check(). Behavior is
unchanged, including SPATIAL falling back to the GPU when the list has
no spatial hash.

Add tests covering which path each method takes (spatial hash, every
sprite, or GPU, including the WebGL fallback) for both enum members and
plain numbers, through both functions. The same cases pass on the
previous code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pvcraven
pvcraven merged commit 0b443f4 into development Oct 1, 2026
7 checks passed
@pvcraven
pvcraven deleted the feature/collision-method-enum branch October 1, 2026 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant