From 5838d811503bb57893c76242c628bcb9f37a6f93 Mon Sep 17 00:00:00 2001 From: Darren Eberly Date: Tue, 20 May 2025 10:16:47 -0400 Subject: [PATCH 1/3] Add method for updating a sprite's hitbox in pymunk --- arcade/pymunk_physics_engine.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/arcade/pymunk_physics_engine.py b/arcade/pymunk_physics_engine.py index 7e966d5724..1db44cecd3 100644 --- a/arcade/pymunk_physics_engine.py +++ b/arcade/pymunk_physics_engine.py @@ -600,6 +600,31 @@ def _f4(arbiter, space, data): if separate_handler: h.separate = _f4 + def update_sprite(self, sprite: Sprite) -> None: + """ + Updates a Sprite's Shape to match it's current hitbox + + Args: + sprite: The Sprite to update + """ + physics_object = self.sprites[sprite] + old_shape = physics_object.shape + assert old_shape is not None, "Tried to update the shape for a Sprite which does not currently have a shape" + + # Set the physics shape to the sprite's hitbox + poly = sprite.hit_box.points + scaled_poly = [[x * sprite.scale_x for x in z] for z in poly] + shape = pymunk.Poly(physics_object.body, scaled_poly, radius=old_shape.radius) # type: ignore + + shape.collision_type = old_shape.collision_type + shape.elasticity = old_shape.elasticity + shape.friction = old_shape.friction + + self.space.remove(old_shape) + self.space.add(shape) + physics_object.shape = shape + + def resync_sprites(self) -> None: """ Set visual sprites to be the same location as physics engine sprites. From 8f4c7a1d331d8f5e3932a59040af7bae3b1031d6 Mon Sep 17 00:00:00 2001 From: Darren Eberly Date: Tue, 20 May 2025 11:08:12 -0400 Subject: [PATCH 2/3] Formatting --- arcade/pymunk_physics_engine.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arcade/pymunk_physics_engine.py b/arcade/pymunk_physics_engine.py index 1db44cecd3..49ad237f07 100644 --- a/arcade/pymunk_physics_engine.py +++ b/arcade/pymunk_physics_engine.py @@ -609,7 +609,9 @@ def update_sprite(self, sprite: Sprite) -> None: """ physics_object = self.sprites[sprite] old_shape = physics_object.shape - assert old_shape is not None, "Tried to update the shape for a Sprite which does not currently have a shape" + assert old_shape is not None, """ + Tried to update the shape for a Sprite which does not currently have a shape + """ # Set the physics shape to the sprite's hitbox poly = sprite.hit_box.points From 75d18bcf80ded20d4247a6f127e7a98dd5d3efce Mon Sep 17 00:00:00 2001 From: Darren Eberly Date: Tue, 20 May 2025 11:15:36 -0400 Subject: [PATCH 3/3] formatting --- arcade/pymunk_physics_engine.py | 1 - 1 file changed, 1 deletion(-) diff --git a/arcade/pymunk_physics_engine.py b/arcade/pymunk_physics_engine.py index 49ad237f07..04e0e721d4 100644 --- a/arcade/pymunk_physics_engine.py +++ b/arcade/pymunk_physics_engine.py @@ -626,7 +626,6 @@ def update_sprite(self, sprite: Sprite) -> None: self.space.add(shape) physics_object.shape = shape - def resync_sprites(self) -> None: """ Set visual sprites to be the same location as physics engine sprites.