Skip to content

Rework HitBox to support multiple named collision regions#2835

Open
Cleptomania wants to merge 1 commit intodevelopmentfrom
hitbox-rework
Open

Rework HitBox to support multiple named collision regions#2835
Cleptomania wants to merge 1 commit intodevelopmentfrom
hitbox-rework

Conversation

@Cleptomania
Copy link
Member

Summary

  • Replaces the single-polygon HitBox with a multi-region system where each HitBox can contain named Point2List regions (e.g. separate head/body/feet hitboxes), while maintaining backward compatibility with the single-region API
  • Merges RotatableHitBox into HitBox and adds JSON serialization support (with optional gzip compression)
  • Updates collision detection, pymunk physics engine, sprite lists, and tilemap to work with multi-region hitboxes
  • Adds sprite_multi_hitbox example and expanded test coverage
  • Fixes type annotations for pyright/mypy compatibility

Test plan

  • Run existing hitbox unit tests (tests/unit/hitbox/test_hitbox.py)
  • Run GUI tests to verify minor formatting fixes don't break anything
  • Verify pymunk physics engine works with multi-region hitboxes
  • Test the new sprite_multi_hitbox example
  • Confirm backward compatibility with single-region hitbox usage

🤖 Generated with Claude Code

Replaces the single-polygon HitBox with a multi-region system where each
HitBox can contain named Point2List regions. This enables sprites to have
distinct collision shapes (e.g. separate head/body/feet hitboxes) while
maintaining backward compatibility with the single-region API.

Key changes:
- HitBox accepts either a Point2List (single "default" region) or a
  dict[str, Point2List] mapping region names to point lists
- Merge RotatableHitBox into HitBox (removed RotatableHitBox)
- Add serialization support (save/load to JSON, with gzip option)
- Update collision detection, pymunk physics, sprite lists, and tilemap
  to work with multi-region hitboxes
- Add sprite_multi_hitbox example
- Fix type annotations for pyright/mypy compatibility

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Cleptomania
Copy link
Member Author

Cleptomania commented Mar 12, 2026

Right now this has a re-work that gets rid of the split between HitBox and RotatableHitBox. Originally this split was decided on because hitboxes are expensive to rotate, but in practice we've not really realized any gains from this, and everything is nearly universally always using RotatableHitBox anyways, so I think this split is not worth the headaches it has caused.

In addition to removing that split, it supports multiple polygons per hitbox, and they can be named. Yet to be done is to allow the collision detects to report what regions were collided, so you could do things like check if a head region or an arm region were hit. Obviously using multiple regions will incur a greater performance hit, as the check is a similar cost per region, but this can potentially save users from needing to manage things by composing a character or something out of multiple sprites.

The new hitboxes also support being loaded from JSON, I have plans to build an editor for creating these points, but that is not done yet.

Another thing yet to be done, is to update the Tilemap loading to support loading multiple regions from Tiled objects, this is something Tiled supports but Arcade has historically ignored. I will have to investigate how the region naming should work for loading these, but I believe the objects in Tiled can be given names, so that will probably be the way to go.

def angle(self) -> float:
"""
The angle to rotate the raw points by in degrees
def to_dict(self) -> dict:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to create a TypedDict 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think it probably does, I’m a big fan of TypedDict for things that are being saved/loaded. Especially if that format somehow gets expanded to have more metadata that an editor might make use of.

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.

2 participants