def __init__(self, colour=(1,1,1), vertices=None, density=None, friction=None, restitution=None): super(Polygon, self).__init__(shape=b2PolygonDef(), colour=colour, density=density, friction=friction, restitution=restitution) if vertices is not None: self.shape.setVertices(vertices)
def setup_world(): world_bounds = b2AABB() world_bounds.lowerBound = (-200, -1000) world_bounds.upperBound = (200, 200) world = b2World( world_bounds, b2Vec2(0, -30), # Gravity vector True # Use "sleep" optimisation ) wallsdef = b2BodyDef() walls = world.CreateBody(wallsdef) walls.userData = 'Blocks' WALLS = [ (W, FLOOR * 0.5, (W / 2, FLOOR * 0.5), 0), # floor (W / 2, 1, (W / 2, H + 1), 0), # ceiling (1, 600, (-1, -500), 0), # left wall (1, 600, (W + 1, -500), 0), # right wall ] for wall in WALLS: shape = b2PolygonDef() shape.SetAsBox(*wall) walls.CreateShape(shape) return world
def append_to_world(self, world): width, height = self._size x, y = self._position box = b2PolygonDef() hitbox_width = (self._hitbox.left * width / 100) * 2 hitbox_height = (self._hitbox.top * height / 100) * 2 box.SetAsBox((width / 2) - hitbox_width, (height / 2) - hitbox_height) body_def = b2BodyDef() body_def.position.Set(x, y) self._body = world.CreateBody(body_def) self._body.CreateShape(box)
def box_def(w, h, center=(0, 0), angle=0, density=1, restitution=0.1, friction=2): s = b2PolygonDef() s.SetAsBox(w * 0.5, h * 0.5, b2Vec2(*center), angle) s.density = density s.restitution = restitution s.friction = friction return s
def create_body(self, pos): bodydef = b2BodyDef() bodydef.position = b2Vec2(*pos) body = world.CreateBody(bodydef) shape = b2PolygonDef() shape.SetAsBox(self.W * 0.5, self.H * 0.5, (0, 0), 0) shape.density = 0.5 shape.restitution = 0.1 shape.friction = 0.5 body.CreateShape(shape) body.SetMassFromShapes() self.body = body
def setup_bear(): bodydef = b2BodyDef() bodydef.position = (W * 0.66, H * 0.7) body = world.CreateBody(bodydef) shape = b2PolygonDef() shape.SetAsBox(BOX_SIZE, BOX_SIZE, (0, 0), 0) shape.density = 0.3 shape.restitution = 0.5 shape.friction = 0.5 body.CreateShape(shape) body.SetMassFromShapes() return body
def create_block(pos): bodydef = b2BodyDef() bodydef.position = pos body = world.CreateBody(bodydef) shape = b2PolygonDef() shape.SetAsBox(BOX_SIZE, BOX_SIZE, (0, 0), 0) shape.density = 0.1 shape.restitution = 0.2 shape.friction = 0.5 body.CreateShape(shape) body.SetMassFromShapes() return body
def append_to_world(self, world): self.world = world width, height = self._size x, y = self._position box = b2PolygonDef() hitbox_width = (self._hitbox.left * width / 100) * 2 hitbox_height = (self._hitbox.top * height / 100) * 2 box.SetAsBox((width / 2) - hitbox_width, (height / 2) - hitbox_height) box.density = self._density box.friction = 0 box.angle = 0 box.restitution = 0 body_def = b2BodyDef() body_def.fixedRotation = True body_def.position.Set(x, y) self._body = world.CreateBody(body_def) self._body.CreateShape(box) self._body.SetMassFromShapes()
def create_body(self, pos): x, y = pos self.lend = self.create_left_end(pos) self.segments = [] self.ends = [] if self.lend: prevBody = self.lend.body self.ends.append(self.lend) else: prevBody = None for i in range(self.length): sd = b2PolygonDef() sd.SetAsBox(self.PIECE_LENGTH, self.radius) sd.density = self.density sd.friction = 1 sd.restitution = 0 sd.filter.groupIndex = -1 bd = b2BodyDef() bd.linearDamping = 0 bd.angularDamping = 0.2 bd.position = (x + self.PIECE_LENGTH * 2 * i - self.PIECE_LENGTH, y) body = world.CreateBody(bd) body.CreateShape(sd) body.SetMassFromShapes() self.segments.append(body) if prevBody: jd = b2RevoluteJointDef() anchor = (x + self.PIECE_LENGTH * (2 * i - 1), y) jd.Initialize(prevBody, body, anchor) world.CreateJoint(jd).getAsType() prevBody = body self.rend = self.create_right_end((x + self.PIECE_LENGTH * (self.length - 1) * 2, y)) if self.rend: jd = b2RevoluteJointDef() anchor = (x + self.PIECE_LENGTH * (self.length - 1) * 2, y) jd.Initialize(prevBody, self.rend.body, anchor) world.CreateJoint(jd).getAsType() self.ends.append(self.rend)
def read_shapes_from_svg(fname): """Dirty way to read polygons from a subset of SVG.""" doc = parse(fname) shapes = [] h = float(doc.getroot().get('height')) for p in doc.findall('.//{http://www.w3.org/2000/svg}path'): path = p.get('d') or '' coords = [] last = b2Vec2(0, h) for cmd in path.split(): if ',' in cmd: x, y = [float(c) for c in cmd.split(',')] c = b2Vec2(x, -y) + last last = c coords.append(c) shape = b2PolygonDef() shape.setVertices(tuple(coords)) shapes.append(shape) return shapes
def setup_world(): global buoyancy world_bounds = b2AABB() world_bounds.lowerBound = (-200, -100) world_bounds.upperBound = (1000, 1000) world = b2World( world_bounds, b2Vec2(0, -30), # Gravity vector True # Use "sleep" optimisation ) wallsdef = b2BodyDef() walls = world.CreateBody(wallsdef) walls.userData = 'Blocks' WALLS = [ #(W, FLOOR * 0.5, (W / 2, FLOOR * 0.5), 0), # floor #(W / 2, 1, (W / 2, H + 1), 0), # ceiling (1, 600, (-1, -500), 0), # left wall #(1, 600, (W + 1, -500), 0), # right wall ] for wall in WALLS: shape = b2PolygonDef() shape.SetAsBox(*wall) walls.CreateShape(shape) for shape in read_shapes_from_svg('shapes/ground.svg'): walls.CreateShape(shape) buoyancydef = b2BuoyancyControllerDef() buoyancydef.normal = b2Vec2(0, 1) buoyancydef.offset = WATER_LEVEL buoyancydef.density = 2.5 buoyancydef.angularDrag = 0.5 buoyancydef.linearDrag = 3 buoyancy = world.CreateController(buoyancydef) return world