def __init__(self, body, vertices, offset=(0,0), auto_order_vertices=True): """Create a polygon body : `Body` The body to attach the poly to vertices : [(x,y)] or [`Vec2d`] Define a convex hull of the polygon with a counterclockwise winding. offset : (x,y) or `Vec2d` The offset from the body's center of gravity in body local coordinates. auto_order_vertices : bool Set to True to automatically order the vertices. If you know the vertices are in the correct (clockwise) orded you can gain a little performance by setting this to False. """ self._body = body self.offset = offset #self.verts = (Vec2d * len(vertices))(*vertices) self.verts = (Vec2d * len(vertices)) self.verts = self.verts(Vec2d(0, 0)) i_vs = enumerate(vertices) if auto_order_vertices and not u.is_clockwise(vertices): i_vs = zip(xrange(len(vertices)-1, -1, -1), vertices) for (i, vertex) in i_vs: self.verts[i].x = vertex[0] self.verts[i].y = vertex[1] self._shape = cp.cpPolyShapeNew(body._body, len(vertices), self.verts, offset) self._shapecontents = self._shape.contents
def __init__(self, body, vertices, offset=(0,0), auto_order_vertices=False): """Create a polygon body : `Body` The body to attach the poly to vertices : [(x,y)] or [`Vec2d`] Define a convex hull of the polygon with a counterclockwise winding. offset : (x,y) or `Vec2d` The offset from the body's center of gravity in body local coordinates. auto_order_vertices : bool Set to True to automatically order the vertices. Currently not supported. """ if auto_order_vertices: raise Exception(NotImplemented) self._body = body self.offset = offset #self.verts = (Vec2d * len(vertices))(*vertices) self.verts = (Vec2d * len(vertices)) self.verts = self.verts(Vec2d(0, 0)) for (i, vertex) in enumerate(vertices): self.verts[i].x = vertex[0] self.verts[i].y = vertex[1] self._shape = cp.cpPolyShapeNew(body._body, len(vertices), self.verts, offset) self._shapecontents = self._shape.contents
def create_cube(space, mass=5.0, xy=(0, 0)): p_num = 4 P_ARR = vec2d * p_num p_arr = P_ARR(vec2d(0, 0)) s = 5 sprite = rabbyt.Sprite(shape=(-s, s, s, -s), xy=xy) for i, p in enumerate(sprite.shape): p_arr[i].x, p_arr[i].y = p moment = 500 body = cp.cpBodyNew(mass, moment) body.contents.p = vec2d(*xy) cp.cpSpaceAddBody(space, body) shape = cp.cpPolyShapeNew(body, p_num, p_arr, vec2d(0, 0)) shape.contents.u = 0.5 shape.contents.collision_type = COLLTYPE_DEFAULT cp.cpSpaceAddShape(space, shape) anims = chipmunk_body_anims(body) sprite.x, sprite.y, sprite.rot = anims return shape, sprite
def create_cube(space, mass = 5.0, xy = (0,0)): p_num = 4 P_ARR = vec2d * p_num p_arr = P_ARR(vec2d(0,0)) s = 5 sprite = rabbyt.Sprite(shape=(-s,s,s,-s), xy=xy) for i, p in enumerate(sprite.shape): p_arr[i].x, p_arr[i].y = p moment = 500 body = cp.cpBodyNew(mass, moment) body.contents.p = vec2d(*xy) cp.cpSpaceAddBody(space, body) shape = cp.cpPolyShapeNew(body, p_num, p_arr, vec2d(0,0)) shape.contents.u = 0.5 shape.contents.collision_type = COLLTYPE_DEFAULT cp.cpSpaceAddShape(space, shape) anims = chipmunk_body_anims(body) sprite.x, sprite.y, sprite.rot = anims return shape, sprite
def __init__(self, space, mass, width, height, pos, color=(255,0,0,255)): poly = [ [ width/2, height/2 ], [ width/2, -height/2 ], [ -width/2, -height/2 ], [ -width/2, height/2 ], ] p_num = len(poly) P_ARR = vec2d * p_num p_arr = P_ARR(vec2d(0,0)) for i, (x,y) in enumerate( poly ): p_arr[i].x = x p_arr[i].y = y moment = cp.cpMomentForPoly( mass, p_num, p_arr, vec2d(0,0)) self.color = color self.body = cp.cpBodyNew( mass, moment ) self.body.contents.p = vec2d( *pos ) cp.cpSpaceAddBody( space, self.body ) self.shape = shape = cp.cpPolyShapeNew( self.body, p_num, p_arr, vec2d(0,0) ) shape.contents.u = 0.5 cp.cpSpaceAddShape(space, shape)