Fix collision method and duplicate oddities, document touching - #2902
Merged
Merged
Conversation
- CollisionMethod.SPATIAL on a list without a spatial hash now chooses the same way as AUTO (check every sprite for 1500 or fewer, else the GPU). Before, it always used the GPU, which is slow when called many times per frame and needs an open window. - check_for_collision_with_lists returns each colliding sprite once, even if it's in more than one of the lists, in the order first found. Only checked when more than one list is passed. - Document that hit boxes that only touch don't collide, while a point exactly on an edge counts for get_sprites_at_point and collides_with_point. Correct get_sprites_in_rect's docs, which said touching sprites were returned, and its outdated tuple description. Add tests for the new SPATIAL paths, duplicates, and touching behavior. 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
Three small collision inconsistencies. The first two are behavior changes; the third is documentation only.
1.
CollisionMethod.SPATIALwithout a spatial hashBefore:
SPATIALon a list without a spatial hash always used the GPU, even for a one-sprite list. Someone choosingSPATIALis likely calling this many times per frame, which is when the GPU path is slowest. It also needs an open window: without one it raisedNoArcadeWindowError, whereAUTOon the same list worked.Now: it chooses the same way as
AUTO. Lists of 1500 or fewer check every sprite, and larger lists use the GPU. With a spatial hash, nothing changes, and no other method changes.2. Duplicates from
check_for_collision_with_listsBefore: a sprite in more than one of the lists was returned once per list.
Now: each colliding sprite is returned once, in the order first found. This only runs when more than one list is passed and there are 2 or more hits.
The built-in physics engines only check whether the result is empty, or loop over it, so they don't rely on duplicates.
3. Touching edges (docs only)
Hit boxes that only touch, sharing an edge or a corner, don't collide. A point exactly on an edge, though, counts for
get_sprites_at_pointandcollides_with_point. This has to stay as it is: the physics engines push a sprite until it no longer collides, so a sprite resting on the ground must not count as colliding with it.So this PR documents the difference on
check_for_collision,collides_with_sprite,get_sprites_at_point,collides_with_point, andget_sprites_in_rect. It also correctsget_sprites_in_rect's docs, which said sprites touching the rectangle were returned (they aren't) and described the rectangle as a tuple (it's aRect).Tests
test_collision_method_pathstable now hasSPATIALwithout a hash checking every sprite on small lists, using the GPU on large ones, and checking every sprite on WebGL. Each case runs with the enum and with the plain number, through both functions.test_check_for_collision_with_lists_no_duplicatescovers first-found order, the same list passed twice, a generator of lists, and a spatial-hashed list (compared as a set, since a spatial hash returns sprites in no particular order).test_touching_edgespins down the documented behavior: edge and corner touches don't collide (sprite to sprite, list, and rect), while points on an edge or corner count.development, only the changedSPATIALrows and the duplicates test fail.test_touching_edgespasses there too, since that behavior is unchanged.Performance
check_for_collision_with_lists(player, [walls, platforms]), with the old and new versions interleaved in one process (best of 300 rounds):That's about 0.05 µs without hits and 0.26 µs (3%) when duplicates are removed.
🤖 Generated with Claude Code