Repository navigation
Make a new Sprite.hit_box match the sprite right away - #2928
Merged
Merged
Conversation
The hit_box setter kept the given hit box's position and scale, which for a new HitBox are (0, 0) and 1, so it hit nothing until the sprite moved (and kept the wrong scale until the sprite was scaled). The spatial hash wasn't updated for the new hit box's size either. Now the setter gives the hit box the sprite's position, scale and angle, for HitBox and RotatableHitBox alike, and updates the spatial hash. 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
Fixes the
Sprite.hit_boxsetter, which I noted earlier while working on collisions.The problem
The setter kept the given hit box's own position and scale. For a new
HitBox(points), those are (0, 0) and 1. For aHitBox, it copied only the sprite's angle; aRotatableHitBoxwas used exactly as given. For a sprite at (100, 100) with scale 2:sprite.hit_box = HitBox(points)So a sprite with a custom hit box collided with nothing until it moved. Even then, the hit box kept a scale of 1 until the sprite was scaled. A spatial hash also kept using the old hit box's size.
The fix
The setter now gives the hit box the sprite's position, scale and angle, for
HitBoxandRotatableHitBoxalike, and updates the spatial hash. The docstring now says that the points are relative to the sprite's center, and that the hit box's own position, scale and angle are replaced.The three places in Arcade that set
hit_boxalready passed the sprite's own position, angle and scale:Sprite.texture,Sprite.sync_hit_box_to_texture()and the tile map loader. So their behavior doesn't change.Tests
New in
tests/unit/sprite/test_sprite_hitbox.py:test_set_hit_box_matches_sprite: right after setting, the position, scale and angle match the sprite, and so do the adjusted points (rotated 90°, scaled by 2, moved).test_set_rotatable_hit_box_matches_sprite: aRotatableHitBox's own position, scale and angle are replaced.test_set_hit_box_follows_sprite_afterwards.test_set_hit_box_collides_right_away.test_set_hit_box_updates_spatial_hash.4 of them fail on
development. The "follows afterwards" one already passed and guards that behavior. I also updated a comment in a sweep test that described the old behavior as a workaround.Full suite: 1588 passed, no warnings, with no failures on my machine now that #2926 is in. Ruff is clean, and mypy has no new errors.
The changelog has an entry under Unreleased → Fixes.
🤖 Generated with Claude Code