旋转在pymunk的身体组

时间:2018-06-06 18:22:55

标签: python pygame simulation chipmunk pymunk

我想用pymunk做一个2D抓握手,它可以旋转,上下移动到两侧并握紧或伸出手指,基本上,手看起来像'U'字母。我尝试通过制作3个独立的身体(手掌,左手指,右手指)并通过一些关节(PinJoint和可能是一些不同的关节)连接它。问题是当我旋转手掌时(通过改变角度或angular_velocity),手指开始奇怪地行动。另外,我也希望手指可伸缩/可切换。

这是我的抓手代码:

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,
        )

1 个答案:

答案 0 :(得分:0)

也许是因为你通过直接设置速度来旋转。您可以尝试在掌体和空间静态体之间使用SimpleMotor约束进行旋转。以下是SimpleMotor的api文档:http://www.pymunk.org/en/latest/pymunk.constraint.html#pymunk.constraint.SimpleMotor