Skip to content

Commit 1c4d3d5

Browse files
authored
Add method for syncing a Sprite's hitbox to it's pymunk Shape (#2691)
* Add method for updating a sprite's hitbox in pymunk * Formatting * formatting
1 parent 0b0200f commit 1c4d3d5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

arcade/pymunk_physics_engine.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,32 @@ def _f4(arbiter, space, data):
600600
if separate_handler:
601601
h.separate = _f4
602602

603+
def update_sprite(self, sprite: Sprite) -> None:
604+
"""
605+
Updates a Sprite's Shape to match it's current hitbox
606+
607+
Args:
608+
sprite: The Sprite to update
609+
"""
610+
physics_object = self.sprites[sprite]
611+
old_shape = physics_object.shape
612+
assert old_shape is not None, """
613+
Tried to update the shape for a Sprite which does not currently have a shape
614+
"""
615+
616+
# Set the physics shape to the sprite's hitbox
617+
poly = sprite.hit_box.points
618+
scaled_poly = [[x * sprite.scale_x for x in z] for z in poly]
619+
shape = pymunk.Poly(physics_object.body, scaled_poly, radius=old_shape.radius) # type: ignore
620+
621+
shape.collision_type = old_shape.collision_type
622+
shape.elasticity = old_shape.elasticity
623+
shape.friction = old_shape.friction
624+
625+
self.space.remove(old_shape)
626+
self.space.add(shape)
627+
physics_object.shape = shape
628+
603629
def resync_sprites(self) -> None:
604630
"""
605631
Set visual sprites to be the same location as physics engine sprites.

0 commit comments

Comments
 (0)