Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testGetConstraints(self):
s = p.Space()
b1 = p.Body(1,1)
b2 = p.Body(1,1)
s.add(b1)
j1 = p.PivotJoint(b1,s.static_body,(0,0))
j2 = p.PivotJoint(b2,s.static_body,(0,0))
self.assertTrue(j1 in b1.constraints)
self.assertTrue(j1 not in b2.constraints)
self.assertTrue(j1 in s.static_body.constraints)
self.assertTrue(j2 in s.static_body.constraints)
def testAnchorByPivot(self):
a,b = p.Body(10,10), p.Body(20,20)
a.position = (5,7)
j = p.PivotJoint(a, b, (1,2))
self.assertEqual(j.anchor_a, (-4,-5))
self.assertEqual(j.anchor_b, (1,2))
def testAnchorByAnchor(self):
a,b = p.Body(10,10), p.Body(20,20)
j = p.PivotJoint(a, b, (1,2), (3,4))
self.assertEqual(j.anchor_a, (1,2))
self.assertEqual(j.anchor_b, (3,4))
j.anchor_a = (5,6)
j.anchor_b = (7,8)
self.assertEqual(j.anchor_a, (5,6))
self.assertEqual(j.anchor_b, (7,8))
for i in range(len(bs)-1):
add_joint(bs[i], bs[i+1])
i2 = i+20
if len(bs) > i2:
add_joint(bs[i], bs[i2])
### WEB ATTACH POINTS
static_bs = []
for b in bs[-17::4]:
static_body = pymunk.Body(body_type = pymunk.Body.STATIC)
static_body.position = b.position
static_bs.append(static_body)
j = pymunk.PivotJoint(static_body, b, static_body.position)
j = pymunk.DampedSpring(static_body, b, (0,0), (0,0), 0, 0, 0)
j.damping = 100
j.stiffness = 20000
space.add(j)
### ALL SETUP DONE
def update(dt):
# Note that we dont use dt as input into step. That is because the
# simulation will behave much better if the step size doesnt change
# between frames.
r = 10
for x in range(r):
space.step(1./30./r)
pyglet.clock.schedule_interval(update, 1/30.)
# paddle_w = 200
# paddle_h = 2
# paddle_body_position = 300.0, 300.0
# joint_point = paddle_body_position
paddle_mass = 30
paddle_body = pymunk.Body(mass=paddle_mass, moment=pymunk.moment_for_box(paddle_mass, (paddle_w, paddle_h)))
paddle_body.position = paddle_body_position
paddle_shape = pymunk.Poly.create_box(paddle_body, (paddle_w, paddle_h))
paddle_shape.elasticity = 1.0
paddle_shape.friction = 0.0
paddle_shape.color = (0, 0, 0, 255)
space.add(paddle_body, paddle_shape)
pj_body = pymunk.Body(body_type=pymunk.Body.STATIC)
pj_body.position = paddle_body.position
pj = pymunk.PivotJoint(pj_body, paddle_body, joint_point)
pj.elasticity = 1.0
pj.friction = 0.0
space.add(pj)
timestep = 0
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == KEYDOWN and event.key == K_ESCAPE:
running = False
elif event.type == KEYDOWN and event.key == K_p:
pygame.image.save(screen, "bouncing_balls.png")
### Clear screen
screen.fill(THECOLORS["white"])
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)
a.position = (550,400)
sa = pymunk.Circle(a, 20)
b = pymunk.Body(1,1)
b.position = (650,420)
sb = pymunk.Circle(b, 20)
j = pymunk.PivotJoint(a, b, (600,390))
space.add(sa, sb, a, b, j)
# GrooveJoints
captions.append(((760,660), "Groove Joint"))
a = pymunk.Body(1,1)
a.position = (750,600)
sa = pymunk.Circle(a, 20)
b = pymunk.Body(1,1)
b.position = (850,620)
sb = pymunk.Circle(b, 20)
j = pymunk.GrooveJoint(a, b, (40,40), (40,-40), (-60,0))
space.add(sa, sb, a, b, j)
# DampedSpring
captions.append(((760,550), "Damped Spring"))
def connect(self, shape):
join = pymunk.PivotJoint(shape.body, self.body, self.body.position)
self.shape.append(join)
space.add(join)
def stick_arrow_to_target(arrow_body, target_body, position, space):
pivot_joint = pymunk.PivotJoint(arrow_body, target_body, position)
phase = target_body.angle - arrow_body.angle
gear_joint = pymunk.GearJoint(arrow_body, target_body,phase,1)
space.add(pivot_joint)
space.add(gear_joint)
def __init__(self, a, b, position, range=(-pi, pi), max_rate=2.0):
self.pivot = pymunk.PivotJoint(a, b, position)
self.motor = pymunk.SimpleMotor(a, b, 0)
self.motor.max_force = 1e7
self.limit = pymunk.RotaryLimitJoint(a, b, range[0], range[1])
self.limit.max_force = 1e1
self.limit.max_bias = 0.5
self.max_rate = max_rate