Repository navigation
Add early-exit and between-lists collision functions - #2905
Merged
Merged
Conversation
- 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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) -> boolhas_collision_with_lists(sprite, sprite_lists, method=AUTO) -> boolThese return
Trueat the first collision, instead of building the whole hit list the waylen(check_for_collision_with_list(...)) > 0does. They take the samemethodargument 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:
list_a,methodchooses how to searchlist_b, as incheck_for_collision_with_list. The docstring recommends putting the spatial-hashed or less-moving list second.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.All three are exported from
arcadeandarcade.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 ceilingwhileloopphysics_engines._wiggle_until_freePhysicsEnginePlatformer.is_on_ladder()andcan_jump()paths._check_for_collisionandAStarBarrierList.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:
AStarBarrierListover a 1600×400 levelcan_jump()The platformer frame barely changes, because a normal frame only finds 1–2 hits, so there's little to skip.
check_for_collision_between_listsvs 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:check_for_collision_between_listsTests
test_has_collision_with_list: everyCollisionMethod, with and without a spatial hash. It agrees withcheck_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_listsand 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_listand_shared_spritecover the same-list and shared-sprite cases._same_listtests, and not stopping early fails_stops_at_first_hit.🤖 Generated with Claude Code