Skip to content

Make a new Sprite.hit_box match the sprite right away - #2928

Merged
pvcraven merged 1 commit into
developmentfrom
fix/hit-box-setter
Oct 6, 2026
Merged

pvcraven merged 1 commit into
developmentfrom
fix/hit-box-setter

Conversation

@pvcraven

@pvcraven pvcraven commented Oct 6, 2026

Copy link
Copy Markdown
Member

Summary

Fixes the Sprite.hit_box setter, 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 a HitBox, it copied only the sprite's angle; a RotatableHitBox was used exactly as given. For a sprite at (100, 100) with scale 2:

After sprite.hit_box = HitBox(points) Before After
Hit box position (0, 0) (100, 100)
Hit box scale 1 2
Collides with a sprite in the same spot no yes
Spatial hash finds a bigger hit box no yes

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 HitBox and RotatableHitBox alike, 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_box already 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: a RotatableHitBox'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

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>
@pvcraven
pvcraven merged commit 4b7fcd4 into development Oct 6, 2026
7 checks passed
@pvcraven
pvcraven deleted the fix/hit-box-setter branch October 6, 2026 19:37
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