How to use the pymunk.Body function in pymunk

To help you get started, we’ve selected a few pymunk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github noio / peas / peas / tasks / walking.py View on Github external
pygame.init()
            screen = pygame.display.set_mode((self.track_length, 200))
            pygame.display.set_caption("Simulation")
            clock = pygame.time.Clock()
            running = True
            font = pygame.font.Font(pygame.font.get_default_font(), 8)
        
        # Initialize pymunk
        self.space = space = pymunk.Space()
        space.gravity = (0.0, 900.0)
        space.damping = 0.7
        self.touching_floor = False
        # Create objects
        # Floor

        floor = pymunk.Body()
        floor.position = pymunk.Vec2d(self.track_length/2.0 , 210)
        sfloor = pymunk.Poly.create_box(floor, (self.track_length, 40))
        sfloor.friction = 1.0
        sfloor.collision_type = 1
        space.add_static(sfloor)

        # Torso
        torsolength = 20 + (self.num_legs // 2 - 1) * self.leg_spacing
        mass = torsolength * self.torso_height * self.torso_density
        torso = pymunk.Body(mass, pymunk.moment_for_box(mass, torsolength, self.torso_height))
        torso.position = pymunk.Vec2d(200, 200 - self.leg_length * 2 - self.torso_height)
        storso = pymunk.Poly.create_box(torso, (torsolength, self.torso_height))
        storso.group = 1
        storso.collision_type = 1
        storso.friction = 2.0
        # space.add_static(storso)
github viblo / pymunk / tests / test_space.py View on Github external
def testReindexStaticCollision(self):
        s = p.Space()
        b1 = p.Body(10, 1000)
        c1 = p.Circle(b1, 10)
        b1.position = 20, 20

        b2 = p.Body(body_type=p.Body.STATIC)
        s2 = p.Segment(b2, (-10,0), (10,0),1)

        s.add(b1,c1)
        s.add(s2)

        s2.unsafe_set_endpoints((-10,0), (100,0))
        s.gravity = 0, -100

        for x in range(10):
            s.step(.1)
            
        self.assertTrue(b1.position.y < 0)

        b1.position = 20,20
        b1.velocity = 0,0
        s.reindex_static()
github viblo / pymunk / tests / test_constraint.py View on Github external
def testMaxBias(self):
        a,b = p.Body(10,10), p.Body(10,10)
        j = p.PivotJoint(a, b, (0,0))
        self.assertEqual(j.max_bias, p.inf)
        j.max_bias = 10
        self.assertEqual(j.max_bias, 10)
github viblo / pymunk / tests / test_arbiter.py View on Github external
def testShapes(self):
        s = p.Space()
        s.gravity = 0,-100
        
        b1 = p.Body(1, 30)
        c1 = p.Circle(b1, 10)
        b1.position = 5, 3
        c1.collision_type = 1
        c1.friction = 0.5
        
        b2 = p.Body(body_type = p.Body.STATIC)
        c2 = p.Circle(b2, 10)
        c2.collision_type = 2
        c2.friction = 0.8
        
        s.add(b1, c1, b2, c2)
        
        self.called = False
        def pre_solve(arb, space, data):
            self.called = True
            self.assertEqual(len(arb.shapes), 2)
            self.assertEqual(arb.shapes[0], c1)
            self.assertEqual(arb.shapes[1], c2)
            return True
        
        s.add_collision_handler(1,2).pre_solve = pre_solve
github MultiNEAT / MultiNEAT / examples / NoveltySearch.py View on Github external
def main():
    pygame.init()
    screen = pygame.display.set_mode((600, 600))
    if ns_on:
        pygame.display.set_caption("Novelty Search [Press F to turn on/off fast mode]")
    else:
        pygame.display.set_caption("Fitness Search [Press F to turn on/off fast mode]")


    ### Physics stuff
    space = pm.Space()
    space.gravity = Vec2d(0.0, 0.0)

    # walls - the left-top-right walls
    body = pm.Body()
    walls= [# the enclosure
            pm.Segment(body, (50, 50), (50, 550), 5),
            pm.Segment(body, (50, 550), (560, 550), 5),
            pm.Segment(body, (560, 550), (560, 50), 5),
            pm.Segment(body, (50, 50), (560, 50), 5),

            # the obstacle walls
            pm.Segment(body, (120, 480), (560, 480), 5),
            pm.Segment(body, (180, 480), (180, 180), 5),
            pm.Segment(body, (320, 50), (320, 360), 5),
            pm.Segment(body, (440, 480), (440, 360), 5),
            ]

    for s in walls:
        s.friction = 0
        s.elasticity = 0.99
github estevaofon / angry-birds-python / examples / spiderweb.py View on Github external
### 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.)

selected = None
selected_joint = None
mouse_body = pymunk.Body()

@window.event
def on_mouse_press(x, y, button, modifiers):
    mouse_body.position = x,y
    hit = space.nearest_point_query_nearest((x,y),10)
    if hit != None:
        global selected
        body = hit['shape'].body
        rest_length = mouse_body.position.get_distance(body.position)
        stiffness = 1000
        damping = 10
        selected = pymunk.DampedSpring(mouse_body, body, (0,0), (0,0), rest_length, stiffness, damping)
        space.add(selected)

@window.event
def on_mouse_release(x, y, button, modifiers):
github viblo / pymunk / examples / shapes_for_draw_demos.py View on Github external
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"))
    a = pymunk.Body(1,1)
    a.position = (750,480)
    sa = pymunk.Circle(a, 20)
    b = pymunk.Body(1,1)
    b.position = (850,500)
    sb = pymunk.Circle(b, 20)
    j = pymunk.DampedSpring(a, b, (0,0), (0,10), 100, 1,1)
    space.add(sa, sb, a, b, j)
    
    # DampedRotarySpring
    captions.append(((740,430), "Damped Rotary Spring"))
    a = pymunk.Body(1,1)
    a.position = (750,350)
    sa = pymunk.Circle(a, 20)
    b = pymunk.Body(1,1)
    b.position = (850,380)
    sb = pymunk.Circle(b, 20)
    j = pymunk.DampedRotarySpring(a, b, 10, 1, 1)
    space.add(sa, sb, a, b, j)
github viblo / pymunk / examples / pymunx.py View on Github external
def add_wall(self, p1, p2, friction=1.0, elasticity=0.1, mass=inf, inertia=inf):
		""" Adds a fixed Wall pos = (int(x), int(y))
		    Parameter: p1 == pos(startpoint), p2 == pos(endpoint)
		    Optional: See #physical_parameters
		    Returns: pymunk.Shape() (=> .Segment())
		"""
		body = pm.Body(mass, inertia)
		shape= pm.Segment(body, self.Vec2df(p1), self.Vec2df(p2), 2.0)	
		shape.friction = friction
		shape.elasticity = elasticity
		
		shape.color = self.get_color()
		shape.color2 = self.get_color()

		self.space.add(shape)
		self.element_count += 1
		return shape
github jseidl / virtuaplant / plants / bottle-filling / world.py View on Github external
def add_ball(space):
    mass = 0.01
    radius = 3
    inertia = pymunk.moment_for_circle(mass, 0, radius, (0,0))
    body = pymunk.Body(mass, inertia)
    body._bodycontents.v_limit = 120
    body._bodycontents.h_limit = 1
    x = random.randint(181,182)
    body.position = x, 410
    shape = pymunk.Circle(body, radius, (0,0))
    shape.collision_type = 0x5 #liquid
    space.add(body, shape)
    return shape
github viblo / pymunk / examples / copy_and_pickle.py View on Github external
draw_options1 = pymunk.pygame_util.DrawOptions(surf1) 
    draw_options2 = pymunk.pygame_util.DrawOptions(surf2) 
    
    box = [(5,5), (295,5), (295,295), (5,295)]
    for i,p1 in enumerate(box):
        if i+1 >= len(box):
            p2 = box[0]
        else:
            p2 = box[i+1]
        l = pymunk.Segment(space1.static_body, p1, p2, 5)
        l.elasticity = 0.5
        l.friction = 1
        
        space1.add(l)

    template_box = pymunk.Poly.create_box(pymunk.Body(), (20,20))
    template_box.mass = 1
    template_box.friction = 1

    for x in range(3):
        for y in range(7):
            box = template_box.copy()
            box.body.position = 200+x*30, 10+y*20
            space1.add(box, box.body)
            
    b = pymunk.Body()
    b.position = 30,100
    ball = pymunk.Circle(b, 20)
    ball.mass = 20
    ball.friction = 1
    ball.color = THECOLORS['red']
    space1.add(ball, b)