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, asm, res): super().__init__(asm, res) self.button = None self.sm = None self.conversation = None self.police_effect = None self.minimap = None if BOX2D_DEBUG: Framework.__init__(self) self.load() self.run()
def Step(self, settings): if not self.doorOpened: #print(self.arm.get_points(self.arm.get_angles())) settings.positionIterations = 50 settings.velocityIterations = 250 if self.door.joint.angle < -math.pi / 3: self.doorOpened = True print("Door Successfully Opened") Framework.Step(self, settings) self.update_keyboard_angles() self.arm.update_arm_IK(self.arm_speeds[0], self.arm_speeds[1], self.arm_speeds[2], self.arm_speeds[3]) if self.recording: print("recording") self.dataGen.produceAnnotation(self.arm, self.door, self.arm_speeds) if self.reset: print("reset") if self.recording: self.dataGen.closeFile() print("closed file") self.dataGen.newFile() self.__reset() self.reset = False
def __init__(self): Framework.__init__(self) ground = self.world.CreateStaticBody( position=(0, 20), shapes=[b2EdgeShape(vertices=[(-20, 0), (20, 0)])], ) a = 0.5 depth = 0 self.root = self.add_node(ground, (0, 0), depth, 3.0, a) self.world.CreateRevoluteJoint(bodyA=ground, bodyB=self.root, localAnchorA=(0, 0), localAnchorB=(0, a))
def PreSolve(self, contact, old_manifold): Framework.PreSolve(self, contact, old_manifold) fixture_a, fixture_b = contact.fixtureA, contact.fixtureB if fixture_a == self.platform_fixture: contact.tangentSpeed = 5.0 elif fixture_b == self.platform_fixture: contact.tangentSpeed = -5.0
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 Step(self, settings): settings.positionIterations = 50 settings.velocityIterations = 250 if self.door.joint.angle < -math.pi / 3: self.doorOpened = True print("Door Successfully Opened") Framework.Step(self, settings) self.update_keyboard_angles() self.arm.update_arm(target_pose_mode=True) renderer = self.renderer
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 __reset(self): self.alphabet = [False] * 26 Framework.__init__(self) Framework.setZoom(self, 115) Framework.setCenter(self, [0, 1.74]) self.wristAngle = math.pi / 2 self.doorOpened = False self.recording = False self.reset = False self.arm_angles = [ math.pi / 2, math.pi / 2, math.pi / 2, math.pi / 2, math.pi / 2 ] lengths = [1, 1, .4, .27] self.arm = Arm(self.world, self.arm_angles, lengths, basePos=(-.1, 0.08), motorRange=2 * math.pi) self.arm.target_pose[0] = random.uniform(-1, 1) self.arm.target_pose[1] = random.uniform(1.5, 2.3) ground = self.world.CreateBody(position=(0, 0)) ground.CreateEdgeChain([(-4, -2), (-4, .6), (-2, .6), (-2, 0), (2, 0), (2, 6), (4, 6), (4, -2), (-2, -2)]) self.door = Door(self.world, (-2, .62), 1.2)
def __init__(self): Framework.__init__(self) self.wristAngle = math.pi / 2 self.doorOpened = False self.arm_angles = [ math.pi / 2, math.pi / 2, math.pi / 2, math.pi / 2, math.pi / 2 ] lengths = [1, 1, .4, .27] self.arm = Arm(self.world, self.arm_angles, lengths, basePos=(-.1, 0.08), motorRange=2 * math.pi) ground = self.world.CreateBody(position=(0, 0)) ground.CreateEdgeChain([(-4, -2), (-4, .6), (-2, .6), (-2, 0), (2, 0), (2, 6), (4, 6), (4, -2), (-2, -2)]) self.door = Door(self.world, (-2, .62), 1.2) box = self.world.CreateDynamicBody(position=(1, 2)) box.CreatePolygonFixture(box=(.1, .1), density=0.3, friction=0.5)
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 Step(self, settings): Framework.Step(self, settings) if self.go and settings.hz > 0.0: self.time += 1.0 / settings.hz linear_offset = (6 * sin(2.0 * self.time), 8.0 + 4.0 * sin(1.0 * self.time)) angular_offset = 4.0 * self.time self.joint.linearOffset = linear_offset self.joint.angularOffset = angular_offset renderer = self.renderer renderer.DrawPoint(renderer.to_screen( linear_offset), 4, b2Color(0.9, 0.9, 0.9))
def Step(self, settings): Framework.Step(self, settings) # Apply external force # Apply spring force to building 1 x0 = 0 - self.floor[0].position.x x1 = self.floor[1].position.x - self.floor[0].position.x self.floor[0].ApplyForce((spring_k * (x0 + x1), 0), (0, 0), True) xf = self.floor[-2].position.x - self.floor[-1].position.x self.floor[-1].ApplyForce((spring_k * xf, 0), (0, 0), True) for i in range(1, num_of_floors - 1): x_bot = self.floor[i - 1].position.x - self.floor[i].position.x x_top = self.floor[i + 1].position.x - self.floor[i].position.x self.floor[i].ApplyForce((spring_k * (x_bot + x_top), 0), (0, 0), True) # Apply pendulum damping rel_vel = self.pendulum.linearVelocity.x - self.floor[ -1].linearVelocity.x self.pendulum.ApplyForce((-pendulum_damping * rel_vel, 0), (0, 0), True) # Apply spring force to building 2 x0 = self.base2.position.x - self.floor2[0].position.x x1 = self.floor2[1].position.x - self.floor2[0].position.x self.floor2[0].ApplyForce((spring_k * (x0 + x1), 0), (0, 0), True) xf = self.floor2[-2].position.x - self.floor2[-1].position.x self.floor2[-1].ApplyForce((spring_k * xf, 0), (0, 0), True) for i in range(1, num_of_floors - 1): x_bot = self.floor2[i - 1].position.x - self.floor2[i].position.x x_top = self.floor2[i + 1].position.x - self.floor2[i].position.x self.floor2[i].ApplyForce((spring_k * (x_bot + x_top), 0), (0, 0), True) # Logging print(str(self.time) + " " + str(self.floor[-1].position.x), file=open("last_data_point.dat", "w"), flush=True) self.time += 1 / 60
def Step(self, settings): Framework.Step(self, settings) renderer = self.renderer try: poly = b2PolygonShape(vertices=self.verts) except AssertionError as ex: self.Print('b2PolygonShape failed: %s' % ex) else: self.Print('Valid: %s' % poly.valid) renderer.DrawPolygon([renderer.to_screen(v) for v in self.verts], b2Color(0.9, 0.9, 0.9)) for i, v in enumerate(self.verts): renderer.DrawPoint(renderer.to_screen(v), 2.0, b2Color(0.9, 0.5, 0.5)) x, y = renderer.to_screen(v) self.DrawStringAt(x + 0.05, y + 0.05, '%d' % i) if self.auto: self.generate()
def __init__(self): Framework.__init__(self) self.auto = False self.generate()
def __init__(self): Framework.__init__(self) self.world.gravity = 100 * self.world.gravity # convert from m/s^2 to cm/s^2 self.time = 0 self.ground = self.world.CreateStaticBody( position=(0, 0), shapes=[b2EdgeShape(vertices=[(-200, 0), (200, 0)])]) self.pendulum = self.world.CreateDynamicBody( position=(0, num_of_floors * floor_height - pendulum_length), shapes=[b2PolygonShape(box=(pendulum_size, pendulum_size))], linearDamping=0) self.pendulum.mass = pendulum_mass self.base = self.world.CreateStaticBody( position=(0, floor_thickness), shapes=[b2PolygonShape(box=(floor_width / 2, floor_thickness))]) self.floor = [] for i in range(num_of_floors): self.floor.append( self.world.CreateDynamicBody( position=(0, floor_thickness + (i + 1) * floor_height), shapes=[ b2PolygonShape(box=(floor_width / 2, floor_thickness)) ])) self.floor[i].mass = floor_mass self.base2 = self.world.CreateStaticBody( position=(second_building_offset, floor_thickness), shapes=[b2PolygonShape(box=(floor_width / 2, floor_thickness))]) self.floor2 = [] for i in range(num_of_floors): self.floor2.append( self.world.CreateDynamicBody( position=(second_building_offset, floor_thickness + (i + 1) * floor_height), shapes=[ b2PolygonShape(box=(floor_width / 2, floor_thickness)) ])) self.floor2[i].mass = floor_mass # Building 1 joints floor_edge_vec = (floor_width / 2, 0) self.world.CreateDistanceJoint( bodyA=self.base, bodyB=self.floor[0], anchorA=self.base.position + floor_edge_vec, anchorB=self.floor[0].position + floor_edge_vec, dampingRatio=1) self.world.CreateDistanceJoint( bodyA=self.base, bodyB=self.floor[0], anchorA=self.base.position - floor_edge_vec, anchorB=self.floor[0].position - floor_edge_vec, dampingRatio=1) for i in range(num_of_floors - 1): self.world.CreateDistanceJoint( bodyA=self.floor[i], bodyB=self.floor[i + 1], anchorA=self.floor[i].position + floor_edge_vec, anchorB=self.floor[i + 1].position + floor_edge_vec, dampingRatio=1) self.world.CreateDistanceJoint( bodyA=self.floor[i], bodyB=self.floor[i + 1], anchorA=self.floor[i].position - floor_edge_vec, anchorB=self.floor[i + 1].position - floor_edge_vec, dampingRatio=1) # Building 2 joints floor_edge_vec = (floor_width / 2, 0) self.world.CreateDistanceJoint( bodyA=self.base2, bodyB=self.floor2[0], anchorA=self.base2.position + floor_edge_vec, anchorB=self.floor2[0].position + floor_edge_vec, dampingRatio=1) self.world.CreateDistanceJoint( bodyA=self.base2, bodyB=self.floor2[0], anchorA=self.base2.position - floor_edge_vec, anchorB=self.floor2[0].position - floor_edge_vec, dampingRatio=1) for i in range(num_of_floors - 1): self.world.CreateDistanceJoint( bodyA=self.floor2[i], bodyB=self.floor2[i + 1], anchorA=self.floor2[i].position + floor_edge_vec, anchorB=self.floor2[i + 1].position + floor_edge_vec, dampingRatio=1) self.world.CreateDistanceJoint( bodyA=self.floor2[i], bodyB=self.floor2[i + 1], anchorA=self.floor2[i].position - floor_edge_vec, anchorB=self.floor2[i + 1].position - floor_edge_vec, dampingRatio=1) # Pendulum joint self.world.CreateDistanceJoint(bodyA=self.floor[-1], bodyB=self.pendulum, anchorA=self.floor[-1].position, anchorB=self.pendulum.position)
def Step(self, settings): Framework.Step(self, settings) blob_step(self.world, self.bodies, self.blob_radius, self.jump, self.move) self.jump = 100
def Step(self, settings): Framework.Step(self, settings) laser_display(self.renderer, self.laser_body, laser_half_width=LASER_HALF_WIDTH)