Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testImpulse(self):
a,b = p.Body(p.inf, p.inf), p.Body(10,10)
b.position = 0,10
j = p.PinJoint(a,b)
s = p.Space()
s.gravity = 0,10
s.add(b,j)
self.assertEqual(j.impulse, 0)
s.step(1)
self.assertEqual(j.impulse, 100)
def __init__(self, space, p1, shape1, p2, shape2):
# Associate the pin with the location of one of the bodies so
# it is removed when that body is out of the simulation
self.body = shape1.body
ax = p1[0] - shape1.body.position.x
ay = p1[1] - shape1.body.position.y
bx = p2[0] - shape2.body.position.x
by = p2[1] - shape2.body.position.y
self.shape = pymunk.PinJoint(shape1.body, shape2.body, (ax, ay), (bx, by))
super().__init__()
space.add(self.shape)
b.position = (400,430)
s = pymunk.Poly(b, [(0, -50), (50, 0), (30, 50),(-30, 50),(-50, 0)],
pymunk.Transform(ty=-100))
space.add(s)
###Constraints
# PinJoints
captions.append(((560,660), "Pin Joint"))
a = pymunk.Body(1,1)
a.position = (550,600)
sa = pymunk.Circle(a, 20)
b = pymunk.Body(1,1)
b.position = (650,620)
sb = pymunk.Circle(b, 20)
j = pymunk.PinJoint(a, b, anchor_a=(0,0), anchor_b=(0,-20))
space.add(sa, sb, a, b, j)
# SlideJoints
captions.append(((560,560), "Slide Joint"))
a = pymunk.Body(1,1)
a.position = (550,500)
sa = pymunk.Circle(a, 20)
b = pymunk.Body(1,1)
b.position = (650,520)
sb = pymunk.Circle(b, 20)
j = pymunk.SlideJoint(a, b, anchor_a=(0,20), anchor_b=(0,-20), min=10, max=30)
space.add(sa, sb, a, b, j)
# PivotJoints
captions.append(((560,460), "Pivot Joint"))
a = pymunk.Body(1,1)
def grab(self):
""" Grab something """
# See if we have a physics object to our right
check_point = (self.player.right + 10, self.player.center_y)
shape_list = self.space.point_query(check_point, 1, pymunk.ShapeFilter())
# Create a joint for an item to our right
for shape in shape_list:
self.grab_joint = pymunk.PinJoint(self.player.shape.body, shape.shape.body)
self.space.add(self.grab_joint)
r_flipper_joint_body = pymunk.Body()
r_flipper_joint_body.position = r_flipper_body.position
j = pymunk.PinJoint(r_flipper_body, r_flipper_joint_body, (0, 0), (0, 0))
s = pymunk.DampedRotarySpring(r_flipper_body, r_flipper_joint_body, 0.15, 20000000, 900000)
space.add(j, s)
# left flipper
l_flipper_body = pymunk.Body(mass, moment)
l_flipper_body.position = 10, 10
l_flipper_shape = pymunk.Poly(l_flipper_body, [(-x, y) for x, y in fp])
space.add(l_flipper_body, l_flipper_shape)
l_flipper_joint_body = pymunk.Body()
l_flipper_joint_body.position = l_flipper_body.position
j = pymunk.PinJoint(l_flipper_body, l_flipper_joint_body, (0, 0), (0, 0))
s = pymunk.DampedRotarySpring(l_flipper_body, l_flipper_joint_body, -0.15, 20000000, 900000)
space.add(j, s)
r_flipper_shape.group = l_flipper_shape.group = 1
r_flipper_shape.elasticity = l_flipper_shape.elasticity = 0.4
return r_flipper_body, r_flipper_shape, l_flipper_body, l_flipper_shape
line.group = 1
space.add(static_lines)
fp = [(20,-20), (-120, 0), (20,20)]
mass = 100
moment = pymunk.moment_for_poly(mass, fp)
# right flipper
r_flipper_body = pymunk.Body(mass, moment)
r_flipper_body.position = 450, 100
r_flipper_shape = pymunk.Poly(r_flipper_body, fp)
space.add(r_flipper_body, r_flipper_shape)
r_flipper_joint_body = pymunk.Body(body_type=pymunk.Body.KINEMATIC)
r_flipper_joint_body.position = r_flipper_body.position
j = pymunk.PinJoint(r_flipper_body, r_flipper_joint_body, (0,0), (0,0))
#todo: tweak values of spring better
s = pymunk.DampedRotarySpring(r_flipper_body, r_flipper_joint_body, 0.15, 20000000,900000)
space.add(j, s)
# left flipper
l_flipper_body = pymunk.Body(mass, moment)
l_flipper_body.position = 150, 100
l_flipper_shape = pymunk.Poly(l_flipper_body, [(-x,y) for x,y in fp])
space.add(l_flipper_body, l_flipper_shape)
l_flipper_joint_body = pymunk.Body(body_type=pymunk.Body.KINEMATIC)
l_flipper_joint_body.position = l_flipper_body.position
j = pymunk.PinJoint(l_flipper_body, l_flipper_joint_body, (0,0), (0,0))
s = pymunk.DampedRotarySpring(l_flipper_body, l_flipper_joint_body, -0.15, 20000000, 900000)
space.add(j, s)
def _draw_constraint(surface, constraint):
if isinstance(constraint, pymunk.GrooveJoint) and hasattr(constraint, "groove_a"):
pv1 = constraint.a.position + constraint.groove_a
pv2 = constraint.a.position + constraint.groove_b
p1 = to_pygame(pv1, surface)
p2 = to_pygame(pv2, surface)
pygame.draw.aalines(surface, pygame.color.THECOLORS["darkgray"], False, [p1,p2])
elif isinstance(constraint, pymunk.PinJoint):
pv1 = constraint.a.position + constraint.anchr1.rotated(constraint.a.angle)
pv2 = constraint.b.position + constraint.anchr2.rotated(constraint.b.angle)
p1 = to_pygame(pv1, surface)
p2 = to_pygame(pv2, surface)
pygame.draw.aalines(surface, pygame.color.THECOLORS["darkgray"], False, [p1,p2])
elif isinstance(constraint, pymunk.GearJoint):
pv1 = constraint.a.position
pv2 = constraint.a.position
p1 = to_pygame(pv1, surface)
p2 = to_pygame(pv2, surface)
pygame.draw.circle(surface, pygame.color.THECOLORS["darkgray"], p1, 3)
pygame.draw.circle(surface, pygame.color.THECOLORS["darkgray"], p2, 3)
elif hasattr(constraint, "anchr1"):
pv1 = constraint.a.position + constraint.anchr1.rotated(constraint.a.angle)
pv2 = constraint.b.position + constraint.anchr2.rotated(constraint.b.angle)
p1 = to_pygame(pv1, surface)
# right flipper
self.right_flipper_poly = arcade.create_polygon(vertices, arcade.color.WHITE, 1)
self.right_flipper_shape_list = arcade.ShapeElementList()
self.right_flipper_shape_list.append(self.right_flipper_poly)
self.right_flipper_shape_list.center_x = 2
self.right_flipper_shape_list.center_y = 2
self.right_flipper_body = pymunk.Body(mass, moment)
self.right_flipper_body.position = pymunk.Vec2d(self.right_flipper_shape_list.center_x, self.right_flipper_shape_list.center_y)
self.right_flipper_pymunk_shape = pymunk.Poly(self.right_flipper_body, vertices)
self.space.add(self.right_flipper_body, self.right_flipper_pymunk_shape)
r_flipper_joint_body = pymunk.Body(body_type=pymunk.Body.KINEMATIC)
r_flipper_joint_body.position = pymunk.Vec2d(self.right_flipper_shape_list.center_x, self.right_flipper_shape_list.center_y)
j = pymunk.PinJoint(self.right_flipper_body, r_flipper_joint_body, (0, 0), (5,5))
#s = pymunk.DampedRotarySpring(self.right_flipper.body, r_flipper_joint_body, 0, 500, 110)
#self.space.add(j, s)
self.space.add(j, r_flipper_joint_body)
print("X1", self.right_flipper_shape_list.center_x, self.right_flipper_shape_list.center_y)
print("X2", self.right_flipper_body.position.x, self.right_flipper_body.position.y)
"""
vertices = [(0.5, -0.5), (-3, 0), (0.5, 0.5)]
vs = [(0,0),(25,45),(0,45)]
shovel_s = pymunk.Poly(chassi_b, vs, transform = pymunk.Transform(tx=85))
shovel_s.friction = 0.5
shovel_s.color = shovel_color
space.add(shovel_s)
wheel1_b.position = pos - (55,0)
wheel2_b.position = pos + (55,0)
chassi_b.position = pos + (0,-25)
space.add(
pymunk.PinJoint(wheel1_b, chassi_b, (0,0), (-25,-15)),
pymunk.PinJoint(wheel1_b, chassi_b, (0,0), (-25, 15)),
pymunk.PinJoint(wheel2_b, chassi_b, (0,0), (25,-15)),
pymunk.PinJoint(wheel2_b, chassi_b, (0,0), (25, 15))
)
speed = 4
space.add(
pymunk.SimpleMotor(wheel1_b, chassi_b, speed),
pymunk.SimpleMotor(wheel2_b, chassi_b, speed)
)
def add_L(space):
rotation_center_body = pymunk.Body()
rotation_center_body.position = (300, 300)
rotation_limit_body = pymunk.Body()
rotation_limit_body.position = (200, 300)
body = pymunk.Body(10, 10000)
body.position = (300, 300)
l1 = pymunk.Segment(body, (-150, 0), (255.0, 0.0), 5.0)
l2 = pymunk.Segment(body, (-150.0, 0), (-150.0, 50.0), 5.0)
rotation_center_joint = pymunk.PinJoint(body,
rotation_center_body,
(0, 0),
(0, 0))
joint_limit = 25
rotation_limit_joint = pymunk.SlideJoint(body,
rotation_limit_body,
(-100, 0),
(0, 0),
0,
joint_limit)
space.add(l1, l2, body, rotation_center_joint, rotation_limit_joint)
return l1, l2