def init_space(): sp = Space() # Cria quadrado L = 5 player = Body(mass=1, moment=100) shape = Poly(player, [(-L, -L), (L, -L), (L, L), (-L, L)]) player.position = (50, 40) player.velocity = (-25, 25) shape.elasticity = 1.0 shape.color = pyxel.COLOR_RED # Cria margens line = Body(body_type=Body.STATIC) lines = [ Segment(line, (-30, -30), (270, -30), 2), Segment(line, (-30, 210), (270, 210), 2), Segment(line, (-30, -30), (-30, 210), 2), Segment(line, (270, -30), (270, 210), 2), ] for line in lines: line.elasticity = 1.0 # Adiciona elementos ao espaço sp.add(player, shape, *lines) sp.player = player return sp
def __init__(self, space, rect, playfield=None): super(PlungerAssembly, self).__init__() self.chute_counter = 0 self.rect = Rect(0, 0, 1, 1) spring_strength = 100 * plunger_mass chute_opening = playfield.position + rect.center - (rect.width / 2. - ball_radius * 4, 0) plunger_rect = Rect(0, 0, rect.width * .2, ball_radius / 2.) anchor0 = chute_opening - playfield.position - (ball_radius * 3., 0) anchor1 = anchor0 + (rect.width * .8, 0) anchor2 = -plunger_rect.width / 2., 0 plunger_body = Body(plunger_mass, pymunk.inf) plunger_shape = Poly.create_box(plunger_body, plunger_rect.size) plunger_shape.layers = 1 plunger_shape.friction = 0.1 plunger_shape.elasticity = 1.0 plunger_shape.collision_type = plunger_type plunger_body.position = chute_opening + (plunger_rect.width / 2., 0) j0 = GrooveJoint(playfield, plunger_body, anchor0, anchor1, anchor2) j1 = DampedSpring(playfield, plunger_body, anchor0, anchor2, 0, spring_strength, 5) s0 = Circle(Body(), ball_radius / 2.) s0.layers = 1 s0.sensor = True s0.collision_type = sensor0_type s0.body.position = chute_opening + (ball_radius * 4., 0.0) s1 = Circle(Body(), ball_radius * 3.) s1.layers = 1 s1.sensor = True s1.collision_type = sensor1_type s1.body.position = chute_opening def inc_counter(space, arbiter): self.chute_counter += 1 return True def dec_counter(space, arbiter): self.chute_counter -= 1 f = space.add_collision_handler f(sensor1_type, plunger_type, begin=inc_counter, separate=dec_counter) self.playfield = playfield self.plunger_offset = playfield.position - plunger_body.position + ( ball_radius * 3, 0) self.spring = j1 self.spring_length = rect.width / 2. self.spring_strength = spring_strength self.plunger_body = plunger_body self.ball_chute = Rect(0, 0, ball_radius * 2., ball_radius * 2.) self.ball_chute.center = chute_opening self._original_image = pygame.Surface(plunger_rect.size) self._original_image.fill((192, 255, 255)) self.shapes = [plunger_shape, s0, s1, j0, j1] self.visible = 0
def load(self, obj_info: 'MutableMapping[str, Any]', space: 'pymunk.Space', prefixes: 'Sequence[str]' = ()) -> 'ObjectInfo': config_content = obj_info.get('Config', {}) if config_content is None: is_static = False else: is_static = config_content.get('static', False) shapes = self.__shape_loader.load(obj_info['Shape']) if is_static is True: body = Body(body_type=Body.STATIC) for shape in shapes: shape.body = body space.add(shapes) else: mass = sum(shape.mass for shape in shapes) moment = sum(shape.moment for shape in shapes) body = Body(mass, moment) for shape in shapes: shape.body = body space.add(body, shapes) return ObjectInfo(body, loadImages(obj_info.get('Image', ()), prefixes=prefixes))
def init_space(): sp = Space() sp.gravity = (0, 50) chain = make_pivot_chain(sp, (0, 0), (240, 30), 30) sp.add(constraint.PivotJoint(chain[0], sp.static_body, chain[0].position)) # Cria quadrado L = 25 player = Body(mass=1, moment=100) shape = Poly(player, [(-L, -L), (L, -L), (L, L), (-L, L)]) player.position = (90, 60) player.velocity = (-25, 25) shape.elasticity = 1.0 shape.color = pyxel.COLOR_RED shape.collision_type = 42 ball = Body(mass=1, moment=200) ball_shape = Circle(ball, 20) ball.position = (player.position.x, 130) ball_shape.elasticity = 1.0 shape.color = pyxel.COLOR_NAVY ball_shape.collision_type = 42 joint1 = constraint.DampedSpring(player, ball, (0, 0), (20, 0), 20, 3, 0.5) joint2 = constraint.PivotJoint(sp.static_body, player, (65, 35)) joint1.collide_bodies = False sp.add(joint1, joint2) body2 = Body(1, 100) sp.add(body2) sp.add(Poly(body2, [(-3, 3), (3, 3), (3, -3), (-3, -3)])) body2.position = 220, 50 sp.add(constraint.DampedRotarySpring(body2, ball, 0, 2, 1)) sp.body2 = body2 # Cria margens line = Body(body_type=Body.STATIC) e = 0 lines = [ Segment(line, (-e, -e), (240 + e, -e), 2), Segment(line, (-e, 180 + e), (240 + e, 180 + e), 2), Segment(line, (-e, -e), (-e, 180 + e), 2), Segment(line, (240 + e, -e), (240 + e, 180 + e), 2), ] for line in lines: line.elasticity = 1.0 lines = [] # Adiciona elementos ao espaço sp.add(player, shape, ball, ball_shape, *lines) sp.player = player #handler = sp.add_collision_handler(42, 42) #handler.begin = lambda *args: False return sp
def __init__(self): self.mass = 1 # 1 kg # e-puck: 0.037 meter radius, converted to pixels for display #self.radius = 3.7 * CM_TO_PIXELS # Bupimo: 9 cm radius, converted to pixels for display self.radius = 9 * CM_TO_PIXELS self.circular = False; if self.circular: rob_I = moment_for_circle(self.mass, 0, self.radius) self.body = Body(self.mass, rob_I) self.shape = Circle(self.body, self.radius) else: r = self.radius d = self.radius * 1.5 # Amount the wedge part pokes out. vertices = [(0, -r), (d, 0), (0, r)] #vertices = [(0, -r), # (d/4, 0), # (d/2, r)] # Now add the semicircular back part n = 5 angles = [pi/2 + i*(pi/n) for i in range(1, n)] for a in angles: vertices.append((r*cos(a), r*sin(a))) rob_I = moment_for_poly(self.mass, vertices) self.body = Body(self.mass, rob_I) self.shape = Poly(self.body, vertices) # EXPERIMENTAL: Add bristles to robot # rest_length = 100 # stiffness = 500 # damping = 10 # self.bristle_body = pymunk.DampedSpring(self.body, body, \ # (0,0), (0,0), rest_length, stiffness, damping) # self.sim.env.add(self.spring_body) self.body.position = 0, 0 self.body.angle = 0 self.body.velocity = 0, 0 self.body.angular_velocity = 0 self.shape.color = 0, 255, 0 self.shape.filter = ShapeFilter(categories = ROBOT_MASK) self.command = Twist()
def generate_pickles(space): for i in range(0, 10): pickle = Body(mass=1, moment=1) pickle_shape = Pickle(pickle, 6) pickle_shape.elasticity = 1 x = random.randrange(260, 800) y = random.randrange(0, 180) pickle.position = (x, y) pickle.velocity_func = zero_gravity v = random.randrange(-200, -70) pickle.velocity = (v, 0) # Set collison type for pickle pickle_shape.collision_type = 2 # Setup the collision callback function Cat and Pickle h = space.add_collision_handler(1, 2) h.begin = dead # Setup the collision callback function Pickle and Pickle p = space.add_collision_handler(2, 2) p.begin = same_elem_collision # Setup the collision callback function Pickle and Pickle under boost pb = space.add_collision_handler(2, 6) pb.begin = same_elem_collision space.add(pickle, pickle_shape)
def init_space(): sp = Space() player = Body(mass=1, moment=1) shape = Circle(player, 10) player.position = (20, 90) player.velocity = (5, 0) shape.color = pyxel.COLOR_YELLOW line = Body(body_type=Body.STATIC) line_shape = Segment(line, (0, 1), (240, 1), 2) line_shape.color = pyxel.COLOR_RED sp.add(player, shape, line, line_shape) sp.player = player return sp
def __init__(self, center, road, side): # setup shape mass = 90 radius = 2.5 inertia = moment_for_circle(mass, 0, radius * 2, (0, 0)) body = Body(mass, inertia) body.position = center self.shape = Circle(body, radius * 2, (0, 0)) self.shape.color = (0, 255, 255) self.shape.collision_type = CollisionType.Pedestrian self.shape.elasticity = 0.05 # Walk parameter self.lenRange = road.length self.widthRange = (road.nLanes + 1) * road.width * 2 self.side = side # Bool flags self.dead = False # Move parameters self.moving = 0 self.direction = road.direction self.normal = road.normal self.speed = random.randint(3, 6) # Flags for crossing self.crossing = False self.beginCrossing = False
def __init__(self, mask, radius): self.body = Body(0, 0, Body.STATIC) self.body.position = 0, 0 self.body.angle = 0 self.body.velocity = 0, 0 self.body.angular_velocity = 0 self.shape = Circle(self.body, radius) self.mask = mask self.shape.filter = ShapeFilter(categories=mask) if mask == ARC_LANDMARK_MASK: self.shape.color = 0, 255, 0 elif mask == POLE_LANDMARK_MASK: self.shape.color = 0, 0, 255 elif mask == BLAST_LANDMARK_MASK: self.shape.color = 255, 0, 0 else: sys.exit("Unknown landmark mask: " + str(mask)) # The following is just to set the appropriate params to visualize below config = ConfigSingleton.get_instance() self.vis_range_max = \ config.getfloat("RangeScan:landmarks", "range_max") \ + radius self.vis_inside_radius = \ config.getfloat("LandmarkCircleController", "inside_radius") \ + radius self.vis_outside_radius = \ config.getfloat("LandmarkCircleController", "outside_radius") \ + radius
def __init__(self, mask): self.body = Body(0, 0, Body.STATIC) self.body.position = 0, 0 self.body.angle = 0 self.body.velocity = 0, 0 self.body.angular_velocity = 0 #self.radius = 9 * CM_TO_PIXELS self.radius = 6 * CM_TO_PIXELS self.shape = Circle(self.body, self.radius) self.mask = mask self.shape.filter = ShapeFilter(mask=ShapeFilter.ALL_MASKS ^ (ANY_PUCK_MASK | ROBOT_MASK), categories=mask) if mask == ARC_LANDMARK_MASK: self.shape.color = 255, 0, 255 elif mask == ENTER_ARC_LANDMARK_MASK: self.shape.color = 0, 255, 0 elif mask == EXIT_ARC_LANDMARK_MASK: self.shape.color = 255, 0, 0 elif mask == POLE_LANDMARK_MASK: self.shape.color = 0, 0, 255 elif mask == BLAST_LANDMARK_MASK: self.shape.color = 255, 255, 255 else: sys.exit("Unknown landmark mask: " + str(mask))
def generate_stars(space): for i in range(0, 5): star = Body(mass=0.00000001, moment=1) star_shape = Star(star, 5) x = random.randrange(400, 1600) y = random.randrange(0, 180) star.position = (x, y) star.velocity_func = zero_gravity v = random.randrange(-90, -50) star.velocity = (v, 0) star_shape.collision_type = 3 # Setup the collision callback function Cat and Star g = space.add_collision_handler(1, 3) g.begin = boost # Setup the collision callback function Star and Star s = space.add_collision_handler(3, 3) s.begin = same_elem_collision # Setup the collision callback function Star and Pickle s = space.add_collision_handler(2, 3) s.begin = same_elem_collision # Setup the collision callback function Star and Pickle under boost pb = space.add_collision_handler(3, 6) pb.begin = same_elem_collision space.add(star, star_shape)
def add_segment(self, x1, y1, x2, y2, thickness=1, dynamic=True): body = Body(body_type=(Body.STATIC, Body.DYNAMIC)[int(dynamic)]) seg = Segment(body, (x1, y1), (x2, y2), radius=thickness) seg.density = Environment.DEFAULT_DENSITY self.space.add(body, seg) self.bodies.append(body) return body
def add_circle(self, x, y, r, dynamic=True): body = Body(body_type=(Body.STATIC, Body.DYNAMIC)[int(dynamic)]) body.position = x, y circle = Circle(body, r) circle.density = Environment.DEFAULT_DENSITY self.space.add(body, circle) self.bodies.append(body) return body
def add_rect(self, x, y, w, h, dynamic=True): body = Body(body_type=(Body.STATIC, Body.DYNAMIC)[int(dynamic)]) body.position = x, y poly = Poly.create_box(body, size=(w, h)) poly.density = Environment.DEFAULT_DENSITY self.space.add(body, poly) self.bodies.append(body) return body
def add_polygon(self, x, y, *vertices, dynamic=True): body = Body(body_type=(Body.STATIC, Body.DYNAMIC)[int(dynamic)]) body.position = x, y poly = Poly(body, vertices) poly.density = Environment.DEFAULT_DENSITY self.space.add(body, poly) self.bodies.append(body) return body
def __init__(self, x, y, side, dir): body = Body(0, 0, Body.STATIC) body.position = x, y self.side = side self.dir = dir self.shape = Circle(body, self.radius * 2, (0, 0)) self.shape.color = (0, 0, 255, 0) self.shape.elasticity = 0.95 self.shape.collision_type = CollisionType.Goalpost
def create_body(self): verts = map(Vec2d, self.get_verts()) self.body = Body(self.mass, moment_for_poly(self.mass, verts)) self.body.position = (self.x, self.y) self.shape = Poly(self.body, verts, (0, 0)) self.shape.layers = self.layers # this is so you can get to it from collision handlers. # eg. arbiter.shapes[0].parent self.shape.parent = self
def __init__(self, kind, immobile=False): self.immobile = immobile self.mass = 0.1 # 0.1 kg # 0.1 meter radius, converted to pixels for display #self.radius = 0.1 * M_TO_PIXELS # Hockey puck radius = 23mm = 0.023 self.radius = 0.023 * M_TO_PIXELS # Plate puck radius = 12.8cm = 0.128 #self.radius = 0.128 * M_TO_PIXELS # moment of inertia for disk moment_inertia = moment_for_circle(self.mass, 0, self.radius) if not immobile: self.body = Body(self.mass, moment_inertia) else: self.body = Body(0, 0, Body.STATIC) self.body.position = 0, 0 self.body.angle = 0 self.body.velocity = 0, 0 self.body.angular_velocity = 0 self.shape = Circle(self.body, self.radius) self.kind = kind if kind == 0: self.shape.color = 200, 100, 100 self.shape.filter = ShapeFilter(categories=RED_PUCK_MASK) elif kind == 1: self.shape.color = 100, 200, 100 self.shape.filter = ShapeFilter(categories=GREEN_PUCK_MASK) elif kind == 2: self.shape.color = 100, 100, 200 self.shape.filter = ShapeFilter(categories=BLUE_PUCK_MASK) else: sys.exit("Unknown puck kind: " + kind) if immobile: self.shape.color = self.shape.color[0] / 3, self.shape.color[ 1] / 3, self.shape.color[2] / 3
def __init__(self): # We'll have a body, just to have a way of representing its position # and treating it similarly to a robot (where convenient). self.body = Body(0, 0, Body.STATIC) self.body.position = 0, 0 self.body.angle = 0 self.body.velocity = 0, 0 self.body.angular_velocity = 0 self.radius = 0
def _create_poly(self) -> Circle: """ Create the polygon used for ray-casting. (Ultrasonic sensor) :return: a Pymunk Circle object. """ body = Body(body_type=Body.STATIC) body.position = Vec2d(self.center_x, self.center_y) return Circle(body, self.radius)
def _create_poly(self): """ Create the polygon used for ray-casting. (Ultrasonic sensor) :return: a Pymunk Poly object. """ body = Body(body_type=Body.STATIC) body.position = Vec2d(self.center_x, self.center_y) return Poly.create_box(body, (self.width, self.height))
def init_space(): sp = Space() sp.gravity = (0, 50) sp.damping = 1.0 floor = Body(body_type=Body.STATIC) stick = Body(mass=100, moment=100 * 50**2) L = 20 shapes = [ Poly(stick, [(-L, -L), (L, -L), (L, L), (0, L + L / 2), (-L, L)], radius=3), Segment(floor, (1, 179), (239, 179), 1), Segment(floor, (1, 1), (239, 1), 1), Segment(floor, (1, 1), (1, 179), 1), Segment(floor, (239, 1), (239, 179), 1), ] stick.position = (120, L) bodies = [] for _ in range(L): r = random.uniform(2, 6) mass = pi * r**2 body = Body(mass=mass, moment=mass * r**2 / 2) circle = Circle(body, r) x = random.uniform(r, 240 - r) y = random.uniform(r, 180 - r) body.position = (x, y) vx = random.uniform(-L, L) vy = random.uniform(-L, L) body.velocity = (vx, vy) bodies.append(body) shapes.append(circle) circle.color = random.randint(1, 15) for shape in shapes: shape.elasticity = 1.0 sp.add(floor, stick, *bodies, *shapes) return sp
def __init__( self, space: Space, shape_type: str, pos: Tuple[float, float], scale: float = 1., mass: float = 1, static: bool = False, friction: float = 1., color: Tuple[float, float, float] = (1., 1., 1.), ): self.space = space self.shape_type = shape_type self.static = static self._scale = scale self.color = color padding = 0.03 if not static: if self.shape_type == "box": moment = pymunk.moment_for_box(mass, (self.scale, self.scale)) elif self.shape_type == "circle": moment = pymunk.moment_for_circle(mass, 0, self.scale / 2.) else: raise ValueError(f"Invalid shape_type '{self.shape_type}'") self.mass = mass self.body = Body(mass, moment) self.body.position = pos self._static_position = None if self.shape_type == "box": self.shape = Poly.create_box(self.body, (1, 1)) elif self.shape_type == "circle": self.shape = Circle(self.body, self.scale / 2. - padding) # static else: self.mass = 0 self.body = self.space.static_body self._static_position = Vec2d(*pos) if self.shape_type == "box": self.shape = Poly( self.space.static_body, [(pos[0] + p[0] * scale, pos[1] + p[1] * scale) for p in SHAPE_TYPES[self.shape_type]["polygon"]]) self.shape.get_vertices() elif self.shape_type == "circle": self.shape = Circle(self.space.static_body, self.scale / 2. - padding, offset=pos) self.shape.friction = friction
def __init__(self, rect): super(Ball, self).__init__() radius = rect.width / 2 body = Body() body.position = rect.center self.shape = Circle(body, radius) self.shape.mass = ball_mass self.shape.elasticity = .25 self.shape.friction = 1 self.rect = Rect(0, 0, rect.width, rect.width) self.original_image = resources.gfx("yarnball.png", convert_alpha=True) self.pymunk_shapes = (body, self.shape)
def generate_cat(space): cat = Body(mass=1, moment=1) cat_shape = Cat(cat, 15) cat_shape.elasticity = 1 cat.position = (50, 120) cat_shape.collision_type = 1 # Setup the collision callback function Cat and Pickle under boost g = space.add_collision_handler(1, 6) g.begin = same_elem_collision space.add(cat, cat_shape)
def __init__(self, center, width, height): # Create body body = Body(0, 0, Body.STATIC) body.position = center points = [Vec2d(width,height),Vec2d(-width,height),-Vec2d(width,height),Vec2d(width,-height)] self.width = width self.height = height self.points = [p+center for p in points] self.shape = Poly(body,points) self.shape.color = (200, 200, 200) self.shape.elasticity = 0.05 self.shape.collision_type = CollisionType.Obstacle
def __init__(self, space, rect): super(Ball, self).__init__() radius = rect.width / 2 body = Body(ball_mass, moment_for_circle(ball_mass, 0, radius)) body.position = rect.center self.shape = Circle(body, radius) self.shape.elasticity = .5 self.shape.friction = 0 self.shape.layers = 1 self.shape.collision_type = ball_type self.rect = pygame.Rect(0, 0, rect.width, rect.width) image = smoothscale(prepare.GFX.get('ball-bearing'), self.rect.size) self._original_image = image.convert_alpha()
def create_body(self): verts = self.get_verts() self.body = Body(self.mass, moment_for_poly(self.mass, verts)) self.body.position = self.branch.tip() self.shape = Poly(self.body, verts) # platforms should only collide with other platforms and woger self.shape.layers = self.layers self.shape.group = self.group self.shape.collision_type = CollisionType.BOUGH self.shape.parent = self
def __init__(self): self.mass = 1 # 1 kg # 0.1 meter radius, converted to pixels for display #self.radius = 0.05 * M_TO_PIXELS # Bupimo: 0.111 meter radius, converted to pixels for display self.radius = 0.111 * M_TO_PIXELS # moment of inertia for disk rob_I = moment_for_circle(self.mass, 0, self.radius) self.body = Body(self.mass, rob_I) self.body.position = 0, 0 self.body.angle = 0 self.body.velocity = 0, 0 self.body.angular_velocity = 0 """ self.shape = Circle(self.body, self.radius) self.shape.color = 127, 0, 255 # a pinkish blue self.shape.filter = ShapeFilter(categories = ROBOT_MASK) """ """ r = self.radius p = self.radius / 2.0 # Amount the wedge part pokes out. vertices = [(r+p, r), (-r/3, r), (-r, 0), (-r/3, -r), (r/3, -r) ] """ r = self.radius d = self.radius * 1.5 # Amount the wedge part pokes out. vertices = [(0, -r), (d, 0), (0, r)] # Now add the semicircular back part n = 3 angles = [pi/2 + i*(pi/n) for i in range(1, n)] for a in angles: vertices.append((r*cos(a), r*sin(a))) vertices = vertices[::-1] self.shape = Poly(self.body, vertices) self.shape.color = 127, 0, 255 # a pinkish blue self.shape.filter = ShapeFilter(categories = ROBOT_MASK) self.command = Twist()
def handle_objectgroup(layer): body = Body() for thing in layer['objects']: for kind in supported_shapes.intersection(thing.keys()): f = get_handler('handle_{}'.format(kind)) if f: for i in f(thing, body): yield i f = get_handler('handle_object_type_{}'.format(thing['type'])) if f: for i in f(thing, body): yield i