我想在 pymunk 中制作一个 2D 抓手,它可以旋转、向上/向下和向两侧移动并握紧或伸展手指,基本上,手看起来像“U”字母。我试图通过制作 3 个独立的身体(手掌、左手指、右手指)并通过一些关节(PinJoint 和可能不同的关节)连接它来做到这一点。问题是当我旋转手掌(通过改变角度或角度速度)时,手指开始出现奇怪的动作。另外,我想让手指也可以伸展/握紧。
这是我的抓手代码:
class Gripper(pymunk.Body):
def __init__(self, space):
super().__init__(2**8, pymunk.inf)
self.position = (100, 25)
palm = pymunk.Segment(self, (-7, 0), (7, 0), 2)
# Left gripper side
self.l_phalanx_body = pymunk.Body(2**8, pymunk.inf)
self.l_phalanx_body.position = (self.position.x - 7, self.position.y)
phalanx_l1 = pymunk.Segment(self.l_phalanx_body, (0, 0), (0, 16), 2)
phalanx_l2 = pymunk.Segment(self.l_phalanx_body, (0, 16), (4, 16), 2)
l_joint = pymunk.PinJoint(self, self.l_phalanx_body, (-7, 0))
# l_rot_joint = pymunk.DampedRotarySpring(self, self.l_phalanx_body, 0, 1, 1)
# Right gripper side
self.r_phalanx_body = pymunk.Body(2**8, pymunk.inf)
self.r_phalanx_body.position = (self.position.x + 7, self.position.y)
phalanx_r1 = pymunk.Segment(self.r_phalanx_body, (0, 0), (0, 16), 2)
phalanx_r2 = pymunk.Segment(self.r_phalanx_body, (0, 16), (-4, 16), 2)
r_joint = pymunk.PinJoint(self, self.r_phalanx_body, (7, 0), (0, 0))
shape_filter = pymunk.ShapeFilter(group=1)
palm.filter = shape_filter
phalanx_l1.filter = shape_filter
phalanx_l2.filter = shape_filter
phalanx_r1.filter = shape_filter
phalanx_r2.filter = shape_filter
space.add(
self, palm,
l_joint,
# l_rot_joint,
self.l_phalanx_body,
phalanx_l1, phalanx_l2,
r_joint, self.r_phalanx_body,
phalanx_r1, phalanx_r2,
)