def __init__(self): super(BoxCutter, self).__init__() # The ground self.ground = self.world.CreateStaticBody( userData='ground', shapes=[ b2EdgeShape(vertices=[(-50, 0), (50, 0)]), b2EdgeShape(vertices=[(-50, 0), (-50, 10)]), b2EdgeShape(vertices=[(50, 0), (50, 10)]), ] ) self.laser_body = self.world.CreateDynamicBody( userData='laser', position=(0, 2), fixtures=b2FixtureDef( density=4.0, shape=b2PolygonShape(box=(LASER_HALF_WIDTH, 1)) ) ) for i in range(2): self.world.CreateDynamicBody( userData=LASER_SPLIT_TAG, position=(3.0 + i * 6, 8), fixtures=b2FixtureDef( density=5.0, shape=b2PolygonShape(box=(3, 3)) ) )
def createWorld(self): self._isLiving = True self._auto = True self._pid_control = PIDControl(1, 0.05, 0.1) # kp, ki, kd self.ground = self.world.CreateBody( shapes=b2EdgeShape(vertices=[(-25, 0), (25, 0)]) ) self.carBody = self.world.CreateDynamicBody( position=(0, 3), fixtures=b2FixtureDef( shape=b2PolygonShape(box=(5, 1)), density=1) ) self.carLwheel = self.world.CreateDynamicBody( position=(-3, 1), fixtures=b2FixtureDef( shape=b2CircleShape(radius=1), density=2, friction=1) ) self.carRwheel = self.world.CreateDynamicBody( position=(3, 1), fixtures=b2FixtureDef( shape=b2CircleShape(radius=1), density=2, friction=1) ) self.pendulum = self.world.CreateDynamicBody( position=(0, 13), fixtures=b2FixtureDef( shape=b2PolygonShape(box=(0.5, 10)), density=1), ) self.pendelumJoin = self.world.CreateRevoluteJoint( bodyA=self.carBody, bodyB=self.pendulum, anchor=(0, 3), maxMotorTorque=1, enableMotor=True ) self.pendelumRJoin =self.world.CreateRevoluteJoint( bodyA=self.carBody, bodyB=self.carRwheel, anchor=(3, 1), maxMotorTorque=1, enableMotor=True, ) self.pendelumLJoin = self.world.CreateRevoluteJoint( bodyA=self.carBody, bodyB=self.carLwheel, anchor=(-3, 1), maxMotorTorque=1, enableMotor=True, )
def __init__(self): super(VerticalStack, self).__init__() columns = 5 rows = 16 ground = self.world.CreateStaticBody(shapes=[ b2EdgeShape(vertices=[(-40, 0), (40, 0)]), b2EdgeShape(vertices=[(20, 0), (20, 20)]), ]) box = b2FixtureDef(shape=b2PolygonShape(box=(0.5, 0.5)), density=1, friction=0.3) circle = b2FixtureDef(shape=b2CircleShape(radius=0.5), density=1, friction=0.3) box_start = -10 box_space = 2.5 circle_start = 8 circle_space = 2.5 for j in range(columns): for i in range(rows): self.world.CreateDynamicBody( fixtures=box, position=(box_start + box_space * j, 0.752 + 1.54 * i)) self.world.CreateDynamicBody( fixtures=circle, position=(circle_start + circle_space * j, 0.752 + 1.54 * i))
def __init__(self): Framework.__init__(self) self.using_contacts = True ground = self.world.CreateStaticBody( shapes=[b2EdgeShape(vertices=[(-20, 0), (20, 0)])], ) # Platform self.platform = self.world.CreateStaticBody( position=(-5, 5), allowSleep=False, fixtures=b2FixtureDef(friction=0.8, shape=b2PolygonShape(box=(10.0, 5.0)),), ) self.platform_fixture = self.platform.fixtures[0] # Boxes for i in range(5): self.platform = self.world.CreateDynamicBody( position=(-10.0 + 2.0 * i, 7.0), fixtures=b2FixtureDef(density=20.0, shape=b2PolygonShape(box=(0.5, 0.5)),), )
def __init__(self): super(Bridge, self).__init__() # The ground ground = self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)])) create_bridge(self.world, ground, (1.0, 0.25), (-14.5, 5), self.numPlanks, 0.2, 20) fixture = b2FixtureDef(shape=b2PolygonShape(vertices=[ (-0.5, 0.0), (0.5, 0.0), (0.0, 1.5), ]), density=1.0) for i in range(2): self.world.CreateDynamicBody( position=(-8 + 8 * i, 12), fixtures=fixture, ) fixture = b2FixtureDef(shape=b2CircleShape(radius=0.5), density=1) for i in range(3): self.world.CreateDynamicBody( position=(-6 + 6 * i, 10), fixtures=fixture, )
def __init__(self): Framework.__init__(self) self.using_contacts = True ground = self.world.CreateStaticBody( shapes=[b2EdgeShape(vertices=[(-20, 0), (20, 0)])], ) # Platform self.platform = self.world.CreateStaticBody( position=(-5, 5), allowSleep=False, fixtures=b2FixtureDef( friction=0.8, shape=b2PolygonShape(box=(10.0, 5.0)), ), ) self.platform_fixture = self.platform.fixtures[0] # Boxes for i in range(5): self.platform = self.world.CreateDynamicBody( position=(-10.0 + 2.0 * i, 7.0), fixtures=b2FixtureDef( density=20.0, shape=b2PolygonShape(box=(0.5, 0.5)), ), )
def __init__(self): super(Spring, self).__init__() ground = self.world.CreateStaticBody( position=(0, 0), shapes=[b2EdgeShape(vertices=[(-50, 0), (50, 0)])], ) self.bodyA = self.world.CreateDynamicBody( position=(0, 10), fixtures=b2FixtureDef( shape=b2PolygonShape(box=(1, 1)), density=1.0) ) self.bodyB = self.world.CreateDynamicBody( position=(0, 5), fixtures=b2FixtureDef( shape=b2CircleShape(radius = 0.5), density=1.0, restitution=0) ) dfn = b2DistanceJointDef( frequencyHz=2.5, dampingRatio=0, bodyA=self.bodyA, bodyB=self.bodyB, anchorA=(0, 10), anchorB=(0, 5), collideConnected=True ) self.world.CreateJoint(dfn)
def __init__(self): super(Bridge, self).__init__() # The ground ground = self.world.CreateBody( shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)]) ) create_bridge(self.world, ground, (1.0, 0.25), (-14.5, 5), self.numPlanks, 0.2, 20) fixture = b2FixtureDef( shape=b2PolygonShape(vertices=[(-0.5, 0.0), (0.5, 0.0), (0.0, 1.5), ]), density=1.0 ) for i in range(2): self.world.CreateDynamicBody( position=(-8 + 8 * i, 12), fixtures=fixture, ) fixture = b2FixtureDef(shape=b2CircleShape(radius=0.5), density=1) for i in range(3): self.world.CreateDynamicBody( position=(-6 + 6 * i, 10), fixtures=fixture, )
def create_car(world, offset, wheel_radius, wheel_separation, density=1.0, wheel_friction=0.9, scale=(1.0, 1.0), chassis_vertices=None, wheel_axis=(0.0, 1.0), wheel_torques=[20.0, 10.0], wheel_drives=[True, False], hz=4.0, zeta=0.7, **kwargs): """ """ x_offset, y_offset = offset scale_x, scale_y = scale if chassis_vertices is None: chassis_vertices = [ (-1.5, -0.5), (1.5, -0.5), (1.5, 0.0), (0.0, 0.9), (-1.15, 0.9), (-1.5, 0.2), ] chassis_vertices = [(scale_x * x, scale_y * y) for x, y in chassis_vertices] radius_scale = sqrt(scale_x ** 2 + scale_y ** 2) wheel_radius *= radius_scale chassis = world.CreateDynamicBody( position=(x_offset, y_offset), fixtures=b2FixtureDef( shape=b2PolygonShape(vertices=chassis_vertices), density=density, ) ) wheels, springs = [], [] wheel_xs = [-wheel_separation * scale_x / 2.0, wheel_separation * scale_x / 2.0] for x, torque, drive in zip(wheel_xs, wheel_torques, wheel_drives): wheel = world.CreateDynamicBody( position=(x_offset + x, y_offset - wheel_radius), fixtures=b2FixtureDef( shape=b2CircleShape(radius=wheel_radius), density=density, ) ) spring = world.CreateWheelJoint( bodyA=chassis, bodyB=wheel, anchor=wheel.position, axis=wheel_axis, motorSpeed=0.0, maxMotorTorque=torque, enableMotor=drive, frequencyHz=hz, dampingRatio=zeta ) wheels.append(wheel) springs.append(spring) return chassis, wheels, springs
def __init__(self, robot_config, b2_world, ground): self.robot_config = robot_config self._robot_state = DefaultRobotState(self.robot_config) # adding the rods rod_fixture = b2FixtureDef( shape=b2PolygonShape(box=(0.5 * robot_config.rod_diameter, 0.5 * robot_config.rod_length)), density=robot_config.rod_density) rod_fixture.filter.groupIndex = -1 self.rods = [] y_pos = 0.5 * robot_config.rod_length for _ in range(2): b2_rod = b2_world.CreateDynamicBody( position=(robot_config.position, y_pos), fixtures=rod_fixture) self.rods.append(b2_rod) y_pos += robot_config.rod_length # adding the rackets racket_fixture = b2FixtureDef( shape=b2PolygonShape(box=(0.5 * robot_config.racket_diameter, 0.5 * robot_config.racket_thickness)), density=robot_config.racket_density, restitution=robot_config.racket_restitution) self.racket = b2_world.CreateDynamicBody( position=(robot_config.position, 2. * robot_config.rod_length + 0.5 * robot_config.racket_thickness), fixtures=racket_fixture) # adding joints self.joints = [None, None, None] # items : rods + racket dyn_items = self.rods + [self.racket] previous_dyn_item = ground previous_length = 0 for index, (robot_item, dyn_item) in enumerate(zip(robot_config.items, dyn_items)): self.joints[index] = b2_world.CreateRevoluteJoint( bodyA=previous_dyn_item, bodyB=dyn_item, anchor=(robot_config.position, previous_length), lowerAngle=-robot_item.joint_limit, upperAngle=+robot_item.joint_limit, enableLimit=True, enableMotor=True) previous_length = previous_length + robot_item.length previous_dyn_item = dyn_item
def __init__(self): super(Pinball, self).__init__() # The ground ground = self.world.CreateBody( shapes=b2LoopShape(vertices=[(0, -2), (8, 6), (8, 20), (-8, 20), (-8, 6)]), ) # Flippers p1, p2 = (-2, 0), (2, 0) fixture = b2FixtureDef(shape=b2PolygonShape(box=(1.75, 0.1)), density=1) flipper = {'fixtures': fixture} self.leftFlipper = self.world.CreateDynamicBody( position=p1, **flipper ) self.rightFlipper = self.world.CreateDynamicBody( position=p2, **flipper ) rjd = b2RevoluteJointDef( bodyA=ground, bodyB=self.leftFlipper, localAnchorA=p1, localAnchorB=(0, 0), enableMotor=True, enableLimit=True, maxMotorTorque=1000, motorSpeed=0, lowerAngle=-30.0 * b2_pi / 180.0, upperAngle=5.0 * b2_pi / 180.0, ) self.leftJoint = self.world.CreateJoint(rjd) rjd.motorSpeed = 0 rjd.localAnchorA = p2 rjd.bodyB = self.rightFlipper rjd.lowerAngle = -5.0 * b2_pi / 180.0 rjd.upperAngle = 30.0 * b2_pi / 180.0 self.rightJoint = self.world.CreateJoint(rjd) # Ball self.ball = self.world.CreateDynamicBody( fixtures=b2FixtureDef( shape=b2CircleShape(radius=0.2), density=1.0), bullet=True, position=(1, 15)) self.pressed = False
def __init__(self): super(BodyTypes, self).__init__() # The ground ground = self.world.CreateBody( shapes=b2EdgeShape(vertices=[(-20, 0), (20, 0)]) ) # The attachment self.attachment = self.world.CreateDynamicBody( position=(0, 3), fixtures=b2FixtureDef( shape=b2PolygonShape(box=(0.5, 2)), density=2.0), ) # The platform fixture = b2FixtureDef( shape=b2PolygonShape(box=(4, 0.5)), density=2, friction=0.6, ) self.platform = self.world.CreateDynamicBody( position=(0, 5), fixtures=fixture, ) # The joints joining the attachment/platform and ground/platform self.world.CreateRevoluteJoint( bodyA=self.attachment, bodyB=self.platform, anchor=(0, 5), maxMotorTorque=50, enableMotor=True ) self.world.CreatePrismaticJoint( bodyA=ground, bodyB=self.platform, anchor=(0, 5), axis=(1, 0), maxMotorForce=1000, enableMotor=True, lowerTranslation=-10, upperTranslation=10, enableLimit=True ) # And the payload that initially sits upon the platform # Reusing the fixture we previously defined above fixture.shape.box = (0.75, 0.75) self.payload = self.world.CreateDynamicBody( position=(0, 8), fixtures=fixture, )
def __init__(self): Framework.__init__(self) ground = self.world.CreateStaticBody( position=(0,0), shapes=b2PolygonShape(box=(1000,1)), ) a=14.5 b=12.2 vertices=[] for i in range(16): ell_x=a*math.cos(22.5*i*math.pi/180) ell_y=b*math.sin(22.5*i*math.pi/180) vertices.append((ell_x,ell_y)) self.qross = self.world.CreateDynamicBody( position=(0, 20), allowSleep=True, fixtures=b2FixtureDef(density=2.0, friction=0.6, shape=b2PolygonShape(vertices=vertices),), ) self.ball = self.world.CreateStaticBody( fixtures=b2FixtureDef( shape=b2CircleShape(radius=2.2), density=1.0), bullet=True, position=(0, 1000)) leg = self.world.CreateDynamicBody( position=(0, 20), allowSleep=True, fixtures=b2FixtureDef(density=2.0, friction=0.0, shape=b2PolygonShape(vertices=[(20,1.3), (0,1.3), (0,0), (20,0)]),), ) self.world.CreateRevoluteJoint( bodyA=leg, bodyB=self.qross, anchor=(0, 20), ) self.joint = self.world.CreateMotorJoint(bodyA=leg, bodyB=self.qross, maxForce=0, maxTorque=500000) self.go = False self.time = 0.0
def __init__(self): super(Tower, self).__init__() #Ground ground = self.world.CreateStaticBody( position=(0, 0), shapes=[b2EdgeShape(vertices=[(-30, 0), (30, 0)])] #, ## b2PolygonShape(box=(0.2, 5, (0.5, 5),0))] ) ## self.world.CreateStaticBody( ## shapes=[b2PolygonShape(box=[1, 1, (5, 15), 0]), ## b2PolygonShape(box=[1, 1, (-2, 12), 0]), ## ] ## ) self._x = 0 ## self.body = self.world.CreateDynamicBody( ## position=(0.5, 10), ## fixtures=b2FixtureDef( ## shape=b2PolygonShape(box=(4, 0.1)), density=1.0), ## ) # Small circle #circle = b2FixtureDef( #shape=b2CircleShape(radius=0.25), #density=100.0, #) # Generate bullet (circle) #self.bullet = self.world.CreateBody( #position=(self._x, 30), #bullet=True, #type=b2_dynamicBody, #fixtures=circle, #linearVelocity=(0, -50) #) self.stack1 = self.world.CreateDynamicBody( position=(self._x, 0), ## bullet=True, type=b2_dynamicBody, fixtures=b2FixtureDef(shape=b2PolygonShape(box=(5, 7)), density=100.0), linearVelocity=(0, -50)) self.stack2 = self.world.CreateDynamicBody( position=(self._x, 0), ## bullet=True, type=b2_dynamicBody, fixtures=b2FixtureDef(shape=b2PolygonShape(box=(4, 4)), density=100.0), linearVelocity=(0, -50))
def __init__(self): super(ApplyForce, self).__init__() self.world.gravity = (0.0, 0.0) # The boundaries ground = self.world.CreateBody(position=(0, 20)) ground.CreateEdgeChain( [(-20, -20), (-20, 20), (20, 20), (20, -20), (-20, -20)] ) # TODO: make note of transform.R.set() -> transform.angle = xf1 = b2Transform() xf1.angle = 0.3524 * b2_pi xf1.position = xf1.R * (1.0, 0.0) xf2 = b2Transform() xf2.angle = -0.3524 * b2_pi xf2.position = xf2.R * (-1.0, 0.0) self.body = self.world.CreateDynamicBody( position=(0, 2), angle=b2_pi, angularDamping=5, linearDamping=0.1, shapes=[b2PolygonShape(vertices=[xf1 * (-1, 0), xf1 * (1, 0), xf1 * (0, .5)]), b2PolygonShape(vertices=[xf2 * (-1, 0), xf2 * (1, 0), xf2 * (0, .5)])], shapeFixture=b2FixtureDef(density=2.0), ) gravity = 10.0 fixtures = b2FixtureDef(shape=b2PolygonShape(box=(0.5, 0.5)), density=1, friction=0.3) for i in range(10): body = self.world.CreateDynamicBody( position=(0, 5 + 1.54 * i), fixtures=fixtures) # For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m) r = sqrt(2.0 * body.inertia / body.mass) self.world.CreateFrictionJoint( bodyA=ground, bodyB=body, localAnchorA=(0, 0), localAnchorB=(0, 0), collideConnected=True, maxForce=body.mass * gravity, maxTorque=body.mass * r * gravity )
def create_jar(world, properties, scene_width, scene_height, shape, color, diameter, x, y, angle, isDynamic): # constants from PHYRE BASE_RATIO = Constant.JAR_BASE_RATIO WIDTH_RATIO = Constant.JAR_WIDTH_RATIO jar_height = scene_width * Constant._jar_diameter_to_default_scale( diameter) jar_width = jar_height * WIDTH_RATIO jar_base_width = jar_width * BASE_RATIO jar_thickness = Constant._jar_thickness_from_height( scene_width, jar_height) vertices_list, _ = Constant._build_jar_vertices(height=jar_height, width=jar_width, base_width=jar_base_width, thickness=jar_thickness) jar_center = _get_jar_center(scene_width, scene_height, x, y, angle, jar_height, jar_thickness) literal1_shape = b2PolygonShape() literal2_shape = b2PolygonShape() bottom_shape = b2PolygonShape() _set_vertices(literal1_shape, vertices_list[0]) _set_vertices(literal2_shape, vertices_list[1]) _set_vertices(bottom_shape, vertices_list[2]) literal1_fixture = b2FixtureDef(shape=literal1_shape, density=properties.densities["jar"], friction=properties.frictions["jar"], restitution=properties.restitutions["jar"]) literal2_fixture = b2FixtureDef(shape=literal2_shape, density=properties.densities["jar"], friction=properties.frictions["jar"], restitution=properties.restitutions["jar"]) bottom_fixture = b2FixtureDef(shape=bottom_shape, density=properties.densities["jar"], friction=properties.frictions["jar"], restitution=properties.restitutions["jar"]) if isDynamic: body = world.CreateDynamicBody( position=jar_center, fixtures=[literal1_fixture, literal2_fixture, bottom_fixture], angle=2 * b2_pi * angle) else: body = world.CreateStaticBody( position=jar_center, fixtures=[literal1_fixture, literal2_fixture, bottom_fixture], angle=2 * b2_pi * angle) return body
def __init__(self): super(fallingObjects, self).__init__() #Groud ground = self.world.CreateStaticBody( position=(0, 0), shapes=[ b2EdgeShape(vertices=[(-30, 0), (30, 0)]), b2PolygonShape(box=(0.2, 5, (0.5, 5), 0)) ]) self.world.CreateStaticBody(shapes=[ b2PolygonShape(box=[1, 1, (5, 15), 0]), b2PolygonShape(box=[1, 1, (-2, 12), 0]), ]) self._x = 0.20352793 self.body = self.world.CreateDynamicBody( position=(0.5, 10), fixtures=b2FixtureDef(shape=b2PolygonShape(box=(4, 0.1)), density=1.0), ) # Small circle #circle = b2FixtureDef( #shape=b2CircleShape(radius=0.25), #density=100.0, #) # Generate bullet (circle) #self.bullet = self.world.CreateBody( #position=(self._x, 30), #bullet=True, #type=b2_dynamicBody, #fixtures=circle, #linearVelocity=(0, -50) #) self.bullet = self.world.CreateDynamicBody( position=(self._x, 30), bullet=True, fixtures=b2FixtureDef(shape=b2PolygonShape(box=(0.25, 0.25)), density=100.0), linearVelocity=(0, -50)) self.bullet1 = self.world.CreateDynamicBody( position=(self._x, 30), bullet=True, fixtures=b2FixtureDef(shape=b2PolygonShape(box=(0.25, 0.25)), density=100.0), linearVelocity=(0, -50))
def __init__(self, **kwargs): super(combatSettings, self).__init__() # display self.render = False self.record = False self.record_dir = "../imgs/" self.verbose_display = True # environment self.start_spread = 20 self.start_point = [0, 0] self.agent_rotation_speed = 0.8 * (2 * np.pi) self.agent_force = 20 self.time_limit = 60 self.cooldown_atk = 1 # minimum time between consecutive attacks self.cooldown_mov_penalty = 0.5 # movement speed penalty. Maybe due to attacking or getting hit circle = b2FixtureDef(shape=b2CircleShape(radius=0.5), density=1, friction=0.3) self.bodySettings = { "fixtures": circle, "linearDamping": 5, "fixedRotation": True }
def __init__(self): super(OneSidedPlatform, self).__init__() # The ground ground = self.world.CreateBody( shapes=b2EdgeShape(vertices=[(-20, 0), (20, 0)]) ) # The platform half_height = 0.5 ypos = 10 body = self.world.CreateBody( position=(0, ypos), shapes=b2PolygonShape(box=(3, half_height)) ) self.platform = body.fixtures[0] # The circular character self.character_radius = 0.5 body = self.world.CreateDynamicBody( position=(0, 12), fixtures=b2FixtureDef(shape=b2CircleShape( radius=self.character_radius), density=1.0), ) self.character = body.fixtures[0] body.linearVelocity = (0, -50) self.bottom = ypos - half_height # The bottom of the platform self.top = ypos + half_height # The top of the platform self.state = 'unknown'
def __init__(self, dt, ball_radius=0.2, pixels_per_worldunit=8): """ :param dt: Timestep delta """ self.dt = dt self.ball_radius = ball_radius self.ball_init_position = None self.ball_init_velocity = (0, -0.001) self.last_row_xs = [] self.camera = drawing_util.Camera( pos=(0, -self.basket_slack), fov_dims=(max(abs(self.arena_bounds_x[0]), abs(self.arena_bounds_x[1])), max(abs(self.arena_bounds_y[0]), abs(self.arena_bounds_y[1])))) self.display_objects = dict() self.world = Box2D.b2World(gravity=(0, -10.0), doSleep=True) self.obstacle_bodies = [] ball_fixture = b2FixtureDef( shape=b2CircleShape(radius=self.ball_radius), density=self.ball_density, friction=self.ball_friction, restitution=self.ball_restitution) self.ball_body = self.world.CreateDynamicBody( position=self.ball_init_position, fixtures=ball_fixture) self.pixels_per_worldunit = pixels_per_worldunit drawing_util.add_circle(self.display_objects, self.ball_body, self.ball_radius, name='ball', drawing_layer=1, color=self.ball_color)
class TileMap(Entity): boxShape = b2PolygonShape() boxShape.SetAsBox(1, 1) boxFixtureDef = b2FixtureDef(shape=boxShape, density=1) def __init__(self, pos=(0, 0), size=(10, 10)): super().__init__() self.pos = pos #self.size = size raw = Image.open('q4_map.png') pixels = raw.load() self.size = raw.width, raw.height self.blocks = dict() for x in range(self.size[0]): for y in range(self.size[1]): if pixels[x, y][3] == 0: self.blocks[x, y] = None else: self.blocks[x, y] = SolidBlock(self, (x, y)) self.blocks[x, y].add() kitten = image.load('q4_map.png') self.texture = kitten.get_texture() def render(self): #glBindTexture(self.texture.target, self.texture.id) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) self.texture.blit(self.pos[0], self.pos[1])
def Break(self): # Create two bodies from one body = self.body center = body.worldCenter body.DestroyFixture(self.fixtures[1]) self.fixture2 = None body2 = self.world.CreateDynamicBody( position=body.position, angle=body.angle, shapes=self.shapes[1], shapeFixture=b2FixtureDef(density=1), ) # Compute consistent velocities for new bodies based on cached # velocity. velocity1 = (self.velocity + b2Cross(self.angularVelocity, body.worldCenter - center)) velocity2 = (self.velocity + b2Cross(self.angularVelocity, body2.worldCenter - center)) body.angularVelocity = self.angularVelocity body.linearVelocity = velocity1 body2.angularVelocity = self.angularVelocity body2.linearVelocity = velocity2
def CreateCircle(self, pos): fixture = b2FixtureDef(shape=b2CircleShape(radius=self.radius, pos=(0, 0)), density=1, friction=0.1) self.world.CreateDynamicBody(position=pos, fixtures=fixture)
def add_node(self, parent, local_anchor, depth, offset, a): density = 20.0 h = (0, a) p = parent.position + local_anchor - h fixture = b2FixtureDef(shape=b2PolygonShape(box=(0.25 * a, a)), density=density) body = self.world.CreateDynamicBody(position=p, fixtures=fixture) if depth == self.max_depth: return body a1 = (offset, -a) a2 = (-offset, -a) body1 = self.add_node(body, a1, depth + 1, 0.5 * offset, a) body2 = self.add_node(body, a2, depth + 1, 0.5 * offset, a) self.world.CreateRevoluteJoint(bodyA=body, bodyB=body1, localAnchorA=a1, localAnchorB=h) self.world.CreateRevoluteJoint(bodyA=body, bodyB=body2, localAnchorA=a2, localAnchorB=h) return body
def __init__(self): super(Pyramid, self).__init__() # The ground ground = self.world.CreateStaticBody( shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)]) ) box_half_size = (0.5, 0.5) box_density = 5.0 box_rows = 20 x = b2Vec2(-7, 0.75) deltaX = (0.5625, 1.25) deltaY = (1.125, 0) for i in range(box_rows): y = x.copy() for j in range(i, box_rows): self.world.CreateDynamicBody( position=y, fixtures=b2FixtureDef( shape=b2PolygonShape(box=box_half_size), density=box_density) ) y += deltaY x += deltaX
def __init__(self, radius): self.radius = radius self.fixture = b2FixtureDef(shape=b2CircleShape(radius=self.radius, pos=(0, 0)), density=1, friction=0.5, restitution=0)
def copyWorld(world): copy = b2World(gravity=world.gravity, doSleep=world.allowSleeping) copy.continuousPhysics = world.continuousPhysics copy.velocityThreshold = world.velocityThreshold copy.positionThreshold = world.positionThreshold for body in world.bodies: fixtures = [] for fixture in body.fixtures: fixtures.append( b2FixtureDef(shape=fixture.shape, density=fixture.density, restitution=fixture.restitution, friction=fixture.friction)) copy.CreateBody(type=body.type, fixtures=fixtures, userData=body.userData, position=b2Vec2(body.position.x, body.position.y), angle=body.angle, linearVelocity=b2Vec2(body.linearVelocity.x, body.linearVelocity.y), angularVelocity=body.angularVelocity) for body in copy.bodies: body.sleepingAllowed = False return copy
def __init__(self, position, world, gravity): self.mWorld = world self.mGravity = gravity pos = b2Vec2(position[0] + self.BOX_WIDTH/2, position[1] + self.BOX_HEIGHT/2) #create box physicsbody body = world.CreateDynamicBody(position = pos) shape = b2PolygonShape() fd = b2FixtureDef() fd.shape = shape fd.isSensor = True #gravitysensor shape.SetAsBox(0.1,0.1) fd.userData = Sensor.GRAVITYZONESENSOR body.CreateFixture(fd) #collisionbody body.CreatePolygonFixture(box=(self.BOX_WIDTH/2, self.BOX_HEIGHT/2), density=6, friction=0) body.fixedRotation = True body.bullet = True body.allowSleep = False body.mass = 2 body.userData = self super(Box, self).__init__(pos, b2Vec2(self.BOX_WIDTH, self.BOX_HEIGHT), body, b2Vec2(0,0), 0, b2Vec2(0,0))
def __init__(self, vertices): self.vertices = vertices assert isinstance(vertices, list) self.fixture = b2FixtureDef(shape=b2LoopShape(vertices=self.vertices), density=1, friction=0.5, restitution=0)
def __init__(self, game, x, y): super().__init__(game) self.velocity = None self.x = x self.y = y self.surface = None self.r_scale = None self.room_change_behavior = RoomChangeBehavior.deactivate self.img = img.load("data/images/star.png").convert_alpha() position = (self.x * self.game.physics_scale, self.y * self.game.physics_scale) self.body = self.game.world.CreateDynamicBody(position=position, userData=self) fixt_def = b2FixtureDef( shape=b2CircleShape(radius=0.1), isSensor=True, categoryBits=Category.PICKUP, maskBits=(Category.ENEMY | Category.CHARACTER | Category.CHARACTER_BULLET | Category.WALL | Category.CORPSE), ) fixture = self.body.CreateFixture(fixt_def)
def __init__(self): super(Pyramid, self).__init__() # The ground ground = self.world.CreateStaticBody(shapes=b2EdgeShape( vertices=[(-40, 0), (40, 0)])) box_half_size = (0.5, 0.5) box_density = 5.0 box_rows = 20 x = b2Vec2(-7, 0.75) deltaX = (0.5625, 1.25) deltaY = (1.125, 0) for i in range(box_rows): y = x.copy() for j in range(i, box_rows): self.world.CreateDynamicBody( position=y, fixtures=b2FixtureDef( shape=b2PolygonShape(box=box_half_size), density=box_density)) y += deltaY x += deltaX
def __init__(self, game, x, y, rotation, category, mask): super().__init__(game) self.room_change_behavior = RoomChangeBehavior.destroy self.rotation = rotation self.x = x self.y = y # image: if category == Category.ENEMY_BULLET: self.img = img.load("data/images/bullet.png").convert_alpha() else: self.img = img.load("data/images/bullet.png").convert_alpha() # collisions: position = (self.x * game.physics_scale, self.y * game.physics_scale) self.body = self.game.world.CreateDynamicBody(position=position, userData=self) fixt_def = b2FixtureDef( shape=b2CircleShape(radius=0.1), isSensor=True, categoryBits=category, maskBits=mask, ) # noinspection PyUnusedLocal fixture = self.body.CreateFixture(fixt_def) self.velocity = [0, 0] # add particles: self.particle_effect = ParticleEffect( self.x, self.y, (190, 190, 190), 0.08, [1, 10], [1, 15], 0, 50 )
def __init__(self): super(OneSidedPlatform, self).__init__() # The ground ground = self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-20, 0), (20, 0)])) # The platform half_height = 0.5 ypos = 10 body = self.world.CreateBody(position=(0, ypos), shapes=b2PolygonShape(box=(3, half_height))) self.platform = body.fixtures[0] # The circular character self.character_radius = 0.5 body = self.world.CreateDynamicBody( position=(0, 12), fixtures=b2FixtureDef( shape=b2CircleShape(radius=self.character_radius), density=1.0), ) self.character = body.fixtures[0] body.linearVelocity = (0, -50) self.bottom = ypos - half_height # The bottom of the platform self.top = ypos + half_height # The top of the platform self.state = 'unknown'
def create_blob(world, center, radius, circle_radius=0.5, shape_num=24, angularDamping=0.5, linearDamping=0.5, friction=0.5, density=5.0, **kwargs): def get_pos(angle): return (cos(angle * pi / 180.0) * radius + center[0], sin(angle * pi / 180.0) * radius + center[1]) circle = b2CircleShape(radius=circle_radius) fixture = b2FixtureDef(shape=circle, friction=friction, density=density, restitution=0.0) bodies = [world.CreateDynamicBody(position=get_pos( i), fixtures=fixture) for i in range(0, 360, int(360 / shape_num))] joints = [] prev_body = bodies[-1] for body in bodies: joint = world.CreateDistanceJoint( bodyA=prev_body, bodyB=body, anchorA=prev_body.position, anchorB=body.position, dampingRatio=10.0) joints.append(joint) prev_body = body return bodies, joints
def _reset(self): self._destroy() membrane_base.reset_helper(self) # Creating the object to manipulate object_fixture = b2FixtureDef(shape=b2PolygonShape(box=(OBJ_SIZE / 2, OBJ_SIZE / 2)), density=1, friction=0.6, restitution=0.0) object_position = (membrane_base.BOX_WIDTH / 2, OBJ_SIZE / 2 + membrane_base.LINK_HEIGHT) self.object = self.world.CreateDynamicBody( position=object_position, fixtures=object_fixture, linearDamping=0.6 # Control this parameter for surface friction ) self.object.color1 = (1, 1, 0) self.object.color2 = (0, 0, 0) self.drawlist = self.drawlist + [self.object] return self._step(np.array([0, 0, 0, 0, 0]))[0] # action: zero motor speed
def replace_body(old_body, new_position, scale, shape='box', category_bits=0x0001, mask_bits=(0x0001 | 0x0002 | 0x0003)): if shape == 'box': new_shape = b2PolygonShape( box=(scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO, scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO)) elif shape == 'circle': new_shape = b2CircleShape( radius=(scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO)) else: new_shape = b2PolygonShape( box=(scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO, scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO)) new_body = defines.world.CreateDynamicBody( position=new_position, angle=old_body.angle, angularDamping=old_body.angularDamping, linearDamping=old_body.linearDamping, shapes=new_shape, bullet=old_body.bullet, shapeFixture=b2FixtureDef(density=2.0, categoryBits=category_bits, maskBits=mask_bits)) return new_body
def create_creature_bodies(world, creatures): ''' Creates a list of creature bodies and returns reference bodies ''' reference_body = {} for creature in creatures: body = {} for edge in creature.edges: vertex = creature.vertices[edge[0]], creature.vertices[edge[1]] point = line_to_rectangle(*vertex, THICKNESS) fixture = b2FixtureDef( shape=b2PolygonShape(vertices=point), density=DENSITY, friction=FRICTION, ) fixture.filter.groupIndex = -1 body[tuple(edge)] = world.CreateDynamicBody( fixtures=fixture, ) for edge in creature.edges: adjacent = find_adjacent_edges(edge, creature.edges) for a_edge in adjacent: anchor = int(creature.vertices[edge[0]][0]), int(creature.vertices[edge[0]][1]) world.CreateRevoluteJoint( bodyA=body[tuple(edge)], bodyB=body[tuple(a_edge)], anchor=anchor, collideConnected=True, motorSpeed=MOTOR_SPEED, maxMotorTorque=MAX_MOTOR_TORQUE, enableMotor=True, ) reference_body[creature.identity] = body[tuple(edge)] return reference_body
def create_wheel(self, anchor, connected, graph_code, angle=0, speed=5, length=2): body = self.world.CreateDynamicBody( position=anchor, angle=angle * b2_pi / 180, angularDamping=10, fixtures=b2FixtureDef(shape=b2CircleShape(radius=0.5 + length / 3), groupIndex=-1, density=1), ) # define joints anchors = [] motorJoint = self.world.CreateRevoluteJoint(bodyA=body, bodyB=connected, anchor=anchor, collideConnected=False, motorSpeed=speed, maxMotorTorque=5000, enableMotor=self.motorOn) return body, anchors, self.encode_part('W', angle, speed, length, graph_code)
def CreateCircle(self, pos): fixture = b2FixtureDef(shape=b2CircleShape(radius=self.radius, pos=(0, 0)), density=1, friction=0.1) self.world.CreateDynamicBody( position=pos, fixtures=fixture )
def Keyboard(self, key): if key == Keys.K_b: if self.bullet: self.world.DestroyBody(self.bullet) self.bullet = None circle = b2FixtureDef(shape=b2CircleShape(radius=0.25), density=20, restitution=0.05) self.bullet = self.world.CreateDynamicBody( position=(-31, 5), bullet=True, fixtures=circle, linearVelocity=(400, 0) )
def create_cloth(world, segment_count, body_size, position=(0, 30), group_index=-1, bar_height=0.5, base_hz=15, base_damping=0.11, density=0.2): segment_w, segment_h = segment_count body_spacing_w = body_size * 2 total_w = body_spacing_w * segment_w position = b2Vec2(*position) # The static bar at the top which holds the cloth bar = world.CreateStaticBody(position=position) bar.CreatePolygonFixture(box=(total_w / 2.0 + body_spacing_w, bar_height / 2.0), groupIndex=group_index) box_fixture = b2FixtureDef(shape=b2CircleShape(radius=body_size), groupIndex=group_index, density=density) weld_joints = [] distance_joints = [] cloth = [[None] * segment_h for x in range(segment_w)] for y in range(segment_h): pos = position - (total_w / 2.0, y * body_spacing_w) for x in range(segment_w): pos += (body_spacing_w, 0.0) body = world.CreateDynamicBody(position=pos, fixtures=box_fixture) cloth[x][y] = body if y == 0: joint = world.CreateWeldJoint(bodyA=body, bodyB=bar, anchor=body.position) weld_joints.append(joint) connect_bodies = [] for y in range(segment_h): for x in range(segment_w): if x <= segment_w - 2: left_body = cloth[x][y] right_body = cloth[x + 1][y] connect_bodies.append((left_body, right_body)) if y > 0: left_body = cloth[x][y] right_body = cloth[x][y - 1] connect_bodies.append((left_body, right_body)) for bodyA, bodyB in connect_bodies: joint = world.CreateDistanceJoint( bodyA=bodyA, bodyB=bodyB, anchorA=bodyA.position, anchorB=bodyB.position, frequencyHz=base_hz + b2Random(0, base_hz / 2.0), dampingRatio=base_damping + b2Random(0.01, base_damping), ) distance_joints.append(joint) return cloth, weld_joints, distance_joints
def __init__(self): super(Rope, self).__init__() # The ground ground = self.world.CreateBody( shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)])) shape = b2PolygonShape(box=(0.5, 0.125)) fd = b2FixtureDef( shape=shape, friction=0.2, density=20, categoryBits=0x0001, maskBits=(0xFFFF & ~0x0002), ) N = 10 y = 15.0 prevBody = ground for i in range(N): if i < N - 1: body = self.world.CreateDynamicBody( position=(0.5 + i, y), fixtures=fd, ) else: shape.box = (1.5, 1.5) fd.density = 100 fd.categoryBits = 0x0002 body = self.world.CreateDynamicBody( position=(i, y), fixtures=fd, angularDamping=0.4, ) self.world.CreateRevoluteJoint( bodyA=prevBody, bodyB=body, anchor=(i, y), collideConnected=False, ) prevBody = body extraLength = 0.01 self.rd = rd = b2RopeJointDef( bodyA=ground, bodyB=body, maxLength=N - 1.0 + extraLength, localAnchorA=(0, y), localAnchorB=(0, 0) ) self.rope = self.world.CreateJoint(rd)
def Step(self, settings): Framework.Step(self, settings) self.count -= 1 if self.count == 0: return self.world.CreateDynamicBody( position=(0, 10), allowSleep=False, fixtures=b2FixtureDef( density=1.0, shape=b2PolygonShape(box=(0.125, 0.125))), )
def __init__(self): super(VerticalStack, self).__init__() columns = 5 rows = 16 ground = self.world.CreateStaticBody( shapes=[b2EdgeShape(vertices=[(-40, 0), (40, 0)]), b2EdgeShape(vertices=[(20, 0), (20, 20)])] ) box = b2FixtureDef(shape=b2PolygonShape(box=(0.5, 0.5)), density=1, friction=0.3) circle = b2FixtureDef(shape=b2CircleShape(radius=0.5), density=1, friction=0.3) box_start = -10 box_space = 2.5 circle_start = 8 circle_space = 2.5 for j in range(columns): for i in range(rows): self.world.CreateDynamicBody(fixtures=box, position=(box_start + box_space * j, 0.752 + 1.54 * i)) self.world.CreateDynamicBody( fixtures=circle, position=(circle_start + circle_space * j, 0.752 + 1.54 * i) )
def __init__(self): super(Bullet, self).__init__() ground = self.world.CreateStaticBody( position=(0, 0), shapes=[b2EdgeShape(vertices=[(-10, 0), (10, 0)]), b2PolygonShape(box=(0.2, 1, (0.5, 1), 0))] ) self._x = 0.20352793 self.body = self.world.CreateDynamicBody( position=(0, 4), fixtures=b2FixtureDef( shape=b2PolygonShape(box=(2, 0.1)), density=1.0), ) self.bullet = self.world.CreateDynamicBody( position=(self._x, 10), bullet=True, fixtures=b2FixtureDef(shape=b2PolygonShape( box=(0.25, 0.25)), density=100.0), linearVelocity=(0, -50) )
def __init__(self): super(BodyTypes, self).__init__() # The ground ground = self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-20, 0), (20, 0)])) # The attachment self.attachment = self.world.CreateDynamicBody( position=(0, 3), fixtures=b2FixtureDef(shape=b2PolygonShape(box=(0.5, 2)), density=2.0) ) # The platform fixture = b2FixtureDef(shape=b2PolygonShape(box=(4, 0.5)), density=2, friction=0.6) self.platform = self.world.CreateDynamicBody(position=(0, 5), fixtures=fixture) # The joints joining the attachment/platform and ground/platform self.world.CreateRevoluteJoint( bodyA=self.attachment, bodyB=self.platform, anchor=(0, 5), maxMotorTorque=50, enableMotor=True ) self.world.CreatePrismaticJoint( bodyA=ground, bodyB=self.platform, anchor=(0, 5), axis=(1, 0), maxMotorForce=1000, enableMotor=True, lowerTranslation=-10, upperTranslation=10, enableLimit=True, ) # And the payload that initially sits upon the platform # Reusing the fixture we previously defined above fixture.shape.box = (0.75, 0.75) self.payload = self.world.CreateDynamicBody(position=(0, 8), fixtures=fixture)
def __init__(self, physworld, gravity, size, lifetime, pos): self.mWorld = physworld self.mGravity = gravity self.mSize = size self.mLifetime = lifetime self.mIsAlive = True #create body self.mBody = self.mWorld.CreateDynamicBody(position = pos) shape = b2PolygonShape() shape.SetAsBox(self.mSize.x/2.0,self.mSize.y/2.0) fd = b2FixtureDef() fd.shape = shape fd.categoryBits = Filter.CATEGORY_FX fd.maskBits = Filter.MASK_FX self.mBody.CreateFixture(fd) self.mBody.userData = self
def __init__(self): super(Breakable, self).__init__() # The ground self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)])) # The breakable body self.shapes = (b2PolygonShape(box=(0.5, 0.5, (-0.5, 0), 0)), b2PolygonShape(box=(0.5, 0.5, (0.5, 0), 0)) ) self.body = self.world.CreateDynamicBody( position=(0, 40), angle=0.25 * b2_pi, shapes=self.shapes, shapeFixture=b2FixtureDef(density=1), ) self.fixtures = self.body.fixtures
def CreateShape(self, shapeindex): try: shape = self.shapes[shapeindex] except IndexError: return pos = (10.0 * (2.0 * random() - 1.0), 10.0 * (2.0 * random() + 1.0)) defn = b2BodyDef( type=b2_dynamicBody, fixtures=b2FixtureDef(shape=shape, friction=0.3), position=pos, angle=(b2_pi * (2.0 * random() - 1.0)), ) if isinstance(shape, b2CircleShape): defn.angularDamping = 0.02 self.world.CreateBody(defn)
def __init__(self): super(Restitution, self).__init__() # The ground ground = self.world.CreateStaticBody( shapes=b2EdgeShape(vertices=[(-20, 0), (20, 0)]) ) radius = 1.0 density = 1.0 # The bodies for i, restitution in enumerate([0.0, 0.1, 0.3, 0.5, 0.75, 0.9, 1.0]): self.world.CreateDynamicBody( position=(-10 + 3.0 * i, 20), fixtures=b2FixtureDef( shape=b2CircleShape(radius=radius), density=density, restitution=restitution) )
def LaunchBomb(self, position, velocity): """ A bomb is a simple circle which has the specified position and velocity. position and velocity must be b2Vec2's. """ if self.bomb: self.world.DestroyBody(self.bomb) self.bomb = None self.bomb = self.world.CreateDynamicBody( allowSleep=True, position=position, linearVelocity=velocity, fixtures=b2FixtureDef( shape=b2CircleShape(radius=0.3), density=20, restitution=0.1) )
def __init__(self): super(Web, self).__init__() # The ground ground = self.world.CreateBody( shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)]) ) fixture = b2FixtureDef(shape=b2PolygonShape(box=(0.5, 0.5)), density=5, friction=0.2) self.bodies = [self.world.CreateDynamicBody( position=pos, fixtures=fixture ) for pos in ((-5, 5), (5, 5), (5, 15), (-5, 15))] bodies = self.bodies # Create the joints between each of the bodies and also the ground # bodyA bodyB localAnchorA localAnchorB sets = [(ground, bodies[0], (-10, 0), (-0.5, -0.5)), (ground, bodies[1], (10, 0), (0.5, -0.5)), (ground, bodies[2], (10, 20), (0.5, 0.5)), (ground, bodies[3], (-10, 20), (-0.5, 0.5)), (bodies[0], bodies[1], (0.5, 0), (-0.5, 0)), (bodies[1], bodies[2], (0, 0.5), (0, -0.5)), (bodies[2], bodies[3], (-0.5, 0), (0.5, 0)), (bodies[3], bodies[0], (0, -0.5), (0, 0.5)), ] # We will define the positions in the local body coordinates, the length # will automatically be set by the __init__ of the b2DistanceJointDef self.joints = [] for bodyA, bodyB, localAnchorA, localAnchorB in sets: dfn = b2DistanceJointDef( frequencyHz=4.0, dampingRatio=0.5, bodyA=bodyA, bodyB=bodyB, localAnchorA=localAnchorA, localAnchorB=localAnchorB, ) self.joints.append(self.world.CreateJoint(dfn))
def __init__(self): Framework.__init__(self) ground = self.world.CreateStaticBody( shapes=[b2EdgeShape(vertices=[(-20, 0), (20, 0)])], ) # Define motorized body body = self.world.CreateDynamicBody( position=(0, 8), allowSleep=False, fixtures=b2FixtureDef(density=2.0, friction=0.6, shape=b2PolygonShape(box=(2.0, 0.5)),), ) self.joint = self.world.CreateMotorJoint(bodyA=ground, bodyB=body, maxForce=1000, maxTorque=1000) self.go = False self.time = 0.0
def __init__(self): super(Tiles, self).__init__() a = 0.5 def ground_positions(): N = 200 M = 10 position = b2Vec2(0, 0) for i in range(M): position.x = -N * a for j in range(N): yield position position.x += 2.0 * a position.y -= 2.0 * a ground = self.world.CreateStaticBody( position=(0, -a), shapes=[b2PolygonShape(box=(a, a, position, 0)) for position in ground_positions()] ) count = 20 def dynamic_positions(): x = b2Vec2(-7.0, 0.75) deltaX = (0.5625, 1.25) deltaY = (1.125, 0.0) for i in range(count): y = x.copy() for j in range(i, count): yield y y += deltaY x += deltaX for pos in dynamic_positions(): self.world.CreateDynamicBody( position=pos, fixtures=b2FixtureDef( shape=b2PolygonShape(box=(a, a)), density=5) )
def __init__(self): Framework.__init__(self) ground = self.world.CreateBody() body = self.world.CreateDynamicBody( position=(0, 10), allowSleep=False, shapeFixture=b2FixtureDef(density=5.0), shapes=[ b2PolygonShape(box=(0.5, 10, (10, 0), 0)), b2PolygonShape(box=(0.5, 10, (-10, 0), 0)), b2PolygonShape(box=(10, 0.5, (0, 10), 0)), b2PolygonShape(box=(10, 0.5, (0, -10), 0)), ] ) self.joint = self.world.CreateRevoluteJoint(bodyA=ground, bodyB=body, localAnchorA=(0, 10), localAnchorB=(0, 0), referenceAngle=0, motorSpeed=0.05 * b2_pi, enableMotor=True, maxMotorTorque=1.0e8)
def create_bridge(world, ground, size, offset, plank_count, friction=0.6, density=1.0): """ Create a bridge with plank_count planks, utilizing rectangular planks of size (width, height). The bridge should start at x_offset, and continue to roughly x_offset+width*plank_count. The y will not change. """ width, height = size x_offset, y_offset = offset half_height = height / 2 plank = b2FixtureDef( shape=b2PolygonShape(box=(width / 2, height / 2)), friction=friction, density=density, ) bodies = [] prevBody = ground for i in range(plank_count): body = world.CreateDynamicBody( position=(x_offset + width * i, y_offset), fixtures=plank, ) bodies.append(body) world.CreateRevoluteJoint( bodyA=prevBody, bodyB=body, anchor=(x_offset + width * (i - 0.5), y_offset) ) prevBody = body world.CreateRevoluteJoint( bodyA=prevBody, bodyB=ground, anchor=(x_offset + width * (plank_count - 0.5), y_offset), ) return bodies
def __createCollisionBody(self, pos, size, shape, ud = None): #collisionbody body = self.mWorld.CreateDynamicBody(position = pos) if shape == EnemyShape.CIRCLE: shape = b2CircleShape(radius=size.x/2) elif shape == EnemyShape.POLYGON: shape = b2PolygonShape() shape.SetAsBox(size.x/2, size.y/2) elif shape == EnemyShape.LINE: shape = b2EdgeShape() shape.vertices = b2Vec2(-size.x / 2.0, -size.y / 2.0), b2Vec2(size.x / 2.0, size.y / 2.0) fd = b2FixtureDef() fd.shape = shape fd.isSensor = True body.CreateFixture(fd) body.isbullet = False body.fixedRotation = True body.userData = self if ud == None else ud return body