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): 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(Membrane, self).__init__() # Creating the Exterior Box that defines the 2D Plane self.exterior_box = self.world.CreateStaticBody( position=(0, 0), shapes=b2LoopShape(vertices=EXT_BOX_POLY)) # Creating the object to manipulate ball_fixture = b2FixtureDef(shape=b2CircleShape(radius=BOX_WIDTH / 40), density=1, friction=0.6) self.ball = self.world.CreateDynamicBody(position=(BOX_WIDTH / 2, BOX_HEIGHT / 2), fixtures=ball_fixture) # Creating 5 actuators self.actuator_list = [] self.actuator_joint_list = [] actuator_fixture = b2FixtureDef(shape=b2CircleShape(radius=BOX_WIDTH / 50.0), density=1, friction=0.6, groupIndex=-1) for i in range(5): actuator = self.world.CreateDynamicBody( position=(BOX_WIDTH / 5 * i + BOX_WIDTH / 10, BOX_HEIGHT / 10), fixtures=actuator_fixture) # actuator = self.world.CreateKinematicBody( # position = (BOX_WIDTH/5*i+BOX_WIDTH/10, BOX_HEIGHT/10), # shapes = b2CircleShape(radius=BOX_WIDTH/50.0), # diameter is 1/5th of the space allocated for each actuator # ) actuator_joint = self.world.CreatePrismaticJoint( bodyA=self.exterior_box, bodyB=actuator, anchor=actuator.position, axis=(0, 1), lowerTranslation=0, upperTranslation=BOX_WIDTH * 5.0 / 6.0, enableLimit=True, maxMotorForce=1000.0, motorSpeed=0, enableMotor=True) self.actuator_list.append(actuator) self.actuator_joint_list.append(actuator_joint) self.actuator_joint_list[0].motorSpeed = 1.0 self.actuator_joint_list[3].motorSpeed = 0.5 self.actuator_joint_list[2].motorSpeed = 0.1 # Creating the linkages that will form the semi-flexible membrane self.link_left_list = [] # four links self.link_right_list = [] # four links link_fixture = b2FixtureDef( shape=b2PolygonShape(box=(BOX_WIDTH / 12, BOX_WIDTH / 70.0)), density=1, friction=0.6, groupIndex=-1 # neg index to prevent collision ) for i in range(1, 5): link_left = self.world.CreateDynamicBody( position=(BOX_WIDTH / 5 * i - BOX_WIDTH / 10 + BOX_WIDTH / 12, BOX_HEIGHT / 10), fixtures=link_fixture) self.link_left_list.append(link_left) link_right = self.world.CreateDynamicBody( position=(BOX_WIDTH / 5 * i + BOX_WIDTH / 10 - BOX_WIDTH / 12, BOX_HEIGHT / 10), fixtures=link_fixture) self.link_right_list.append(link_right) joint_left = self.world.CreateRevoluteJoint( bodyA=self.actuator_list[i - 1], bodyB=link_left, anchor=self.actuator_list[i - 1].worldCenter, collideConnected=False) joint_right = self.world.CreateRevoluteJoint( bodyA=self.actuator_list[i], bodyB=link_right, anchor=self.actuator_list[i].worldCenter, collideConnected=False) joint_middle = self.world.CreatePrismaticJoint( bodyA=link_left, bodyB=link_right, anchor=(link_right.position.x - BOX_WIDTH / 12 + BOX_WIDTH / 70, link_right.position.y), axis=(1, 0), lowerTranslation=0, upperTranslation=BOX_WIDTH / 6 - BOX_WIDTH / 35, enableLimit=True)
def _reset(self): self._destroy() # Creating the Exterior Box that defines the 2D Plane self.exterior_box = self.world.CreateStaticBody( position=(0, 0), shapes=b2LoopShape(vertices=EXT_BOX_POLY)) self.exterior_box.color1 = (0, 0, 0) self.exterior_box.color2 = (0, 0, 0) # Creating the object to manipulate object_fixture = b2FixtureDef(shape=b2CircleShape(radius=BOX_WIDTH * OBJ_SIZE / 2), density=0.3, friction=0.6, restitution=0.5) # Randomizing object's initial position # object_position = ( # self.np_random.uniform(BOX_WIDTH*OBJ_POS_OFFSET,BOX_WIDTH-BOX_WIDTH*OBJ_POS_OFFSET), # BOX_HEIGHT/5 # ) # object_position = ( # self.np_random.uniform(BOX_WIDTH*OBJ_POS_OFFSET,BOX_WIDTH-BOX_WIDTH*OBJ_POS_OFFSET), # self.np_random.uniform(BOX_WIDTH*OBJ_POS_OFFSET,BOX_HEIGHT-BOX_WIDTH*OBJ_POS_OFFSET) # ) object_position = (self.np_random.uniform( BOX_WIDTH * OBJ_POS_OFFSET, BOX_WIDTH - BOX_WIDTH * OBJ_POS_OFFSET), BOX_HEIGHT / 3) self.object = self.world.CreateDynamicBody( position=object_position, fixtures=object_fixture, linearDamping=0.3 # Control this parameter for surface friction ) self.object.at_target = False self.object.at_target_count = 0 self.object.color1 = (1, 1, 0) self.object.color2 = (0, 0, 0) # Creating 5 actuators actuator_fixture = b2FixtureDef( shape=b2CircleShape(radius=BOX_WIDTH * ACTUATOR_TIP_SIZE / 2), density=1, friction=0.6, restitution=0.0, groupIndex=-1) for i in range(5): actuator = self.world.CreateDynamicBody( position=((BOX_SIDE_OFFSET + GAP * i) * BOX_WIDTH, 0), fixtures=actuator_fixture) actuator.color1 = (0, 0, 0.5) actuator.color2 = (0, 0, 0) actuator.joint = self.world.CreatePrismaticJoint( bodyA=self.exterior_box, bodyB=actuator, anchor=actuator.position, axis=(0, 1), lowerTranslation=0, upperTranslation=ACTUATOR_TRANSLATION_MAX, enableLimit=True, maxMotorForce=1000.0, motorSpeed=0, enableMotor=True) self.actuator_list.append(actuator) self.drawlist = self.actuator_list + [self.object] if self.with_linkage: # Creating the linkages that will form the semi-flexible membrane link_fixture = b2FixtureDef( shape=b2PolygonShape(box=(LINK_WIDTH * BOX_WIDTH / 2, LINK_HEIGHT * BOX_WIDTH / 2)), density=1, friction=0.6, restitution=0.0, groupIndex=-1 # neg index to prevent collision ) for i in range(4): link_left = self.world.CreateDynamicBody( position=(BOX_WIDTH * (BOX_SIDE_OFFSET + GAP * i + LINK_WIDTH / 2), 0), fixtures=link_fixture) link_left.color1 = (0, 1, 1) link_left.color2 = (1, 0, 1) self.link_left_list.append(link_left) link_right = self.world.CreateDynamicBody( position=(BOX_WIDTH * (BOX_SIDE_OFFSET + GAP * (i + 1) - LINK_WIDTH / 2), 0), fixtures=link_fixture) link_right.color1 = (0, 1, 1) link_right.color2 = (1, 0, 1) self.link_right_list.append(link_right) joint_left = self.world.CreateRevoluteJoint( bodyA=self.actuator_list[i], bodyB=link_left, anchor=self.actuator_list[i].worldCenter, collideConnected=False) joint_right = self.world.CreateRevoluteJoint( bodyA=self.actuator_list[i + 1], bodyB=link_right, anchor=self.actuator_list[i + 1].worldCenter, collideConnected=False) joint_middle = self.world.CreatePrismaticJoint( bodyA=link_left, bodyB=link_right, anchor=(link_right.position.x - BOX_WIDTH * (LINK_WIDTH / 2 + LINK_HEIGHT / 2), link_right.position.y), axis=(1, 0), lowerTranslation=0, upperTranslation=BOX_WIDTH * LINK_WIDTH * 2 / 3, enableLimit=True) # Adding linkages to the drawlist self.drawlist = self.link_left_list + self.link_right_list + self.drawlist return self._step(np.array([0, 0, 0, 0, 0]))[0] # action: zero motor speed
def reset_helper(env_obj): # Creating the Exterior Box that defines the 2D Plane env_obj.exterior_box = env_obj.world.CreateStaticBody( position=(0, 0), shapes=b2LoopShape(vertices=EXT_BOX_POLY)) env_obj.exterior_box.color1 = (0, 0, 0) env_obj.exterior_box.color2 = (0, 0, 0) # Creating 5 actuators actuator_fixture = b2FixtureDef( shape=b2CircleShape(radius=ACTUATOR_TIP_SIZE / 2), density=1, friction=0.6, restitution=0.0, groupIndex=-1) for i in range(5): actuator = env_obj.world.CreateDynamicBody(position=(BOX_SIDE_OFFSET + GAP * i, 0), fixtures=actuator_fixture) actuator.color1 = (0, 0, 0.5) actuator.color2 = (0, 0, 0) actuator.joint = env_obj.world.CreatePrismaticJoint( bodyA=env_obj.exterior_box, bodyB=actuator, anchor=actuator.position, axis=(0, 1), lowerTranslation=0, upperTranslation=ACTUATOR_TRANSLATION_MAX, enableLimit=True, maxMotorForce=10000.0, motorSpeed=0, enableMotor=True) env_obj.actuator_list.append(actuator) env_obj.drawlist = env_obj.actuator_list # Creating the linkages that will form the semi-flexible membrane link_fixture = b2FixtureDef( shape=b2PolygonShape(box=(LINK_WIDTH / 2, LINK_HEIGHT / 2)), density=1, friction=0.6, restitution=0.0, groupIndex=-1 # neg index to prevent collision ) for i in range(4): link_left = env_obj.world.CreateDynamicBody( position=(BOX_SIDE_OFFSET + (GAP * i + LINK_WIDTH / 2), 0), fixtures=link_fixture) link_left.color1 = (0, 1, 1) link_left.color2 = (1, 0, 1) env_obj.link_left_list.append(link_left) link_right = env_obj.world.CreateDynamicBody( position=(BOX_SIDE_OFFSET + (GAP * (i + 1) - LINK_WIDTH / 2), 0), fixtures=link_fixture) link_right.color1 = (0, 1, 1) link_right.color2 = (1, 0, 1) env_obj.link_right_list.append(link_right) joint_left = env_obj.world.CreateRevoluteJoint( bodyA=env_obj.actuator_list[i], bodyB=link_left, anchor=env_obj.actuator_list[i].worldCenter, collideConnected=False) joint_right = env_obj.world.CreateRevoluteJoint( bodyA=env_obj.actuator_list[i + 1], bodyB=link_right, anchor=env_obj.actuator_list[i + 1].worldCenter, collideConnected=False) joint_middle = env_obj.world.CreatePrismaticJoint( bodyA=link_left, bodyB=link_right, anchor=(link_right.position.x - (LINK_WIDTH / 2 + LINK_HEIGHT / 2), (link_left.position.y + link_right.position.y) / 2.0), axis=(1, 0), lowerTranslation=0, upperTranslation=UPPER_TRANSLATION_MIDDLE_JOINT, enableLimit=True) # Adding linkages to the drawlist env_obj.drawlist = env_obj.link_left_list + env_obj.link_right_list + env_obj.drawlist
def _reset(self): self._destroy() self.game_over = False # To be used later # Creating the Exterior Box that defines the 2D Plane self.exterior_box = self.world.CreateStaticBody( position=(0, 0), shapes=b2LoopShape(vertices=EXT_BOX_POLY)) self.exterior_box.color1 = (0, 0, 0) self.exterior_box.color2 = (0, 0, 0) # Creating the object to manipulate object_fixture = b2FixtureDef(shape=b2CircleShape(radius=BOX_WIDTH / 20), density=1, friction=0.6) self.object = self.world.CreateDynamicBody(position=(5, BOX_HEIGHT / 2), fixtures=object_fixture) self.object.at_target = False self.object.at_target_count = 0 self.object.color1 = (1, 1, 0) self.object.color2 = (0, 0, 0) # Creating 5 actuators self.actuator_list = [] self.actuator_joint_list = [] actuator_fixture = b2FixtureDef(shape=b2CircleShape(radius=BOX_WIDTH / 50.0), density=1, friction=0.6, groupIndex=-1) for i in range(5): actuator = self.world.CreateDynamicBody( position=(BOX_WIDTH / 5 * i + BOX_WIDTH / 10, BOX_HEIGHT / 10), fixtures=actuator_fixture) actuator.color1 = (1, 0, 0) actuator.color2 = (1, 0, 0) #SHOULD STAY COMMENTED # actuator = self.world.CreateKinematicBody( # position = (BOX_WIDTH/5*i+BOX_WIDTH/10, BOX_HEIGHT/10), # shapes = b2CircleShape(radius=BOX_WIDTH/50.0), # diameter is 1/5th of the space allocated for each actuator # ) actuator_joint = self.world.CreatePrismaticJoint( bodyA=self.exterior_box, bodyB=actuator, anchor=actuator.position, axis=(0, 1), lowerTranslation=0, upperTranslation=ACTUATOR_TRANSLATION_MAX, enableLimit=True, maxMotorForce=1000.0, motorSpeed=0, enableMotor=True) self.actuator_list.append(actuator) self.actuator_joint_list.append(actuator_joint) # Creating the linkages that will form the semi-flexible membrane self.link_left_list = [] # four links self.link_right_list = [] # four links link_fixture = b2FixtureDef( shape=b2PolygonShape(box=(BOX_WIDTH / 12, BOX_WIDTH / 70.0)), density=1, friction=0.6, groupIndex=-1 # neg index to prevent collision ) for i in range(1, 5): link_left = self.world.CreateDynamicBody( position=(BOX_WIDTH / 5 * i - BOX_WIDTH / 10 + BOX_WIDTH / 12, BOX_HEIGHT / 10), fixtures=link_fixture) link_left.color1 = (0, 1, 1) link_left.color2 = (1, 0, 1) self.link_left_list.append(link_left) link_right = self.world.CreateDynamicBody( position=(BOX_WIDTH / 5 * i + BOX_WIDTH / 10 - BOX_WIDTH / 12, BOX_HEIGHT / 10), fixtures=link_fixture) link_right.color1 = (0, 1, 1) link_right.color2 = (1, 0, 1) self.link_right_list.append(link_right) joint_left = self.world.CreateRevoluteJoint( bodyA=self.actuator_list[i - 1], bodyB=link_left, anchor=self.actuator_list[i - 1].worldCenter, collideConnected=False) joint_right = self.world.CreateRevoluteJoint( bodyA=self.actuator_list[i], bodyB=link_right, anchor=self.actuator_list[i].worldCenter, collideConnected=False) joint_middle = self.world.CreatePrismaticJoint( bodyA=link_left, bodyB=link_right, anchor=(link_right.position.x - BOX_WIDTH / 12 + BOX_WIDTH / 70, link_right.position.y), axis=(1, 0), lowerTranslation=0, upperTranslation=BOX_WIDTH / 6 - BOX_WIDTH / 35, enableLimit=True) self.drawlist = self.link_right_list + self.link_left_list + [ self.object ] self.drawlist = self.drawlist + self.actuator_list return self._step(np.array([0, 0, 0, 0, 0]))[0] # action: zero motor speed
def _reset(self): self._destroy() self.game_over = False # To be used later # Creating the Exterior Box that defines the 2D Plane exterior_box_fixture = b2FixtureDef( shape=b2LoopShape(vertices=EXT_BOX_POLY), categoryBits=0x0001, maskBits=0x0004 | 0x0001 | 0x0002) self.exterior_box = self.world.CreateStaticBody(position=(0, 0)) self.exterior_box.CreateFixture(exterior_box_fixture) self.exterior_box.color1 = (0, 0, 1) self.exterior_box.color2 = (0, 0, 0) # Creating the object to manipulate object_fixture = b2FixtureDef(shape=b2CircleShape(radius=BOX_WIDTH / 40), density=1, categoryBits=0x0002, maskBits=0x0004 | 0x0002 | 0x0001) self.object = self.world.CreateDynamicBody(position=(BOX_WIDTH / 2, BOX_HEIGHT / 2), fixtures=object_fixture, linearDamping=0.3) self.object.at_target = False self.object.at_target_count = 0 self.object.color1 = (1, 1, 0) self.object.color2 = (0, 0, 0) # Creating the actuator mount self.actuator_mount = self.world.CreateStaticBody( position=(-BOX_WIDTH / 50, BOX_HEIGHT / 2), shapes=b2PolygonShape(box=(BOX_WIDTH / 50, BOX_WIDTH / 30))) self.actuator_mount.color1 = (1, 0, 0) self.actuator_mount.color2 = (0, 0, 0) # Link Position Calculation adj = (BOX_WIDTH / 50 + TIP_POS[0]) / 4 hyp = (LINK_LENGTH / 2) opp = np.sqrt(hyp * hyp - adj * adj) # Creating the actuator first_link_fixture = b2FixtureDef( shape=b2PolygonShape(box=(LINK_LENGTH / 2, BOX_WIDTH / 70, (0, 0), math.tan(opp / adj))), density=1, categoryBits=0x0003, maskBits=0) second_link_fixture = b2FixtureDef( shape=b2PolygonShape(box=(LINK_LENGTH / 2, BOX_WIDTH / 70, (0, 0), math.pi - math.tan(opp / adj))), density=1, categoryBits=0x0003, maskBits=0) tip_fixture = b2FixtureDef(shape=b2CircleShape(radius=BOX_WIDTH / 50), density=1, categoryBits=0x0004, maskBits=0x0004 | 0x0002 | 0x0001) self.first_link = self.world.CreateDynamicBody( position=self.actuator_mount.position + (adj, opp), fixtures=first_link_fixture) self.first_link.color1 = (0, 0, 0) self.first_link.color2 = (0, 0, 0) self.first_link.joint = self.world.CreateRevoluteJoint( bodyA=self.actuator_mount, bodyB=self.first_link, anchor=self.actuator_mount.position, collideConnected=False, maxMotorTorque=MOTOR_TORQUE, motorSpeed=0, enableMotor=True) self.second_link = self.world.CreateDynamicBody( position=self.first_link.position + (adj * 2, 0), fixtures=second_link_fixture) self.second_link.color1 = (0, 0, 0) self.second_link.color2 = (0, 0, 0) self.second_link.joint = self.world.CreateRevoluteJoint( bodyA=self.first_link, bodyB=self.second_link, anchor=self.first_link.position + (adj, opp), collideConnected=False, maxMotorTorque=MOTOR_TORQUE, motorSpeed=0, enableMotor=True) self.tip = self.world.CreateDynamicBody( position=self.second_link.position + (adj, -opp), fixtures=tip_fixture) self.tip.color1 = (0, 1, 1) self.tip.color2 = (0, 0, 0) self.tip.joint = self.world.CreateWeldJoint(bodyA=self.tip, bodyB=self.second_link, anchor=self.tip.position) # Adding bodies to be rendered to the drawlist self.drawlist = [ self.first_link, self.second_link, self.object, self.tip ] return self._step(np.array([0, 0]))[0] # action: zero motor speed