Example #1
0
File: main.py Project: viblo/pymunk
    def car(self, space):
        pos = Vec2d(100,100)

        wheel_color = .2,.86,.47
        shovel_color = .86,.47,.2
        mass = 100
        radius = 25
        moment = pymunk.moment_for_circle(mass, 20, radius)
        wheel1_b = pymunk.Body(mass, moment)
        wheel1_s = pymunk.Circle(wheel1_b, radius)
        wheel1_s.friction = 1.5
        wheel1_s.color = wheel_color
        space.add(wheel1_b, wheel1_s)

        mass = 100
        radius = 25
        moment = pymunk.moment_for_circle(mass, 20, radius)
        wheel2_b = pymunk.Body(mass, moment)
        wheel2_s = pymunk.Circle(wheel2_b, radius)
        wheel2_s.friction = 1.5
        wheel2_s.color = wheel_color
        space.add(wheel2_b, wheel2_s)

        mass = 100
        size = (50,30)
        moment = pymunk.moment_for_box(mass, size)
        chassi_b = pymunk.Body(mass, moment)
        chassi_s = pymunk.Poly.create_box(chassi_b, size)
        space.add(chassi_b, chassi_s)

        vs = [(0,0),(0,-45),(25,-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)
        )
        with self.canvas:
            Color(*wheel_color)
            wheel1_s.ky = self.ellipse_from_circle(wheel1_s)
            Color(*wheel_color)
            wheel2_s.ky = self.ellipse_from_circle(wheel2_s)
            Color(*shovel_color)
            chassi_s.ky = Quad(points=self.points_from_poly(chassi_s))
            shovel_s.ky = Triangle(points=self.points_from_poly(shovel_s))
Example #2
0
    def __init__(self, body_mass, body_position, body_size, wheel_base):
        self.body_mass = body_mass
        self.body_position = body_position
        self.body_size = body_size
        self.body_body = pymunk.Body(self.body_mass, 1000)
        self.body_body.position = self.body_position
        self.body_poly = pymunk.Poly.create_box(self.body_body, self.body_size)
        
        self.body_poly.friction = 0.5

        body_space.add(self.body_body, self.body_poly)

        self.wheel_base = wheel_base

        self.wheelL_mass = 1
        self.wheelL_radius = (self.body_size[1]//1.5)
        self.wheelL_position_x = self.body_position[0]-(self.body_size[0]//2)+self.wheelL_radius - self.wheel_base
        self.wheelL_position_y = self.body_position[1] + self.body_size[1]*1.5
        self.wheelL_position = self.wheelL_position_x, self.wheelL_position_y
        
        self.inertiaL = pymunk.moment_for_circle(self.wheelL_mass, 0, self.wheelL_radius)
        self.wheelL_b = pymunk.Body(self.wheelL_mass, self.inertiaL)
        self.wheelL_b.position = self.wheelL_position
        self.wheelL_shape = pymunk.Circle(self.wheelL_b, self.wheelL_radius)
        
        self.wheelL_shape.friction = 0.5
        
        space.add(self.wheelL_b, self.wheelL_shape)

        self.wheelR_mass = 1
        self.wheelR_radius = (self.body_size[1]//1.5)
        self.wheelR_position_x = self.body_position[0]+(self.body_size[0]//2)-self.wheelL_radius + self.wheel_base
        self.wheelR_position_y = self.body_position[1] + self.body_size[1]*1.5
        self.wheelR_position = self.wheelR_position_x, self.wheelR_position_y
        
        self.inertiaR = pymunk.moment_for_circle(self.wheelR_mass, 0, self.wheelR_radius)
        self.wheelR_b = pymunk.Body(self.wheelR_mass, self.inertiaR)
        self.wheelR_b.position = self.wheelR_position
        self.wheelR_shape = pymunk.Circle(self.wheelR_b, self.wheelR_radius)

        self.wheelR_shape.friction = 0.5
        
        space.add(self.wheelR_b, self.wheelR_shape)

        self.left_spring = pymunk.constraint.DampedSpring(self.body_body, self.wheelL_b, (0, self.body_size[1]//2), (0,0), 200, 80.0, 50)
        self.right_spring = pymunk.constraint.DampedSpring(self.body_body, self.wheelR_b, (0, self.body_size[1]//2), (0,0), 200, 80.0, 50)
    
        self.left_groove_start = (-self.body_size[0]//2 + self.wheelL_radius, 0)
        self.left_groove_end = (-self.body_size[0]//2 + self.wheelL_radius - self.wheel_base, self.wheelL_radius + 20)
        
        self.left_groove_joint = pymunk.constraint.GrooveJoint(self.body_body, self.wheelL_b, self.left_groove_start, self.left_groove_end, (0,0))
        
        self.right_groove_start = (self.body_size[0]//2 - self.wheelL_radius, 0)
        self.right_groove_end = (self.body_size[0]//2 - self.wheelR_radius + self.wheel_base, self.wheelR_radius + 20)
        
        self.right_groove_joint = pymunk.constraint.GrooveJoint(self.body_body, self.wheelR_b, self.right_groove_start, self.right_groove_end, (0,0))
        
        space.add(self.left_spring, self.right_spring, self.left_groove_joint, self.right_groove_joint)
    def __init__(self, body_mass, body_position, body_size, wheel_base):
        self.body_mass = body_mass
        self.body_position = body_position
        self.body_size = body_size
        self.body_body = pymunk.Body(self.body_mass, 1000)
        self.body_body.position = self.body_position
        self.body_poly = pymunk.Poly.create_box(self.body_body, self.body_size)
        
        self.body_poly.friction = 0.5

        body_space.add(self.body_body, self.body_poly)

        self.wheel_base = wheel_base

        self.wheelL_mass = 1
        self.wheelL_radius = (self.body_size[1]//1.5)
        self.wheelL_position_x = self.body_position[0]-(self.body_size[0]//2)+self.wheelL_radius - self.wheel_base
        self.wheelL_position_y = self.body_position[1] + self.body_size[1] + 20
        self.wheelL_position = self.wheelL_position_x, self.wheelL_position_y
        
        self.inertiaL = pymunk.moment_for_circle(self.wheelL_mass, 0, self.wheelL_radius)
        self.wheelL_b = pymunk.Body(self.wheelL_mass, self.inertiaL)
        self.wheelL_b.position = self.wheelL_position
        self.wheelL_shape = pymunk.Circle(self.wheelL_b, self.wheelL_radius)
        
        self.wheelL_shape.friction = 0.5
        
        space.add(self.wheelL_b, self.wheelL_shape)

        self.wheelR_mass = 1
        self.wheelR_radius = (self.body_size[1]//1.5)
        self.wheelR_position_x = self.body_position[0]+(self.body_size[0]//2)-self.wheelL_radius + self.wheel_base
        self.wheelR_position_y = self.body_position[1] + self.body_size[1] + 20
        self.wheelR_position = self.wheelR_position_x, self.wheelR_position_y
        
        self.inertiaR = pymunk.moment_for_circle(self.wheelR_mass, 0, self.wheelR_radius)
        self.wheelR_b = pymunk.Body(self.wheelR_mass, self.inertiaR)
        self.wheelR_b.position = self.wheelR_position
        self.wheelR_shape = pymunk.Circle(self.wheelR_b, self.wheelR_radius)

        self.wheelR_shape.friction = 0.5
        
        space.add(self.wheelR_b, self.wheelR_shape)

        self.left_spring = pymunk.constraint.DampedSpring(self.body_body, self.wheelL_b, (-self.body_size[0]//2, 0), (0,0), self.wheelL_position_y, 7.0, 7)
        self.right_spring = pymunk.constraint.DampedSpring(self.body_body, self.wheelR_b, (self.body_size[0]//2, 0), (0,0), self.wheelR_position_y, 7.0, 7)
        self.middle_spring = pymunk.constraint.DampedSpring(self.wheelL_b, self.wheelR_b, (0,0), (0,0), self.body_size[0], 100.0, 5)

        self.left_pinjoint = pymunk.constraint.PinJoint(self.body_body, self.wheelL_b, (0,0), (0,0))
        self.right_pinjoint = pymunk.constraint.PinJoint(self.body_body, self.wheelR_b, (0,0), (0,0))

        
        
        space.add(self.left_spring, self.right_spring, self.left_pinjoint, self.right_pinjoint, self.middle_spring)
Example #4
0
def car(space):
    pos = Vec2d(100,200)

    wheel_color = 52,219,119
    shovel_color = 219,119,52
    mass = 100
    radius = 25
    moment = pymunk.moment_for_circle(mass, 20, radius)
    wheel1_b = pymunk.Body(mass, moment)
    wheel1_s = pymunk.Circle(wheel1_b, radius)
    wheel1_s.friction = 1.5
    wheel1_s.color = wheel_color
    space.add(wheel1_b, wheel1_s)

    mass = 100
    radius = 25
    moment = pymunk.moment_for_circle(mass, 20, radius)
    wheel2_b = pymunk.Body(mass, moment)
    wheel2_s = pymunk.Circle(wheel2_b, radius)
    wheel2_s.friction = 1.5
    wheel2_s.color = wheel_color
    space.add(wheel2_b, wheel2_s)

    mass = 100
    size = (50,30)
    moment = pymunk.moment_for_box(mass, size)
    chassi_b = pymunk.Body(mass, moment)
    chassi_s = pymunk.Poly.create_box(chassi_b, size)
    space.add(chassi_b, chassi_s)

    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)
    )
Example #5
0
    def _build_physics_elements(self):
        self.dynamic_object = []
        for element in self.collect_object_list:
            width, height = element.size
            mass = width * height
            if element.is_circle:
                moment = pymunk.moment_for_circle(mass, 0, 100)
                body = pymunk.Body(mass, moment)
                shape = pymunk.Circle(body, width/2)
                body.elasticity = 0.9
            else:
                vs = [(-width/2, height/2), (width/2, height/2), (width/2, -height/2), (-width/2, -height/2)]
                moment = pymunk.moment_for_poly(mass, vs)
                body = pymunk.Body(mass, moment)
                shape = pymunk.Poly(body, vs)
            shape.friction = 0.6
            body.position = element.position[0]+width/2, self.flipy(element.position[1]+height/2)
            body.angle = math.pi
            self.space.add(body, shape)
            self.dynamic_object.append((element, shape))

        static_body = pymunk.Body()
        for element in self.collide_object_list:
            x, y = element.position[0], self.flipy(element.position[1])
            width, height = element.size[0], element.size[1]
            static_lines = [pymunk.Segment(static_body, (x, y), (x+width, y), 0.0),
                            pymunk.Segment(static_body, (x+width, y), (x+width, y-height), 0.0),
                            pymunk.Segment(static_body, (x+width, y-height), (x, y-height), 0.0),
                            pymunk.Segment(static_body, (x, y-height), (x, y), 0.0),
                            ]
            for l in static_lines:
                l.friction = 0.5
            self.space.add(static_lines)
Example #6
0
    def enter(self):
        self.original = self.parent.entity.avatar.animations['roll'].surface
        self.original = self.original.convert_alpha()
        self.parent.entity.avatar.play('roll')

        entity = self.parent.entity
        old_body = entity.body
        entity.parent.space.remove(entity.bodies, entity.shapes, entity.joints)

        w, h = entity.size
        r = w * .5
        m = pymunk.moment_for_circle(entity.mass*5, 0, r)

        body = pymunk.Body(entity.mass, m)
        body.position = old_body.position + (0, r*2)
        body.velocity = old_body.velocity

        shape = pymunk.Circle(body, r)
        shape.friction = .5

        entity.bodies = [body]
        entity.shapes = [shape]
        entity.joints = []
        entity.parent.space.add(entity.bodies, entity.shapes)

        self.body = body
Example #7
0
 def update_center_of_mass_old(self):
     if len(self.units) == 0: return
     avg_x = 0.0
     avg_y = 0.0
     mass_total = 0.0
     inertia_total = 0.0
     
     for u in self.units:
         mass_total += u.mass
         inertia_total += pymunk.moment_for_circle(
                         u.mass, 0, u.radius, u.offset)
         avg_x += u.mass * u.offset[0]
         avg_y += u.mass * u.offset[1]
         u.remove_shapes()
     avg_x /= mass_total
     avg_y /= mass_total
     new_body_position = self.body.local_to_world((avg_x,avg_y))
     
     old_body = self.body
     physics.space.remove(old_body)
     
     self.body = pymunk.Body(mass_total, inertia_total)
     self.body.position = new_body_position
     self.body.velocity = old_body.velocity
     self.body.angular_velocity = old_body.angular_velocity
     self.body.angle = old_body.angle
     old_body = None
     
     physics.space.add(self.body)
     
     for u in self.units:
         x, y = u.offset[0]-avg_x, u.offset[1]-avg_y
         u.set_offset(x,y)
         u.initialize(self,collision_type=physics.PLAYER)
         u.add_shapes()
Example #8
0
 def update_center_of_mass(self):
     if len(self.units) == 0: return
     avg_x = 0.0
     avg_y = 0.0
     mass_total = 0.0
     inertia_total = 0.0
     
     for u in self.units:
         mass_total += u.mass
         inertia_total += pymunk.moment_for_circle(
                         u.mass, 0, u.radius, u.offset)
         avg_x += u.mass * u.offset[0]
         avg_y += u.mass * u.offset[1]
         #u.remove_shapes()
     avg_x /= mass_total
     avg_y /= mass_total
     new_body_position = self.body.local_to_world((avg_x,avg_y))
     
     self.body.position = new_body_position
     self.body.mass = mass_total
     self.body.moment = inertia_total
     
     for u in self.units:
         x, y = u.offset[0]-avg_x, u.offset[1]-avg_y
         u.set_offset(x,y)
         u.update_shapes()
Example #9
0
 def addBall(self, mass, radius, pos):
     inertia = pymunk.moment_for_circle(mass, 0, radius, (0, 0))
     body = pymunk.Body(mass, inertia)
     body.position = pos
     shape = pymunk.Circle(body, radius, (0, 0))
     shape.elasticity = 0.75
     return shape
Example #10
0
  def __init__(self, level, radius=100, position=(0,0), density=1.0, verticies=None, orbit=None, mass=None, static=False, **kwargs):
    # physics
    if not mass:
      mass = math.pi * radius**2 * density  #4/3 * math.pi * radius**3 * density * 0.1
    inertia = pymunk.moment_for_circle(mass, 0, radius, (0,0))
    body = pymunk.Body(mass, inertia)
    body.position = Vec2d(position)
    body.radius = radius
    shape = pymunk.Circle(body, radius, (0,0))
    shape.friction = 2

    if static:
      level.space.add_static(body,shape)
    else:
      level.space.add(body,shape)

    # visuals
    if not verticies:
      verticies = int(radius / 5.) + 10
    vert_list = generate_circle(radius=radius, steps=verticies)
    display = level.batch.add(verticies, GL_LINE_LOOP, PositionedList(self), ('v2f', vert_list))

    if orbit:
      gravitation = level.gravity * (body.mass + orbit.body.mass)
      distance = body.position.get_distance(orbit.body.position)
      speed = (gravitation/distance)**(0.5)
      body.velocity = (body.position - orbit.body.position).rotated(90).normalized() * speed


    self.body = body
    self.shape = shape
    self.display = display
    self.level = level
Example #11
0
 def create_ball(self, x, y, mass=40, radius=7):
     inertia = pymunk.moment_for_circle(mass, 0, radius, (0, 0))
     body = pymunk.Body(mass, inertia)
     body.position = x, y
     shape = pymunk.Circle(body, radius, (0, 0))
     shape.elasticity = 0.95
     return body, shape
Example #12
0
    def __init__(self, world, position, width=1.6, length=4.0, tire_width=.25, tire_length=.8, skidmarks=None, body_density=1.0):
        mass = width * length * body_density
        inertia = moment_for_box(mass, width, length)
        self.world = world
        body = Body(mass, inertia, )
        body.position = position
        shape = Poly.create_box(body, size=(width, length))
        super(Car, self).__init__(world, body, shape)

        slot_density = .01
        slot_radius = .1
        slot_mass = slot_density * (slot_radius ** 2) * pi
        slot_inertia = moment_for_circle(slot_mass, 0.0, slot_radius)
        #self.slot = Body(slot_mass, slot_inertia)


        flpos = position[0] - width / 2.0 - tire_width * 2, position[1] + length / 2.0
        self.front_left = Tire(self, flpos, tire_width, tire_length, skidmarks=skidmarks, powered=False, density=body_density)


        frpos = position[0] + width / 2.0 + tire_width * 2, position[1] + length / 2.0
        self.front_right = Tire(self, frpos, tire_width, tire_length, skidmarks=skidmarks, powered=False, density=body_density)
        
        rlpos = position[0] - width / 2.0 - tire_width * 2, position[1] - length / 2.0
        self.rear_left = Tire(self, rlpos, tire_width * 1.5, tire_length, steerable=False, skidmarks=skidmarks, density=body_density)

        rrpos = position[0] + width / 2.0 + tire_width * 2, position[1] - length / 2.0
        self.rear_right = Tire(self, rrpos, tire_width * 1.5, tire_length, steerable=False, skidmarks=skidmarks, density=body_density)
        self.tires = [self.front_left, self.front_right, self.rear_left, self.rear_right]
 def __init__(self, x, y):
     self.mass = 1
     self.radius = 14
     self.inertia = pk.moment_for_circle(self.mass, 0, self.radius)
     self.body = pk.Body(self.mass, self.inertia)
     self.body.position = x, y
     self.shape = pk.Circle(self.body, self.radius)
Example #14
0
 def __init__(
             self, col_type, img, damage, 
             x, y, vx, vy, rotate=False, batch=None, group=None
         ):
     super(Bullet, self).__init__(img, x, y, batch=batch, group=group)
     self.damage = damage
     
     if rotate:
         self.rotation = math.degrees(-math.atan2(vy, vx))
     
     radius = 4
     mass = physics.default_density*(radius*radius*2*math.pi)
     inertia = pymunk.moment_for_circle(mass, 0, radius, (0,0))
     self.body = pymunk.Body(mass, inertia)
     self.body.position = (x,y)
     self.body.velocity = (vx, vy)
     
     self.shape = pymunk.Circle(self.body, radius, (0,0))
     self.shape.friction = 1.0
     self.shape.elasticity = 0.0
     self.shape.parent = self
     self.shape.collision_type = col_type
     self.shape.layers = 1
     
     physics.space.add(self.body)
     physics.space.add(self.shape)
     
     physics.body_update_list.append(self)
 def __init__(self, name, width = 0, height = 0, oType = 'loi'):
      
    self.name = name
    self.type = oType
       
    if width == 0 or height == 0:
       mass       = 1
       radius     = 0.5
       inertia    = pymunk.moment_for_circle(mass, 0, radius)
       self.body  = pymunk.Body(mass, inertia)
       self.shape = pymunk.Circle(self.body, radius)
          
       self.dim   = (1,1)
    else:
       mass       = 1
       radius     = 0.5
       inertia   = pymunk.moment_for_box(mass, width, height)
       self.body  = pymunk.Body(mass, inertia)
       verts = [(-width/2,-height/2), (width/2,-height/2),
                 (width/2, height/2), (-width/2,height/2)]
       self.shape = pymunk.Poly(self.body,verts)
       self.shape.friction = 0.01
       self.shape.elasticity = 0.0
          
       self.dim   = (width,height)
Example #16
0
	def __init__(self, scene, tex = 'blinky', position=(0,0)):
		self.start_pos = position
		
		radius, mass = 7, .001

		p_man_moment = pymunk.moment_for_circle(mass, 0, radius)

		self.ghost_b 					= pymunk.Body(mass, p_man_moment)
		self.ghost_b.position 			= position
		self.ghost_shape 				= pymunk.Circle(self.ghost_b, radius)
		self.ghost_shape.friction 		= .5
		self.ghost_shape.elasticity 	= .2
		self.ghost_shape.collision_type = 4

		self.ghost_shape.fill_color = (0,0,0,0)

		scene.space.add(self.ghost_b, self.ghost_shape)

		ghost_img = pyglet.image.load('resources/textures/'+tex+'.png')
		ghost_img.anchor_x, ghost_img.anchor_y = ghost_img.width/2, ghost_img.height/2
		tex = ghost_img.get_texture()
		
		glTexParameteri(tex.target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
		glTexParameteri(tex.target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
		
		self.ghost_s = pyglet.sprite.Sprite(
			ghost_img, batch=scene.normal_batch, group=scene.ordered_group3)

		self.ghost_s.scale = 1

		self.ghost_b.on_tween_complete = self.on_tween_complete
		self.ghost_b.update_tween = self.update_tween

		self.ghost_b.posx = position[0]
		self.ghost_b.posy = position[1]
Example #17
0
    def __init__(self,
            naubino = None,
            pos     = (0, 0),
            name    = None):
        super(Naub, self).__init__()
        self.bind(time = lambda *_: self.property("pos").dispatch(self))
        self.register_event_type('on_remove')
        self.register_event_type('on_remove_merged')

        mass                = Config.naub_mass()
        radius              = self.radius
        inertia             = pymunk.moment_for_circle(mass, radius, radius)
        body                = pymunk.Body(mass, inertia)
        body.position       = pos
        body.data           = self
        shape               = pymunk.Circle(body, radius)
        shape.friction      = Config.naub_friction()
        shape.elasticity    = Config.naub_elasticity()

        self.alive          = True
        self.tag            = None # fill with whatever you like
        self.body           = body
        self.shape          = shape
        self.cycle_check    = 0
        self.cycle_number   = 0
        self.pointer_joints = {}
        self.naubs_joints   = {}
        self.__naubino      = None
        self.name           = name or id(self)
        self.naubino        = naubino
Example #18
0
 def __init__(self, pos, rot, child_unit):
     self.unit = child_unit
     self.obj_id = child_unit.obj_id
     inertia = pymunk.moment_for_circle(
         self.unit.mass, 0, self.unit.radius,(0,0)
     )
     
     self.body = pymunk.Body(self.unit.mass, inertia)
     self.unit.set_offset(0,0)
     self.unit.local_angle = 0
     self.unit.local_angle_target = 0
     self.unit.rotation = 0
     self.unit.initialize(self,collision_type=physics.FREE)
     
     self.body.angle = math.radians(rot)
     self.body.position = pos
     self.angle_degrees = rot
     
     self.attachable = True
     self.visible = True
     
     physics.body_update_list.append(self)
     physics.unit_update_list.append(self.unit)
     physics.space.add(self.body)
     self.unit.add_shapes()
    def __init__(self, pos, age, img_path, batch, group, start_impulse=(0, 0), *args, **kwargs):

        self.age = age
        self.batch = batch

        self.start_impulse = Vec2d(start_impulse)

        self.sprite = loaders.spriteloader(
            img_path, pos=pos, anchor=("center", "center"), batch=batch, group=group, linear_interpolation=True
        )
        self.sprite.visible = False

        self.mass = 0.00001
        self.radius = self.sprite.image.width / 2
        inertia = pymunk.moment_for_circle(self.mass, 0, self.radius)
        self.body = pymunk.Body(self.mass, pymunk.inf)
        self.body.position = Vec2d(pos)
        self.shape = pymunk.Circle(self.body, self.radius)
        self.shape.elasticity = 0.5
        self.shape.friction = 0.1
        self.shape.group = 1

        self.body.apply_impulse(self.start_impulse)
        self.tweener = PiTweener.Tweener()
        self.tweener.add_tween(self, age=0, tween_time=age, tween_type=self.tweener.LINEAR)
        self.tween = False
        self.removed = False
Example #20
0
 def __init__(self, space, x, y):
     inertia = pymunk.moment_for_circle(Raindrop.MASS, 0, Raindrop.RADIUS)
     self.body = pymunk.Body(Raindrop.MASS, inertia)
     self.body.position = x, y
     shape = pymunk.Circle(self.body, Raindrop.RADIUS)
     shape.friction = 0.5
     space.add(self.body, shape)
Example #21
0
    def spawn_player(self, point):
        logger.info("Spawning player...")
        self.player = Mage(
            self, window=self.window,
            x=point[0], y=point[1]
        )

        # Player body for physics
        inertia = pymunk.moment_for_circle(10, 0, 12, (0, 0))
        body = pymunk.Body(10, inertia)
        body.position = (self.player.x, self.player.y)
        shape = pymunk.Circle(body, 12, (0, 0))
        shape.elasticity = 0.2
        # shape.collision_type = 1
        shape.group = 0
        self.space.add(body, shape)
        self.player.body = body

        self.window.set_offset(
            self.player.windowpos[0] - self.player.x,
            self.player.windowpos[1] - self.player.y,

        )
        self.window.update_display_dungeon()

        logger.info("Done.")
Example #22
0
 def _createPhysicsObject(self):
     """Return a new physics object"""
     if self.geometry_type == 'circle':
         inertia = pymunk.moment_for_circle(self.mass, 0, self.radius, (0,0))
     else:
         inertia = pymunk.moment_for_box(self.mass, self.width, self.height)
     #
     body = pymunk.Body(self.mass, inertia)
     body.velocity = self.velocity
     body.force = self.force
     #
     if self.geometry_type == 'circle':
         shape = pymunk.Circle(body, self.radius, (0,0))
     else:
         #shape = pymunk.Poly(body, [(0, 0), (self.width, 0), 
         #                           (self.width, self.height), (0, self.height)])
         w2, h2 = self.width/2, self.height/2
         shape = pymunk.Poly(body, [(-w2,-h2), (+w2, -h2), (+w2, +h2), (-w2, +h2)])
     #
     shape.elasticity = self.elasticity
     shape.collision_type = 2
     shape.group = self.group
     shape.layers = self.layers
     shape.friction = self.friction
     self.shape = shape
     self.body = body
Example #23
0
 def __init__(self, game, pos, bonus_type=0):
     self._game = game
     self.is_alive = True
     self._life = constants.ENEMY_ARMOR
     self.bonus_type = bonus_type
     # Mouse pointer 'body'
     self.aim = pymunk.Body(1, 1)
     self.aim_shape = pymunk.Circle(self.aim, 1, (0, 0))
     self.aim_shape.layers = 0b000  # The 'aim' should not collide with any objects
     self.aim.position = pos
     # Enemy body
     mass = 3
     radius = 15
     inertia = pymunk.moment_for_circle(mass, 0, radius, (0, 0))
     self.body = pymunk.Body(mass, inertia)
     shape = pymunk.Circle(self.body, radius, (0, 0))
     shape.elasticity = 0.9
     shape.friction = 0.8
     shape.collision_type = 3
     self.shape = shape
     if not bonus_type:
         self.sprite = Sprite("enemy.png")
     elif bonus_type == constants.HEALTH_BONUS_TYPE:
         animation = pyglet.resource.animation("bonus_enemy_green.gif")
         self.sprite = Sprite(animation)
     elif bonus_type == constants.KILLALL_BONUS_TYPE:
         animation = pyglet.resource.animation("bonus_enemy_yellow.gif")
         self.sprite = Sprite(animation)
     self.sprite.do(Repeat(RotateBy(360, 2)))
     self.body.position = self.sprite.position = pos
     self.body.apply_force(-(self.body.mass + self.aim.mass) * game.space.gravity)
     # Connect aim and body with a DampedSpring - this should create the effect of flying through the air to the
     # player
     self.move = pymunk.constraint.DampedSpring(self.aim, self.body, (0, 0), (0, 0), 1, 600.0, 100)
     game.space.add(self.body, self.shape, self.aim, self.move)
Example #24
0
 def _constructPhysics(self, mass, pos):
     """Called by parent constructor.
     """
     self.mass = mass
     self.inertia = pymunk.moment_for_circle(mass, 0, self.radius, (0, 0))
     self.body = pymunk.Body(mass, self.inertia)
     self.shape = pymunk.Circle(self.body, self.radius, (0, 0))
Example #25
0
    def __init__(self, bod_x, bod_y, space):
        
        linelength = 50 #can make this a parameter 
        self.rotation_center_body = pm.Body(pm.inf, pm.inf) # rigid center of rotation
        self.rotation_center_body.position = (bod_x,bod_y)
            
        self.body = pm.Body(60, 5000) # this will be the body for the line
        self.body.position = (bod_x,bod_y)    
            
        self.l1 = pm.Segment(self.body, (0, 0), (0.0, -linelength), 5.0)
        
        #create the ball
        mass = 10
        radius = 20 
        inertia = pm.moment_for_circle(mass, 0, radius, (0,0))
        self.ballBody = pm.Body(mass, inertia)
        self.ballBody.position = (bod_x, bod_y - (linelength + radius) )
        
        self.circle = pm.Circle(self.ballBody, radius, (0,0))
        self.circle.elasticity = 1
   
        #connect the line to the center of rotation and then the ball to the line
        self.rotation_center_joint = pm.PinJoint(self.body, self.rotation_center_body, (0,0), (0,0))
        self.rotation_center_joint2 = pm.PinJoint(self.body, self.ballBody, (0,-linelength), (0,0))    


        space.add(self.l1, self.circle, self.ballBody, self.body, self.rotation_center_joint, self.rotation_center_joint2)
     
            
Example #26
0
def add_ball(space, add=True):
    """Add a ball to the given space at a random position"""
    radius = 15 if add else 3
    mass = 10 if add else pymunk.inf
    inertia = pymunk.moment_for_circle(mass, 0, radius, (20,0)) + pymunk.moment_for_circle(mass, 0, radius, (-20,0))
    print inertia
    body = pymunk.Body(mass, inertia)

    body.position = random.randint(120,130),random.randint(620,680)
    shape1 = pymunk.Circle(body, radius, (20,0))
    shape2 = pymunk.Circle(body, radius, (-20,5))
    space.add(body, shape1, shape2)
    shape1.collision_type = 2
    shape1.elasticity = 0.9
    shape2.collision_type = 2
    shape2.elasticity = 0.9
    return shape1, shape2
Example #27
0
 def addCircle(self, actorID, **kwargs):
     '''Create a circle shape and body'''
     if kwargs['moment'] == -1:
         kwargs['moment'] = pymunk.moment_for_circle(kwargs['mass'], 0, kwargs['size'])
         
     body = self.createBody(kwargs['isStatic'], kwargs['mass'], kwargs['moment'], kwargs['pos'])
     shape = pymunk.Circle(body, kwargs['size'])
     self.addShape(actorID, body, shape, kwargs['elasticity'], kwargs['collisionType'])
Example #28
0
 def __init__(self, space, radius, mass=None, position=None, velocity=None):
     super(RoundBody, self).__init__(space, mass, position, velocity)
     self.radius = radius
     inertia = pymunk.moment_for_circle(self.mass, 0, self.radius)
     self.game_body = pymunk.Body(self.mass, inertia)
     self.game_body.position = self.position
     self.game_shape = pymunk.Circle(self.game_body, self.radius)
     self.space.game_space.add(self.game_body, self.game_shape)
Example #29
0
def create_circle(collision_type=None, elasticity=None, friction=None,
                  mass=None, moment=None, offset=None, position=None, 
                  radius=None, **extras):
  if _lacks_moment(mass, moment):
    moment = pymunk.moment_for_circle(mass, 0, radius, offset)
  body = _create_body(mass, moment, position)
  shape = pymunk.Circle(body, radius, offset)
  return _configure_shape(shape, elasticity, friction, collision_type)
Example #30
0
 def insert_space_object(self, me_obj, mass=10, radius=10):
     moment = pm.moment_for_circle(mass, 0, radius, (0,0))
     body = pm.Body(mass, moment)
     body.position = me_obj.loc.x, me_obj.loc.y
     shape = pm.Circle(body, radius, (0,0))
     shape.friction = 0.7
     shape.collision_type = COLLTYPE_BALL
     self.space.add(body, shape)
     self.objects.update({me_obj.id: {"body": body, "me_obj": me_obj, "shape": shape}})
Example #31
0
def addball():
    radius = 20
    mass = 1
    inertia = pymunk.moment_for_circle(mass, 20, radius, (0, 0))
    ball_body = pymunk.Body(mass, inertia)
    ball_body.position = (WINW / 2, WINH / 2)

    mainball = pymunk.Circle(ball_body, radius, (0, 0))
    mainball.elasticity = 0.95
    space.add(ball_body, mainball)
    balls.append(mainball)
Example #32
0
 def circle_body(self, mass, radius, elasticity=0.8, friction=1.0):
     # Helper for when you really don't care what shape the thing is.
     angular_mass = pymunk.moment_for_circle(mass, 0, radius)
     self.body = pymunk.Body(mass, angular_mass)
     self.body.position = (self.x, self.y)
     self.shapes = {
         'body': pymunk.Circle(self.body, radius),
     }
     self.shapes['body'].elasticity = elasticity
     self.shapes['body'].friction = friction
     self.shapes['body'].collision_type = self.collision_type
Example #33
0
def on_mouse_press(x, y, button, modifiers):
    ######## Circle ##############
    circle_moment = pymunk.moment_for_circle(mass, 0, radius)
    circle_body = pymunk.Body(mass, circle_moment)
    circle_body.position = x, y
    circle_shape = pymunk.Circle(circle_body, radius)
    ## Set Elasticity ##
    circle_shape.elasticity = 0.8  # By using this the object can jump on the surface
    circle_shape.friction = 1.0  # By using this we can rotate the object

    space.add(circle_body, circle_shape)
 def create_cat(self):
     inertia = pymunk.moment_for_circle(1, 0, 14, (0, 0))
     self.cat_body = pymunk.Body(1, inertia)
     self.cat_body.position = 50, height - 100
     self.cat_shape = pymunk.Circle(self.cat_body, 30)
     self.cat_shape.color = THECOLORS["orange"]
     self.cat_shape.elasticity = 1.0
     self.cat_shape.angle = 0.5
     direction = Vec2d(1, 0).rotated(self.cat_body.angle)
     self.space.add(self.cat_body, self.cat_shape)
     return self.cat_body
Example #35
0
 def __init__(self, universe):
     mass = 1
     radius = 14
     inertia = pymunk.moment_for_circle(mass, 0, radius, (0, 0))
     self.body = body = pymunk.Body(mass, inertia) 
     body.position = 65, 550
     body.friction = 0.6
     self.shape = shape = pymunk.Circle(body, radius)
     shape.collision_type = 2
     shape.color = THECOLORS['blue']
     universe.space.add(body, shape)
 def __init__(self, x, y, r, *args, **kwargs):
     mass = 10
     # Create a Body with mass and moment
     self.body = pymunk.Body(
         mass, pymunk.moment_for_circle(mass, 0, r, (0, 0)))
     self.body.position = x, y
     # Create a box shape and attach to body
     self.shape = pymunk.Circle(self.body, r, offset=(0, 0))
     self.shape.elasticity = 0.99999
     self.shape.friction = 0.8
     self.gui_circle_figure = None
Example #37
0
def add_ball(space):
    mass = 1
    radius = 14
    moment = pymunk.moment_for_circle(mass, 0, radius)
    body = pymunk.Body(mass, moment)
    x = random.randint(120, 380)
    body.position = x, 550
    shape = pymunk.Circle(body, radius)
    shape.friction = 1.0
    space.add(body, shape)
    return shape
 def createObservedBall(self, radius, mass, x, y, elasticity, impulseX,
                        impulseY):
     inertia = pymunk.moment_for_circle(mass, 0, radius, (0, 0))
     body = pymunk.Body(mass, inertia)
     body.position = x, y
     body.apply_impulse_at_local_point((impulseX, impulseY))
     shape = pymunk.Circle(body, radius, (0, 0))
     shape.elasticity = elasticity
     self.space.add(body, shape)
     self.trackedShapes.append(shape)
     self.shapeCounter = self.shapeCounter + 1
Example #39
0
    def create_ball(self, point, mass=1.0, radius=15.0):

        moment = pm.moment_for_circle(mass, 0.0, radius)
        ball_body = pm.Body(mass, moment)
        ball_body.position = Vec2d(point)

        ball_shape = pm.Circle(ball_body, radius)
        ball_shape.friction = 1.5
        ball_shape.collision_type = COLLTYPE_DEFAULT
        self.space.add(ball_body, ball_shape)
        return ball_shape
Example #40
0
def add_ball(space):
    mass = 50
    radius = 14
    inertia = pymunk.moment_for_circle(mass, 0, radius, (0,0)) # 1
    body = pymunk.Body(mass, inertia) # 2
    x = random.randint(120,380)
    body.position = x, 550 # 3
    shape = pymunk.Circle(body, radius, (0,0)) # 4
    shape.elasticity = .95 
    space.add(body, shape) # 5
    return shape
Example #41
0
def create_circle(position):
    mass = 1
    inertia = pymunk.moment_for_circle(mass, 0, rad)
    body = pymunk.Body(mass, inertia)
    body.position = position
    shape = pymunk.Circle(body, rad)
    shape.elasticity = ball_elasticity
    shape.friction = friction
    space.add(body, shape)

    return shape
def add_ball(space):
    """Add a ball to the given space at a random position"""
    mass = 1
    radius = 14
    inertia = pymunk.moment_for_circle(mass, 0, radius, (0,0))
    body = pymunk.Body(mass, inertia)
    x = random.randint(120,380)
    body.position = x, 550
    shape = pymunk.Circle(body, radius, (0,0))
    space.add(body, shape)
    return shape
Example #43
0
def add_vehicle(space):
    mass = 10
    radius = 14
    moment = pymunk.moment_for_circle(mass, 0, radius)
    body = pymunk.Body(mass, moment)
    body.position = random.randint(0, WIDTH), random.randint(0, HEIGHT)
    body.velocity = (-20 + random.randint(0, 40) * 10,
                     -20 + random.randint(0, 40) * 10)
    shape = pymunk.Circle(body, radius)
    space.add(body, shape)
    return shape
Example #44
0
def create_ball(mass, radius=25):
    global balls
    inertia = pymunk.moment_for_circle(mass, 0, radius, (0, 0))
    body = pymunk.Body(mass, inertia)
    x = random.randint(115, 350)
    body.position = x, 400
    shape = pymunk.Circle(body, radius, (0, 0))
    shape.elasticity = 0.95
    shape.friction = 0.9
    space.add(body, shape)
    balls.append(shape)
Example #45
0
 def create_cat(self):
     # Create a lighter body in PyMunk to represent a fast moving cat
     inertia = pymunk.moment_for_circle(1, 0, 14 * self.game.scale, (0, 0))
     self.cat_body = pymunk.Body(1, inertia)
     self.cat_body.position = 50 * self.game.scale, self.game.height - 100 * self.game.scale
     self.cat_shape = pymunk.Circle(self.cat_body, 30 * self.game.scale)
     self.cat_shape.color = THECOLORS["orange"]
     self.cat_shape.elasticity = 1.0
     self.cat_shape.angle = 0.5
     direction = Vec2d(1, 0).rotated(self.cat_body.angle)
     self.space.add(self.cat_body, self.cat_shape)
Example #46
0
 def create_car(self, x, y, r):
     inertia = pymunk.moment_for_circle(1, 0, 14, (0, 0))
     self.car_body = pymunk.Body(1, inertia)
     self.car_body.position = x, y
     self.car_shape = pymunk.Circle(self.car_body, 25)
     self.car_shape.color = THECOLORS["green"]
     self.car_shape.elasticity = 1.0
     self.car_body.angle = r
     driving_direction = Vec2d(1, 0).rotated(self.car_body.angle)
     self.car_body.apply_impulse(driving_direction)
     self.space.add(self.car_body, self.car_shape)
Example #47
0
 def create_cat(self):
     inertia = pymunk.moment_for_circle(1, 0, 14, (0, 0))
     self.cat_body = pymunk.Body(pymunk.inf, pymunk.inf)
     #self.cat_body.position = init_x+100, init_y-100
     self.cat_body.position = init_x+200, init_y+500
     self.cat_shape = pymunk.Circle(self.cat_body, 30)
     self.cat_shape.color = THECOLORS["orange"]
     self.cat_shape.elasticity = 1.0
     self.cat_shape.angle = 0.5
     direction = Vec2d(1, 0).rotated(self.cat_body.angle)
     self.space.add(self.cat_body, self.cat_shape)
Example #48
0
    def create_ball(self, point, mass=4.0, radius=BALL_DEFAULT_RADIUS):

        moment = pm.moment_for_circle(mass, 0.0, radius)
        ball_body = pm.Body(mass, moment)
        ball_body.position = Vec2d(point)

        ball_shape = pm.Circle(ball_body, radius)
        ball_shape.friction = .5
        ball_shape.elasticity = .9999
        ball_shape.collision_type = COLLTYPE_DEFAULT
        self.space.add(ball_body, ball_shape)
        return ball_shape
Example #49
0
def create_primate(space):
    mass = 1
    radius = 25
    inertia = pymunk.moment_for_circle(mass, 0, radius, (0, 0))
    body = pymunk.Body(mass, inertia)
    x = random.randint(0, 500)
    body.position = 10, 10
    primate = pymunk.Circle(body, radius, (0, 0))
    primate.elasticity = 0.95
    primate.friction = 25
    space.add(body, primate)
    return primate
Example #50
0
    def create_fingertip(self, point, mass=1000.0, radius=TIP_RADIUS):
        moment = pm.moment_for_circle(mass, 0.0, radius)
        fingertip_body = pm.Body(mass, moment)
        fingertip_body.position = Vec2d(point)
        fingertip_body.velocity_func = maintain_velocity

        fingertip_shape = pm.Circle(fingertip_body, radius)
        fingertip_shape.friction = 20
        fingertip_shape.collision_type = COLLTYPE_FINGERTIP
        fingertip_shape.elasticity = .9999
        self.space.add(fingertip_body, fingertip_shape)
        return fingertip_shape
Example #51
0
    def create_palm(self, point, radius):
        moment = pm.moment_for_circle(10000, 0.0, radius)
        palm_body = pm.Body(10000, moment)
        palm_body.position = Vec2d(point)
        palm_body.velocity_func = maintain_velocity

        palm_shape = pm.Circle(palm_body, radius)
        palm_shape.friction = 20
        palm_shape.collision_type = COLLTYPE_FINGERTIP
        palm_shape.elasticity = .5
        self.space.add(palm_body, palm_shape)
        return palm_shape
Example #52
0
    def load_test_ball(self):
        mass = 1
        radius = 14
        inertia = pymunk.moment_for_circle(mass, 0, radius, (0, 0))
        body = pymunk.Body(mass, inertia)
        x = random.randint(120, 380)
        body.position = (x, 550)
        shape = pymunk.Circle(body, radius, (0, 0))

        self.test_body = body

        self.space.add(body, shape)
Example #53
0
    def __init__(self, x, y, **kwargs):
        self.mass = 10
        self.radius = BASE_RADIUS * SCALE_FACTOR
        self.inertia = pymunk.moment_for_circle(self.mass, 0, self.radius,
                                                (0, 0))

        self.body = pymunk.Body(self.mass, self.inertia)
        self.body.position = x, y

        self.shape = pymunk.Circle(self.body, self.radius, (0, 0))
        self.shape.elasticity = 0.95
        self.shape.friction = 0.9
Example #54
0
 def __init__(self, x, y, radius, space):
     mass = 5
     inertia = pm.moment_for_circle(mass, 0, radius, (0, 0))
     body = pm.Body(mass, inertia)
     body.position = x, y
     shape = pm.Circle(body, radius, (0, 0))
     shape.elasticity = 0.95
     shape.friction = 1
     shape.collision_type = COLLISION_PIG
     space.add(body, shape)
     self.body = body
     self.shape = shape
Example #55
0
 def create_ped(self, pos):
     pedMass = 1
     pedRadius = 0.25 * multi
     pedMoment = pymunk.moment_for_circle(pedMass, 0, pedRadius, (0, 0))
     self.pedBody = pymunk.Body(pedMass, pedRadius)
     self.pedBody.position = pos
     self.pedShape = pymunk.Circle(self.pedBody, pedRadius)
     self.pedShape.color = THECOLORS["white"]
     self.pedShape.elasticity = 1.0
     self.pedShape.angle = 0
     direction = Vec2d(1, 0).rotated(self.pedBody.angle)
     self.space.add(self.pedBody, self.pedShape)
Example #56
0
    def testMomentHelpers(self):
        m = p.moment_for_circle(1, 2, 3, (1, 2))
        self.assertAlmostEqual(m, 11.5)

        m = p.moment_for_segment(1, (-10, 0), (10, 0), 1)
        self.assertAlmostEqual(m, 40.6666666666)

        m = p.moment_for_poly(1, [(0, 0), (10, 10), (10, 0)], (1, 2), 3)
        self.assertAlmostEqual(m, 98.3333333333)

        m = p.moment_for_box(1, (2, 3))
        self.assertAlmostEqual(m, 1.08333333333)
Example #57
0
    def make_circle(self, x, y):
        size = 20
        mass = 12.0
        moment = pymunk.moment_for_circle(mass, 0, size, (0, 0))
        body = pymunk.Body(mass, moment)
        body.position = pymunk.Vec2d(x, y)
        shape = pymunk.Circle(body, size, pymunk.Vec2d(0, 0))
        shape.friction = 0.3
        self.space.add(body, shape)

        sprite = CircleSprite(shape, "images/coin_01.png")
        self.sprite_list.append(sprite)
Example #58
0
def add_ball(space):
    """Add a ball to the given space at a random position"""
    mass = 5
    radius = 30
    inertia = pymunk.moment_for_circle(mass, 0, radius, (0, 0))
    body = pymunk.Body(mass, inertia)
    x = random.randint(60, 200)
    body.position = x, 930
    shape = pymunk.Circle(body, radius, (0, 0))
    shape.elasticity = 0.5
    space.add(body, shape)
    return shape
Example #59
0
    def create_body_shape(self):
        inertia = pymunk.moment_for_circle(self.mass, 0, self.radius, (0, 0))
        self.body = pymunk.Body(self.mass, inertia)
        self.body.position = self.x, self.y

        self.shape = pymunk.Circle(self.body, self.radius, (0, 0))
        self.shape.elasticity = .99
        self.shape.friction = 0.68
        self.shape.collision_type = 1

        self.shape.coin = self
        self.body.coin = self
Example #60
0
    def __init__(self, mass, radius, x, y, batch, image, space):

        self.mass = mass
        self.radius = radius

        self.image = pyglet.sprite.Sprite(image, batch=batch)

        inertia = pymunk.moment_for_circle(mass, 0, radius)
        self.body = body = pymunk.Body(mass, inertia)
        body.position = x, y
        self.shape = shape = pymunk.Circle(body, radius)
        space.add(body, shape)