Skip to content

Commit

Permalink
Fix hitbox and transformations (#1968)
Browse files Browse the repository at this point in the history
* Update

* Update

* Update

* Update

* Update
  • Loading branch information
pvcraven committed Feb 11, 2024
1 parent 5d069d0 commit 6097397
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 23 deletions.
5 changes: 5 additions & 0 deletions arcade/experimental/texture_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def __init__(self):
for i, tex in enumerate(self.textures):
self.spritelist.append(arcade.Sprite(tex, center_x=100 + 130 * i, center_y=300))

for i in range(len(TRANSFORMS)):
sprite = self.spritelist[i]
sprite.texture = sprite.texture.transform(TRANSFORMS[i])
sprite.sync_hit_box_to_texture()

def on_draw(self):
self.clear()
self.spritelist.draw()
Expand Down
11 changes: 11 additions & 0 deletions arcade/sprite/sprite.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,14 @@ def register_physics_engine(self, physics_engine: Any) -> None:
or a custom one you made.
"""
self.physics_engines.append(physics_engine)

def sync_hit_box_to_texture(self):
"""
Update the sprite's hit box to match the current texture's hit box.
"""
self.hit_box = RotatableHitBox(
self.texture.hit_box_points,
position=self._position,
angle=self.angle,
scale=self._scale,
)
10 changes: 5 additions & 5 deletions arcade/texture/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Rotate90Transform(Transform):
def transform_hit_box_points(
points: PointList,
) -> PointList:
return tuple(rotate_point(point[0], point[1], 0, 0, -90) for point in points)
return tuple(rotate_point(point[0], point[1], 0, 0, 90) for point in points)


class Rotate180Transform(Transform):
Expand All @@ -119,7 +119,7 @@ class Rotate180Transform(Transform):
def transform_hit_box_points(
points: PointList,
) -> PointList:
return tuple(rotate_point(point[0], point[1], 0, 0, -180) for point in points)
return tuple(rotate_point(point[0], point[1], 0, 0, 180) for point in points)


class Rotate270Transform(Transform):
Expand All @@ -136,7 +136,7 @@ class Rotate270Transform(Transform):
def transform_hit_box_points(
points: PointList,
) -> PointList:
return tuple(rotate_point(point[0], point[1], 0, 0, -270) for point in points)
return tuple(rotate_point(point[0], point[1], 0, 0, 270) for point in points)


class FlipLeftRightTransform(Transform):
Expand Down Expand Up @@ -191,7 +191,7 @@ def transform_hit_box_points(
points: PointList,
) -> PointList:
points = FlipLeftRightTransform.transform_hit_box_points(points)
points = Rotate90Transform.transform_hit_box_points(points)
points = Rotate270Transform.transform_hit_box_points(points)
return points


Expand All @@ -211,7 +211,7 @@ def transform_hit_box_points(
points: PointList,
) -> PointList:
points = FlipLeftRightTransform.transform_hit_box_points(points)
points = Rotate270Transform.transform_hit_box_points(points)
points = Rotate90Transform.transform_hit_box_points(points)
return points


Expand Down
9 changes: 3 additions & 6 deletions arcade/tilemap/tilemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,16 +576,13 @@ def _create_sprite_from_tile(
print(f"Warning: Hitbox type {type(hitbox)} not supported.")

if tile.flipped_vertically:
for point in points:
point = point[0], point[1] * -1
points = [(point[0], -point[1]) for point in points]

if tile.flipped_horizontally:
for point in points:
point = point[0] * -1, point[1]
points = [(-point[0], point[1]) for point in points]

if tile.flipped_diagonally:
for point in points:
point = point[1], point[0]
points = [(point[1], point[0]) for point in points]

my_sprite.hit_box = RotatableHitBox(
cast(List[Point], points),
Expand Down
23 changes: 11 additions & 12 deletions tests/unit/texture/test_texture_transform_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
TransverseTransform,
VertexOrder,
)
# Hit box points for a 128 x 128 texture
# Hit box points to test for transformations
HIT_BOX_POINTS = (
(-64.0, -64.0),
(64.0, -64.0),
(64.0, 64.0),
(-64.0, 64.0),
(1.0, 1.0),
(2.0, 2.0),
(2.0, 1.0)
)
ORDER = (
VertexOrder.UPPER_LEFT.value,
Expand All @@ -27,7 +26,7 @@ def test_rotate90_transform():
"""Test rotate transform."""
# One rotation
result = Rotate90Transform.transform_hit_box_points(HIT_BOX_POINTS)
assert result == ((64.0, -64.0), (64.0, 64.0), (-64.0, 64.0), (-64.0, -64.0))
assert result == ((1.0, -1.0), (2.0, -2.0), (1.0, -2.0))
# Three more should be the original points
result = Rotate90Transform.transform_hit_box_points(result)
result = Rotate90Transform.transform_hit_box_points(result)
Expand All @@ -45,15 +44,15 @@ def test_rotate90_transform():

def test_rotate180_transform():
result = Rotate180Transform.transform_hit_box_points(HIT_BOX_POINTS)
assert result == ((64.0, 64.0), (-64.0, 64.0), (-64.0, -64.0), (64.0, -64.0))
assert result == ((-1.0, -1.0), (-2.0, -2.0), (-2.0, -1.0))

result = Rotate180Transform.transform_vertex_order(ORDER)
assert result == (3, 2, 1, 0)


def test_rotate270_transform():
result = Rotate270Transform.transform_hit_box_points(HIT_BOX_POINTS)
assert result == ((-64.0, 64.0), (-64.0, -64.0), (64.0, -64.0), (64.0, 64.0))
assert result == ((-1.0, 1.0), (-2.0, 2.0), (-1.0, 2.0))

result = Rotate270Transform.transform_vertex_order(ORDER)
assert result == (1, 3, 0, 2)
Expand All @@ -62,7 +61,7 @@ def test_rotate270_transform():
def test_flip_left_right_transform():
# Flip left to right
result = FlipLeftRightTransform.transform_hit_box_points(HIT_BOX_POINTS)
assert result == ((64.0, -64.0), (-64.0, -64.0), (-64.0, 64.0), (64.0, 64.0))
assert result == ((-1.0, 1.0), (-2.0, 2.0), (-2.0, 1.0))
# Flip back
result = FlipLeftRightTransform.transform_hit_box_points(result)
assert result == HIT_BOX_POINTS
Expand All @@ -77,7 +76,7 @@ def test_flip_left_right_transform():
def test_flip_top_bottom_transform():
# Flip top to bottom
result = FlipTopBottomTransform.transform_hit_box_points(HIT_BOX_POINTS)
assert result == ((-64.0, 64.0), (64.0, 64.0), (64.0, -64.0), (-64.0, -64.0))
assert result == ((1.0, -1.0), (2.0, -2.0), (2.0, -1.0))
# Flip back
result = FlipTopBottomTransform.transform_hit_box_points(result)
assert result == HIT_BOX_POINTS
Expand All @@ -91,7 +90,7 @@ def test_flip_top_bottom_transform():
def test_transpose_transform():
# Transpose
result = TransposeTransform.transform_hit_box_points(HIT_BOX_POINTS)
assert result == ((64.0, 64.0), (64.0, -64.0), (-64.0, -64.0), (-64.0, 64.0))
assert result == ((-1.0, -1.0), (-2.0, -2.0), (-1.0, -2.0))
# Flip back
result = TransposeTransform.transform_hit_box_points(result)
assert result == HIT_BOX_POINTS
Expand All @@ -106,7 +105,7 @@ def test_transpose_transform():
def test_transverse_transform():
# Transverse
result = TransverseTransform.transform_hit_box_points(HIT_BOX_POINTS)
assert result == ((-64.0, -64.0), (-64.0, 64.0), (64.0, 64.0), (64.0, -64.0))
assert result == ((1.0, 1.0), (2.0, 2.0), (1.0, 2.0))
# Flip back
result = TransverseTransform.transform_hit_box_points(result)
assert result == HIT_BOX_POINTS
Expand Down

0 comments on commit 6097397

Please sign in to comment.