def xml_2_b2bodies(world: b2World, xmlStr): tree = ET.ElementTree(ET.fromstring(xmlStr)) for body in tree.getroot().findall("body"): id = int(body.attrib["index"]) type = body.attrib["type"] p = body.find("position") px, py = float(p.attrib['x']), float(p.attrib['y']) v = body.find("velocity") vx, vy = float(v.attrib['vx']), float(v.attrib['vy']) shape = body.find("shape").attrib['value'] spin = float(body.find("spin").attrib['omega']) mass = float(body.find("mass").attrib['value']) orientation = float(body.find("orientation").attrib['theta']) inertia = float(body.find("inertia").attrib['value']) if type == "free": bod = world.CreateDynamicBody( position=b2Vec2(px, py), fixtures=eval(shape).fixture, ) elif type == "fixed": bod = world.CreateStaticBody( position=b2Vec2(px, py), fixtures=eval(shape).fixture, ) bod.userData = BodyData(b_id=id, shape=shape) bod.mass = mass bod.inertia = inertia bod.linearVelocity = b2Vec2(vx, vy) bod.angle = orientation bod.angularVelocity = spin
def add_borders(world : Box2D.b2World, left_border : float, right_border : float, bottom_border : float, top_border : float): fixture_defs = _create_fixtures(left_border, right_border, bottom_border, top_border) border_body_def = Box2D.b2BodyDef() border_body_def.fixtures = fixture_defs border_body_def.type = Box2D.b2_staticBody border_body_def.position = (0, 0) return world.CreateBody(border_body_def)
def create_body(world: b2World, body_type, fixture_def, position=b2Vec2(0, 0)) -> b2Body: body_def = b2BodyDef() body_def.type = body_type body_def.position = position body = world.CreateBody(body_def) body.CreateFixture(fixture_def) return body
def __init__(self, space: Box2D.b2World, pos, orientation, physics_pars, facing_axis, color, **kwargs): if self.__class__ == Box2DSegment: raise NotImplementedError( 'Abstract class Box2DSegment cannot be instantiated.') self.physics_pars = physics_pars self._color = color self._body: Box2D.b2Body = space.CreateDynamicBody( position=Box2D.b2Vec2(*pos), angle=orientation, linearDamping=physics_pars['lin_damping'], angularDamping=physics_pars['ang_damping']) self._body.linearVelocity = Box2D.b2Vec2(*[.0, .0]) self._body.angularVelocity = .0 self._body.bullet = True # overriden by LarvaBody self.facing_axis = facing_axis
def add_asteroid_play_space(world: Box2D.b2World, left_border: float, right_border: float, bottom_border: float, top_border: float): fixture_shape = Box2D.b2PolygonShape() width = right_border - left_border + PLAYSPACE_PADDING height = top_border - bottom_border + PLAYSPACE_PADDING fixture_shape.SetAsBox( width, height, Box2D.b2Vec2(left_border + width / 2, bottom_border + height / 2), 0) fixture_def = Box2D.b2FixtureDef() fixture_def.shape = fixture_shape fixture_def.isSensor = True play_space_body_def = Box2D.b2BodyDef() play_space_body_def.fixtures = [fixture_def] play_space_body_def.type = Box2D.b2_staticBody play_space_body_def.position = (0, 0) return world.CreateBody(play_space_body_def)
def __init__(self, world: Box2D.b2World, position=None, orientation=None): if self.__class__ == Body: raise NotImplementedError( 'Abstract class Body cannot be instantiated.') self._color = np.array((93, 133, 195)) self._highlight_color = np.array((238, 80, 62)) if position is None: position = [.0, .0] position = np.asarray(position) if orientation is None: orientation = .0 self._world = world self._body: Box2D.b2Body = world.CreateDynamicBody( position=Box2D.b2Vec2(*(_world_scale * position)), angle=orientation, linearDamping=self._linear_damping, angularDamping=self._angular_damping) self._body.linearVelocity = Box2D.b2Vec2(*[.0, .0]) self._body.angularVelocity = .0