Skip to content

Add early-exit and between-lists collision functions - #2905

Merged
pvcraven merged 1 commit into
developmentfrom
feature/collision-early-exit
Oct 1, 2026
Merged

pvcraven merged 1 commit into
developmentfrom
feature/collision-early-exit

Conversation

@pvcraven

@pvcraven pvcraven commented Oct 1, 2026

Copy link
Copy Markdown
Member

Summary

Adds three collision functions, and uses two of them inside arcade where only a yes/no answer was needed.

has_collision_with_list(sprite, sprite_list, method=AUTO) -> bool

has_collision_with_lists(sprite, sprite_lists, method=AUTO) -> bool

These return True at the first collision, instead of building the whole hit list the way len(check_for_collision_with_list(...)) > 0 does. They take the same method argument and pick the same sprites to check as the existing functions. A sprite never collides with itself.

check_for_collision_between_lists(list_a, list_b, method=AUTO) -> list[tuple[a, b]]

Returns every colliding pair between two lists:

for bullet, enemy in arcade.check_for_collision_between_lists(bullets, enemies):
    bullet.remove_from_sprite_lists()
    enemy.remove_from_sprite_lists()
  • A sprite can be in more than one pair, such as a bullet hitting two enemies.
  • For each sprite in list_a, method chooses how to search list_b, as in check_for_collision_with_list. The docstring recommends putting the spatial-hashed or less-moving list second.
  • Same list twice (check_for_collision_between_lists(sprites, sprites)): each colliding pair is returned once, never as both (a, b) and (b, a), and sprites are never paired with themselves.
  • It's typed generically over both lists' sprite types.

All three are exported from arcade and arcade.sprite_list, and the API doc generator picks them up.

Used inside arcade

Every place that only needed to know whether there was a collision:

  • physics_engines._move_sprite: the initial overlap check, and the ceiling while loop
  • physics_engines._wiggle_until_free
  • PhysicsEnginePlatformer.is_on_ladder() and can_jump()
  • paths._check_for_collision and AStarBarrierList.recalculate() (one check per grid cell)

I left the places that use the hit list itself alone: the rotation hit list and the ramp loop both feed the returned list.

Performance

Old and new code alternated, 3 rounds each:

Case Before After
Yes/no check against 400 overlapping coins 478–523 µs 26–27 µs (~18×)
Building an AStarBarrierList over a 1600×400 level 3.88–3.92 ms 3.54–3.62 ms (~7–9%)
Platformer engine frame + can_jump() 58.9–60.9 µs 58.4–59.0 µs (within noise)

The platformer frame barely changes, because a normal frame only finds 1–2 hits, so there's little to skip.

check_for_collision_between_lists vs the hand-written loop [(b, e) for b in bullets for e in check_for_collision_with_list(b, enemies)], 100 bullets vs 100 meteors, best of 15 interleaved rounds. Both give identical results:

Hand-written loop check_for_collision_between_lists
no spatial hash 1687 µs 1668 µs
spatial hash on the meteors 513 µs 462 µs

Tests

  • test_has_collision_with_list: every CollisionMethod, with and without a spatial hash. It agrees with check_for_collision_with_list, ignores the sprite itself, treats touching edges as not colliding, and handles empty lists.
  • test_has_collision_with_list_stops_at_first_hit: counts the narrow-phase checks to confirm both functions stop after the first hit.
  • test_has_collision_with_lists and type-error tests.
  • test_check_for_collision_between_lists (with and without a spatial hash) covers one bullet hitting two enemies, swapped lists, and empty lists. _same_list and _shared_sprite cover the same-list and shared-sprite cases.
  • Deliberate bugs are caught: reporting same-list pairs twice fails the _same_list tests, and not stopping early fails _stops_at_first_hit.
  • Full test suite (pyglet 3.0.dev11): 1382 passed, including the physics engine and A* tests. The 3 failures are the render tests that only fail on my machine.
  • Ruff and mypy report nothing new for the changed files.

🤖 Generated with Claude Code

- has_collision_with_list() and has_collision_with_lists() return True
  at the first collision instead of building the whole hit list. About
  18x faster than checking for an empty result when many sprites
  overlap.
- check_for_collision_between_lists(list_a, list_b) returns every
  colliding (sprite_a, sprite_b) pair, such as bullets and enemies.
  Given the same list twice, each pair is returned once.

Use the early-exit functions in the physics engines (initial overlap
check, wiggle loop, ceiling loop, is_on_ladder, can_jump) and the A*
pathfinding (_check_for_collision, AStarBarrierList.recalculate),
everywhere only a yes/no answer was needed. Building an
AStarBarrierList is about 7-9% faster.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pvcraven
pvcraven merged commit b6bf564 into development Oct 1, 2026
7 checks passed
@pvcraven
pvcraven deleted the feature/collision-early-exit branch October 1, 2026 19:42
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