Issue #127 wontfix

Custom size hitboxes

Tom C
created an issue

Currently sprites in a group are drawn from the top-left corner of the Rect, which works fine, until the Rect needs to be bigger or smaller than the image.

I have a proposal that could provide an effective fix to this:

Have a HitBox class that stores the size of the hitbox (possibly as a Rect) and references the sprites position. The group's draw function can then be called using the HitBox instance's referenced position instead of a standard Rect. When collision methods are called, they use the HitBox's own Rect instance.

With this improvement the programmer could still use a Rect if they don't need the more advanced HitBox, and compatibility with old code would be retained.

If people think this would be a good idea I'd be happy to upload some modified source to provide the functionality.

More info: The HitBox class could also incorporate custom anchor points, instead of just the top-left corner, but I think someone might be doing that for the GSoC as well. It could be useful to contact him.

Comments (4)

  1. Anonymous

    You can actually assign rect positions more effectively by assigning coordinates to their .center attribute, at which point non-centered anchors are just a matter of punching in some arithmetic alongside the coordinates during assignment (for example, hitbox_rect.center = (player.x + 10, player.y)).

  2. Tom C reporter

    Presumably the image is drawn at the top left of the hitbox, but what if they want it to be in a different place? Each blit call would have to take the top left coords and add an offset to them, which means you've now got 2 vectors floating around, 1 for the hitbox anchor (relative tot he window) and one for the image anchor (relative to the hitbox topleft)

  3. Log in to comment