def createShape(self, verticies): assert len(verticies) >= 2 verticies = self.verticies = map(Vec2d, verticies) if is_clockwise(verticies): # fix it if it is clockwise average = sum(verticies) / len(verticies) verticies.sort(key=lambda v: (average-v).get_angle()) assert not is_clockwise(verticies), 'OMG, my code is wrong! the poly points are still clockwise' assert is_convex(verticies), 'Shape verticies must be convex!' if len(verticies) in self.specialPolyTypes: self.polyType = self.specialPolyTypes[len(verticies)] else: self.polyType = pg.GL_POLYGON self.shape = Poly(self.body, verticies) # note: collisionLayers and collisionType get created by physics.py self.shape.layers = self.collisionLayers self.shape.collision_type = self.collisionType self.shapes = [self.shape] bb = self.shape.cache_bb() self.size = Vec2d(bb.right-bb.left, bb.top-bb.bottom)
def assert_valid(verts): if len(verts) < 3: raise TypeError('need 3 or more verts: %s' % (verts,)) if not is_convex(verts): raise TypeError('not convex: %s' % (verts,)) if area(verts) == 0.0: raise TypeError("colinear: %s" % (verts,)) # note: pymunk considers y-axis points down, ours points up, # hence we consider pymunk's 'clockwise' to be anticlockwise if not is_clockwise(verts): raise TypeError('anticlockwise winding: %s' % (verts,))