Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions arcade/hitbox/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ def __call__(self, *args: Any, **kwds: Any) -> Self:
"""
return self.__class__(*args, **kwds) # type: ignore

def create_bounding_box(self, image: Image) -> PointList:
"""
Create points for a simple bounding box around an image.
This is often used as a fallback if a hit box algorithm
doesn't manage to figure out any reasonable points for
an image.

:param Image image: The image to create a bounding box for.
:return: A tuple of hit box points.
"""
size = image.size
return (
(-size[0] / 2, -size[1] / 2),
(size[0] / 2, -size[1] / 2),
(size[0] / 2, size[1] / 2),
(-size[0] / 2, size[1] / 2),
)


class HitBox:
"""
Expand Down
8 changes: 1 addition & 7 deletions arcade/hitbox/bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,4 @@ def calculate(self, image: Image, **kwargs) -> PointList:

:Returns: List of points
"""
size = image.size
return (
(-size[0] / 2, -size[1] / 2),
(size[0] / 2, -size[1] / 2),
(size[0] / 2, size[1] / 2),
(-size[0] / 2, size[1] / 2),
)
return self.create_bounding_box(image)
2 changes: 1 addition & 1 deletion arcade/hitbox/pymunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def calculate(self, image: Image, detail: Optional[float] = None, **kwargs) -> P
# Trace the image finding all the outlines and holes
line_sets = self.trace_image(image)
if len(line_sets) == 0:
return tuple()
return self.create_bounding_box(image)

# Get the largest line set
line_set = self.select_largest_line_set(line_sets)
Expand Down
2 changes: 1 addition & 1 deletion arcade/hitbox/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def calculate(self, image: Image, **kwargs) -> PointList:
bbox = image.getbbox()
# If there is no bounding box the image is empty
if bbox is None:
return tuple()
return self.create_bounding_box(image)

left_border, top_border, right_border, bottom_border = bbox
right_border -= 1
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/htibox/test_black_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Test fallback for hitbox creation with empty textures.
"""
from PIL import Image
import arcade

EXPECTED = ((-50.0, -50.0), (50.0, -50.0), (50.0, 50.0), (-50.0, 50.0))


def test_simple():
tex = arcade.Texture(
Image.new("RGBA", (100, 100)),
hit_box_algorithm=arcade.hitbox.algo_simple,
)
assert tex.hit_box_points == EXPECTED


def test_pymunk():
tex = arcade.Texture(
Image.new("RGBA", (100, 100)),
hit_box_algorithm=arcade.hitbox.algo_detailed,
)
assert tex.hit_box_points == EXPECTED