def reset(self, world, init_x, init_y, noise): self.world = world self.hull.reset(world, init_x, init_y, noise) self.leg1.reset(world, init_x, init_y) self.leg2.reset(world, init_x, init_y) self.lidar.reset() self.joint1 = world.CreateJoint( revoluteJointDef(bodyA=self.hull.body, bodyB=self.leg1.top_body, localAnchorA=(0, self.leg1.leg_down), localAnchorB=(0, self.leg1.top_height / 2), enableMotor=True, enableLimit=True, maxMotorTorque=self.config.motors_torque, motorSpeed=-1.0, lowerAngle=-0.8, upperAngle=1.1)) self.joint2 = world.CreateJoint( revoluteJointDef(bodyA=self.hull.body, bodyB=self.leg2.top_body, localAnchorA=(0, self.leg2.leg_down), localAnchorB=(0, self.leg2.top_height / 2), enableMotor=True, enableLimit=True, maxMotorTorque=self.config.motors_torque, motorSpeed=1.0, lowerAngle=-0.8, upperAngle=1.1))
def define_legs(self): for i in [-1,+1]: leg = self.world.CreateDynamicBody( position = (self.init_x, self.init_y - self.leg_height/2 - self.leg_down), angle = (i*0.05), fixtures = fixtureDef( shape=polygonShape(box=(self.leg_width/2, self.leg_height/2)), density=1.0, restitution=0.0, categoryBits=0x0020, maskBits=0x001) ) leg.color1 = (0.6-i/10., 0.3-i/10., 0.5-i/10.) leg.color2 = (0.4-i/10., 0.2-i/10., 0.3-i/10.) rjd = revoluteJointDef( bodyA=self.hull, bodyB=leg, localAnchorA=(0, self.leg_down), localAnchorB=(0, self.leg_height/2), enableMotor=True, enableLimit=True, maxMotorTorque=self.motors_torque, motorSpeed = i, lowerAngle = -0.8, upperAngle = 1.1, ) self.legs.append(leg) self.joints.append(self.world.CreateJoint(rjd)) lower = self.world.CreateDynamicBody( position = (self.init_x, self.init_y - self.leg_height*3/2 - self.leg_down), angle = (i*0.05), fixtures = fixtureDef( shape=polygonShape(box=(0.8*self.leg_width/2, self.leg_height/2)), density=1.0, restitution=0.0, categoryBits=0x0020, maskBits=0x001) ) lower.color1 = (0.6-i/10., 0.3-i/10., 0.5-i/10.) lower.color2 = (0.4-i/10., 0.2-i/10., 0.3-i/10.) rjd = revoluteJointDef( bodyA=leg, bodyB=lower, localAnchorA=(0, -self.leg_height/2), localAnchorB=(0, self.leg_height/2), enableMotor=True, enableLimit=True, maxMotorTorque=self.motors_torque, motorSpeed = 1, lowerAngle = -1.6, upperAngle = -0.1, ) lower.ground_contact = False self.legs.append(lower) self.joints.append(self.world.CreateJoint(rjd))
def _createJoints(self): """Internal method to create joints. """ front_wheel_joint = wheelJointDef( bodyA=self.chassis, bodyB=self.front_wheel, localAnchorA=(1.096710562705994, -0.4867535233497620), localAnchorB=(-0.001052246429026127, 0.0007888938998803496), localAxisA=(-0.4521348178386688, 0.8919496536254883), frequencyHz=10.0, enableMotor=True, maxMotorTorque=20.0, dampingRatio=0.8) swingarm_revolute = revoluteJointDef( bodyA=self.chassis, bodyB=self.swingArm, localAnchorA=(-0.1697492003440857, -0.2360641807317734), localAnchorB=(0.3914504945278168, 0.1011910215020180), enableMotor=False, enableLimit=True, maxMotorTorque=9999999999.0, motorSpeed=0, lowerAngle=0.1, upperAngle=0.45) # distancejoint4 swingarm_distance = distanceJointDef( bodyA=self.chassis, bodyB=self.swingArm, localAnchorA=(-0.8274764418601990, 0.3798926770687103), localAnchorB=(-0.2273961603641510, -0.04680897668004036), length=0.8700000047683716, frequencyHz=8.0, dampingRatio=0.8999999761581421) # drivejoint swingarm_drive = revoluteJointDef( bodyA=self.swingArm, bodyB=self.rear_wheel, localAnchorA=(-0.3686238229274750, -0.07278718799352646), localAnchorB=(0, 0), enableMotor=False, enableLimit=False, maxMotorTorque=8.0, motorSpeed=0, lowerAngle=0, upperAngle=0, ) self.front_wheel_joint = self.world.CreateJoint(front_wheel_joint) self.swingarm_revolute = self.world.CreateJoint(swingarm_revolute) self.swingarm_distance = self.world.CreateJoint(swingarm_distance) self.swingarm_drive = self.world.CreateJoint(swingarm_drive)
def reset(self, world, init_x, init_y): self.top_body = world.CreateDynamicBody( position=(init_x, init_y - self.top_shift), angle=0.05 if self.left else -0.05, fixtures=self.top_fixture) self.bot_body = world.CreateDynamicBody( position=(init_x, init_y - self.bot_shift), angle=0.05 if self.left else -0.05, fixtures=self.bot_fixture) self.bot_body.ground_contact = False self.joint = world.CreateJoint( revoluteJointDef(bodyA=self.top_body, bodyB=self.bot_body, localAnchorA=(0, -self.top_height / 2), localAnchorB=(0, self.bot_height / 2), enableMotor=True, enableLimit=True, maxMotorTorque=self.motors_torque, motorSpeed=1, lowerAngle=-1.6, upperAngle=-0.1)) self.top_body.color1 = self.color self.top_body.color2 = Color.BLACK self.bot_body.color1 = self.color self.bot_body.color2 = Color.BLACK
def after_step_climbing_dynamics(self, contact_detector, world): ''' Add climbing joints if needed (i.e. objects are still overlapping after Box2D's solver execution) ''' for sensor in contact_detector.contact_dictionaries: if len(contact_detector.contact_dictionaries[sensor]) > 0 and \ sensor.userData.ready_to_attach and not sensor.userData.has_joint: other_body = contact_detector.contact_dictionaries[sensor][0] # Check if still overlapping after solver # Super coarse yet fast way, mainly useful for creepers other_body_shape = other_body.fixtures[0].shape x_values = [v[0] for v in other_body_shape.vertices] y_values = [v[1] for v in other_body_shape.vertices] radius = sensor.fixtures[0].shape.radius + 0.01 if sensor.worldCenter[0] + radius > min(x_values) and sensor.worldCenter[0] - radius < max(x_values) and \ sensor.worldCenter[1] + radius > min(y_values) and sensor.worldCenter[1] - radius < max(y_values): rjd = revoluteJointDef( bodyA=sensor, bodyB=other_body, anchor=sensor. worldCenter # contact.worldManifold.points[0], ) joint = world.CreateJoint(rjd) joint.bodyA.userData.joint = joint sensor.userData.has_joint = True else: contact_detector.contact_dictionaries[sensor].remove( other_body) if len(contact_detector.contact_dictionaries[sensor]) == 0: sensor.userData.has_contact = False
def create_sensors(self, init_angle, init_x, init_y, WHEEL_POLY): for wx, wy in SENSORPOS: #create sensors around car front_k = 1.0 if wy > 0 else 1.0 w = self.world.CreateDynamicBody( position=(init_x + wx * SIZE, init_y + wy * SIZE), angle=init_angle, fixtures=fixtureDef( shape=polygonShape(vertices=[(x * front_k * SIZE, y * front_k * SIZE) for x, y in WHEEL_POLY]), density=0.000000000000001, categoryBits=0x0020, maskBits=0x001, restitution=0.0)) w.color = (0, 0, 1) rjd = revoluteJointDef( #bind sensors to car (to move with the car, etc) bodyA=self.hull, bodyB=w, localAnchorA=(wx * SIZE, wy * SIZE), localAnchorB=(0, 0), enableMotor=False, enableLimit=True, maxMotorTorque=180 * 900 * SIZE * SIZE, motorSpeed=0, lowerAngle=-0.4, upperAngle=+0.4, ) w.joint = self.world.CreateJoint(rjd) w.tiles = set() w.userData = w self.sensors.append(w)
def create_joint(self,world, parent_component,new_component,connection_site,actuated =True): # First the local coordinates are calculated based on the absolute coordinates and angles of the parent, child and connection site disA = math.sqrt(math.pow(connection_site.position.x - parent_component.position.x,2)+math.pow(connection_site.position.y - parent_component.position.y,2)) local_anchor_a = [connection_site.position.x- parent_component.position[0], connection_site.position.y - parent_component.position[1],0] local_anchor_a[0] = math.cos(connection_site.orientation.x-parent_component.angle+math.pi/2)*disA; local_anchor_a[1] = math.sin(connection_site.orientation.x-parent_component.angle+math.pi/2)*disA; disB = math.sqrt(math.pow(connection_site.position.x - new_component.position.x,2)+math.pow(connection_site.position.y - new_component.position.y,2)) local_anchor_b = [new_component.position[0]-connection_site.position.x, new_component.position[1] - connection_site.position.y,0] local_anchor_b[0] = math.cos(new_component.angle-connection_site.orientation.x - math.pi/2)*disB; local_anchor_b[1] = math.sin(new_component.angle-connection_site.orientation.x - math.pi/2)*disB; if (actuated == True): rjd = revoluteJointDef( bodyA=parent_component, bodyB=new_component, localAnchorA=(local_anchor_a[0],local_anchor_a[1]), localAnchorB= (local_anchor_b[0],local_anchor_b[1]),# (connectionSite.a_b_pos.x,connectionSite.a_b_pos.y),# if (math.isclose(connectionSite.orientation.x,0.0)) else (connectionSite.a_b_pos.x,-connectionSite.a_b_pos.y), enableMotor=actuated, enableLimit=True, maxMotorTorque=self.torque, motorSpeed = 0.0, lowerAngle = -math.pi/2, upperAngle = math.pi/2, referenceAngle = connection_site.orientation.x - parent_component.angle #new_component.angle #connectionSite.orientation.x ) joint = world.CreateJoint(rjd) return joint;
def random_initial_position(env): env.reset() pos_x = VIEWPORT_H / SCALE * random.random() lander_angle = np.pi / 2 * random.random() - np.pi / 4 print(pos_x) env.lander = env.world.CreateDynamicBody( position=(pos_x, VIEWPORT_H / SCALE), angle=lander_angle, fixtures=fixtureDef( shape=polygonShape(vertices=[(x / SCALE, y / SCALE) for x, y in LANDER_POLY]), density=5.0, friction=0.1, categoryBits=0x0010, maskBits=0x001, # collide only with ground restitution=0.0) # 0.99 bouncy ) env.lander.color1 = (0.5, 0.4, 0.9) env.lander.color2 = (0.3, 0.3, 0.5) env.lander.ApplyForceToCenter( (env.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM), env.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM)), True) env.legs = [] for i in [-1, +1]: leg = env.world.CreateDynamicBody( position=(pos_x - i * LEG_AWAY / SCALE, VIEWPORT_H / SCALE), angle=lander_angle + (i * 0.05), fixtures=fixtureDef(shape=polygonShape(box=(LEG_W / SCALE, LEG_H / SCALE)), density=1.0, restitution=0.0, categoryBits=0x0020, maskBits=0x001)) leg.ground_contact = False leg.color1 = (0.5, 0.4, 0.9) leg.color2 = (0.3, 0.3, 0.5) rjd = revoluteJointDef( bodyA=env.lander, bodyB=leg, localAnchorA=(0, 0), localAnchorB=(i * LEG_AWAY / SCALE, LEG_DOWN / SCALE), enableMotor=True, enableLimit=True, maxMotorTorque=LEG_SPRING_TORQUE, motorSpeed=+0.3 * i # low enough not to jump back into the sky ) if i == -1: rjd.lowerAngle = +0.9 - 0.5 # Yes, the most esoteric numbers here, angles legs have freedom to travel within rjd.upperAngle = +0.9 else: rjd.lowerAngle = -0.9 rjd.upperAngle = -0.9 + 0.5 leg.joint = env.world.CreateJoint(rjd) env.legs.append(leg) env.drawlist = [env.lander] + env.legs
def __init__(self, world, init_angle, init_x, init_y, allow_reverse=False): self.world = world self.allow_reverse = allow_reverse self.hull = self.world.CreateDynamicBody( position = (init_x, init_y), angle = init_angle, fixtures = [ fixtureDef(shape = polygonShape(vertices=[ (x*SIZE,y*SIZE) for x,y in HULL_POLY1 ]), density=1.0), fixtureDef(shape = polygonShape(vertices=[ (x*SIZE,y*SIZE) for x,y in HULL_POLY2 ]), density=1.0), fixtureDef(shape = polygonShape(vertices=[ (x*SIZE,y*SIZE) for x,y in HULL_POLY3 ]), density=1.0), fixtureDef(shape = polygonShape(vertices=[ (x*SIZE,y*SIZE) for x,y in HULL_POLY4 ]), density=1.0) ] ) self.hull.color = (0.8,0.0,0.0) self.wheels = [] self.fuel_spent = 0.0 WHEEL_POLY = [ (-WHEEL_W,+WHEEL_R), (+WHEEL_W,+WHEEL_R), (+WHEEL_W,-WHEEL_R), (-WHEEL_W,-WHEEL_R) ] for wx,wy in WHEELPOS: front_k = 1.0 if wy > 0 else 1.0 w = self.world.CreateDynamicBody( position = (init_x+wx*SIZE, init_y+wy*SIZE), angle = init_angle, fixtures = fixtureDef( shape=polygonShape(vertices=[ (x*front_k*SIZE,y*front_k*SIZE) for x,y in WHEEL_POLY ]), density=0.1, categoryBits=0x0020, maskBits=0x001, restitution=0.0) ) w.wheel_rad = front_k*WHEEL_R*SIZE w.color = WHEEL_COLOR w.gas = 0.0 w.brake = 0.0 w.steer = 0.0 w.phase = 0.0 # wheel angle w.omega = 0.0 # angular velocity w.skid_start = None w.skid_particle = None rjd = revoluteJointDef( bodyA=self.hull, bodyB=w, localAnchorA=(wx*SIZE,wy*SIZE), localAnchorB=(0,0), enableMotor=True, enableLimit=True, maxMotorTorque=180*900*SIZE*SIZE, motorSpeed = 0, lowerAngle = -0.4, upperAngle = +0.4, ) w.joint = self.world.CreateJoint(rjd) w.tiles = set() w.userData = w self.wheels.append(w) self.drawlist = self.wheels + [self.hull] self.particles = []
def createJetModule(self, n, nodes, p_c): angle = p_c.angle c_angle = angle + (n.orientation.value[0] * n.module.angle) # get module height and width n_width = n.module.width n_height = n.module.height # get parent node par = None for parent in nodes: if parent.index == n.parent: par = parent # get parent module height p_h = n.module.height if (n.orientation != n.module.connection_type.top): p_h = moduleList[par.type].width pos = [] pos.append( math.sin(c_angle) * (n_height + p_h) * 0.25 + components[compIndex].position[0]) pos.append( math.cos(c_angle) * (n_height + p_h) * 0.25 + components[compIndex].position[1]) fixture = fixtureDef(shape=polygonShape(box=(n_width * MODULE_W, n_height * MODULE_H)), density=1.0, friction=0.1, restitution=0.0, categoryBits=0x0020, maskBits=0x001) ncomponent = self.world.CreateDynamicBody(position=(pos[0], pos[1]), angle=c_angle, fixtures=fixture) color = cmap(n.type / 5) ncomponent.color1 = (color[0], color[1], color[2]) ncomponent.color2 = (color[0], color[1], color[2]) components.append(ncomponent) n.component = ncomponent n.type = "JET" jointPosition = [] jointPosition.append(pos[0] - components[compIndex].position[0]) jointPosition.append(pos[1] - components[compIndex].position[1]) # TODO joint has no local coordinate system rjd = revoluteJointDef(bodyA=components[compIndex], bodyB=ncomponent, localAnchorA=(jointPosition[0] / 2, jointPosition[1] / 2), localAnchorB=(-jointPosition[0] / 2, -jointPosition[1] / 2), enableMotor=True, enableLimit=True, maxMotorTorque=MOTORS_TORQUE, motorSpeed=0, lowerAngle=-math.pi / 2, upperAngle=math.pi / 2, referenceAngle=0) self.joints.append(self.world.CreateJoint(rjd))
def __init__(self, world, init_angle, init_x, init_y): self.world = world self.hull = self.world.CreateDynamicBody( position = (init_x, init_y), angle = init_angle, fixtures = [ fixtureDef(shape = polygonShape(vertices=[ (x*SIZE,y*SIZE) for x,y in HULL_POLY1 ]), density=1.0), fixtureDef(shape = polygonShape(vertices=[ (x*SIZE,y*SIZE) for x,y in HULL_POLY2 ]), density=1.0), fixtureDef(shape = polygonShape(vertices=[ (x*SIZE,y*SIZE) for x,y in HULL_POLY3 ]), density=1.0), fixtureDef(shape = polygonShape(vertices=[ (x*SIZE,y*SIZE) for x,y in HULL_POLY4 ]), density=1.0) ] ) self.hull.color = (0.8,0.0,0.0) self.wheels = [] self.fuel_spent = 0.0 WHEEL_POLY = [ (-WHEEL_W,+WHEEL_R), (+WHEEL_W,+WHEEL_R), (+WHEEL_W,-WHEEL_R), (-WHEEL_W,-WHEEL_R) ] for wx,wy in WHEELPOS: front_k = 1.0 if wy > 0 else 1.0 w = self.world.CreateDynamicBody( position = (init_x+wx*SIZE, init_y+wy*SIZE), angle = init_angle, fixtures = fixtureDef( shape=polygonShape(vertices=[ (x*front_k*SIZE,y*front_k*SIZE) for x,y in WHEEL_POLY ]), density=0.1, categoryBits=0x0020, maskBits=0x001, restitution=0.0) ) w.wheel_rad = front_k*WHEEL_R*SIZE w.color = WHEEL_COLOR w.gas = 0.0 w.brake = 0.0 w.steer = 0.0 w.phase = 0.0 # wheel angle w.omega = 0.0 # angular velocity w.skid_start = None w.skid_particle = None rjd = revoluteJointDef( bodyA=self.hull, bodyB=w, localAnchorA=(wx*SIZE,wy*SIZE), localAnchorB=(0,0), enableMotor=True, enableLimit=True, maxMotorTorque=180*900*SIZE*SIZE, motorSpeed = 0, lowerAngle = -0.4, upperAngle = +0.4, ) w.joint = self.world.CreateJoint(rjd) w.tiles = set() w.userData = w self.wheels.append(w) self.drawlist = self.wheels + [self.hull] self.particles = []
def create_joint(self, parent_component, new_component, connection_site, actuated=True): # First the local coordinates are calculated based on the absolute coordinates and angles of the parent, child and connection site disA = math.sqrt( math.pow(connection_site.position.x - parent_component.position.x, 2) + math.pow(connection_site.position.y - parent_component.position.y, 2)) local_anchor_a = Vector3( connection_site.position.x - parent_component.position.x, connection_site.position.y - parent_component.position.y, 0) local_anchor_a.x = math.cos(connection_site.orientation.x - parent_component.angle + math.pi / 2) * disA local_anchor_a.y = math.sin(connection_site.orientation.x - parent_component.angle + math.pi / 2) * disA disB = math.sqrt( math.pow(connection_site.position.x - new_component.position.x, 2) + math.pow(connection_site.position.y - new_component.position.y, 2)) local_anchor_b = Vector3( new_component.position.x - connection_site.position.x, new_component.position.y - connection_site.position.y, 0) local_anchor_b.x = math.cos(new_component.angle - connection_site.orientation.x - math.pi / 2) * disB local_anchor_b.y = math.sin(new_component.angle - connection_site.orientation.x - math.pi / 2) * disB if (actuated == True): rjd = revoluteJointDef( bodyA=parent_component, bodyB=new_component, localAnchorA=(local_anchor_a.x, local_anchor_a.y), localAnchorB=( local_anchor_b.x, local_anchor_b.y ), # (connectionSite.a_b_pos.x,connectionSite.a_b_pos.y),# if (math.isclose(connectionSite.orientation.x,0.0)) else (connectionSite.a_b_pos.x,-connectionSite.a_b_pos.y), enableMotor=actuated, enableLimit=True, maxMotorTorque=MOTORS_TORQUE, motorSpeed=0.0, lowerAngle=-math.pi / 2, upperAngle=math.pi / 2, referenceAngle= 0.0 #new_component.angle #connectionSite.orientation.x ) joint = self.world.CreateJoint(rjd) return joint return rjd return
def _create_lander(self, lander, number_of_landers): initial_y = VIEWPORT_H/SCALE body_xpos = (lander.player_id + 1) * VIEWPORT_W/SCALE / (number_of_landers + 1) body_ypos = initial_y body = self.world.CreateDynamicBody( position = (body_xpos, body_ypos), angle=0.0, fixtures = fixtureDef( shape=polygonShape(vertices=[ (x/SCALE,y/SCALE) for x,y in LANDER_POLY ]), density=5.0, friction=0.1, categoryBits=0x0010, maskBits=0x001, # collide only with ground restitution=0.0) # 0.99 bouncy ) body.color1 = (0.5,0.4,0.9) body.color2 = (0.3,0.3,0.5) body.ApplyForceToCenter( ( self.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM), self.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM) ), True) legs = [] for i in [-1,+1]: leg = self.world.CreateDynamicBody( position = (body_xpos - i*LEG_AWAY/SCALE, body_ypos), angle = (i*0.05), fixtures = fixtureDef( shape=polygonShape(box=(LEG_W/SCALE, LEG_H/SCALE)), density=1.0, restitution=0.0, categoryBits=0x0020, maskBits=0x001) ) leg.ground_contact = False leg.color1 = (0.5,0.4,0.9) leg.color2 = (0.3,0.3,0.5) rjd = revoluteJointDef( bodyA=body, bodyB=leg, localAnchorA=(0, 0), localAnchorB=(i*LEG_AWAY/SCALE, LEG_DOWN/SCALE), enableMotor=True, enableLimit=True, maxMotorTorque=LEG_SPRING_TORQUE, motorSpeed=+0.3*i # low enough not to jump back into the sky ) if i==-1: rjd.lowerAngle = +0.9 - 0.5 # Yes, the most esoteric numbers here, angles legs have freedom to travel within rjd.upperAngle = +0.9 else: rjd.lowerAngle = -0.9 rjd.upperAngle = -0.9 + 0.5 leg.joint = self.world.CreateJoint(rjd) legs.append(leg) lander.body, lander.legs = body, legs
def make_wheels(hull, world): wheels = [] WHEEL_POLY = [ (-WHEEL_W, +WHEEL_R), (+WHEEL_W, +WHEEL_R), (+WHEEL_W, -WHEEL_R), (-WHEEL_W, -WHEEL_R) ] for wx, wy in WHEELPOS: front_k = 1.0 if wy > 0 else 1.0 w = world.CreateDynamicBody( fixtures=fixtureDef( shape=polygonShape(vertices=[(x * front_k * SIZE, y * front_k * SIZE) for x, y in WHEEL_POLY]), density=0.1, categoryBits=0x0020, maskBits=0x001, restitution=0.0) ) w.wheel_rad = front_k * WHEEL_R * SIZE w.color = WHEEL_COLOR w.gas = 0.0 w.brake = 0.0 w.steer = 0.0 w.phase = 0.0 # wheel angle w.omega = 0.0 # angular velocity w.skid_start = None w.skid_particle = None rjd = revoluteJointDef( bodyA=hull, bodyB=w, localAnchorA=(wx * SIZE, wy * SIZE), localAnchorB=(0, 0), enableMotor=True, enableLimit=True, maxMotorTorque=180 * 900 * SIZE * SIZE, motorSpeed=0, lowerAngle=-0.4, upperAngle=+0.4, ) w.joint = world.CreateJoint(rjd) w.tiles = set() w.userData = w wheels.append(w) return wheels
def __init__(self, world, init_angle, init_x, init_y): self.world = world self.hull = self.world.CreateDynamicBody(position=(init_x, init_y), angle=init_angle, fixtures=HULL_FD) self.hull.color = (0.8, 0.0, 0.0) self.wheels = [] self.fuel_spent = 0.0 for wx, wy in WHEELPOS: w = self.world.CreateDynamicBody(position=(init_x + wx * SIZE, init_y + wy * SIZE), angle=init_angle, fixtures=WHEEL_FD) w.wheel_rad = WHEEL_R * SIZE w.color = WHEEL_COLOR w.gas = 0.0 w.brake = 0.0 w.steer = 0.0 w.phase = 0.0 # wheel angle w.omega = 0.0 # angular velocity w.skid_start = None w.skid_particle = None rjd = revoluteJointDef( bodyA=self.hull, bodyB=w, localAnchorA=(wx * SIZE, wy * SIZE), localAnchorB=(0, 0), enableMotor=True, enableLimit=True, maxMotorTorque=180 * 900 * SIZE * SIZE, motorSpeed=0, lowerAngle=-0.4, upperAngle=+0.4, ) w.joint = self.world.CreateJoint(rjd) w.tiles = set() w.userData = w self.wheels.append(w) self.drawlist = self.wheels + [self.hull] self.particles = []
def generate_landing_legs(world, W, H, booster): legs = [] for i in [-1, +1]: leg = world.CreateDynamicBody( position=(W/2- i * config.LEG_AWAY, H-60), angle=(i * 0.05), fixtures=fixtureDef( shape=polygonShape(box=(config.LEG_W, config.LEG_H)), density=75, friction=1, restitution=0.0, categoryBits=0x0020, maskBits=0x001) ) leg.ground_contact = False leg.color1 = (1, 1, 1) leg.color2 = (0.35, 0.35, 0.35) rjd = revoluteJointDef( bodyA=booster, bodyB=leg, localAnchorA=(0, 0), localAnchorB=(i * config.LEG_AWAY, config.LEG_DOWN), enableMotor=True, enableLimit=True, maxMotorTorque=config.LEG_SPRING_TORQUE, motorSpeed=+0.1 * i # low enough not to jump back into the sky ) if i == -1: rjd.lowerAngle = +0.4 # Yes, the most esoteric numbers here, angles legs have freedom to travel within rjd.upperAngle = +0.65 else: rjd.lowerAngle = -0.65 rjd.upperAngle = -0.4 leg.joint = world.CreateJoint(rjd) legs.append(leg) return legs
def __init__( self, world: Box2D, bot: bool = False, car_image=None, track=None, data_loader=None, ): """ Constructor to define Car. Parameters ---------- world : Box2D World """ global SIZE self.car_image = car_image self.track = track self.data_loader = data_loader self.is_bot = bot self._bot_state = { 'was_break': False, 'stop_for_next': -1, } # all coordinates in XY format, not in IMAGE coordinates init_x, init_y = DataSupporter.get_track_initial_position(self.track['line']) init_angle = DataSupporter.get_track_angle(track) - np.pi / 2 width_y, height_x = self.car_image.size CAR_HULL_POLY4 = [ (-height_x / 2, -width_y / 2), (+height_x / 2, -width_y / 2), (-height_x / 2, +width_y / 2), (+height_x / 2, +width_y / 2) ] N_SENSOR_BOT = [ (-height_x / 2 * 1.11, +width_y / 2 * 1.0), (+height_x / 2 * 1.11, +width_y / 2 * 1.0), # (-height_x/2*1.11, +width_y/2*1.11), (+height_x/2*1.11, +width_y/2*1.11), (-height_x / 2 * 0.8, +width_y / 2 * 3), (+height_x / 2 * 0.8, +width_y / 2 * 3) # (-height_x/2*0.22, +width_y/2*2), (+height_x/2*0.22, +width_y/2*2) ] WHEELPOS = [ (-height_x / 2, +width_y / 2 / 2), (+height_x / 2, +width_y / 2 / 2), (-height_x / 2, -width_y / 2 / 2), (+height_x / 2, -width_y / 2 / 2) ] LEFT_SENSOR = [ (0, 0), (+height_x, 0), (0, +width_y * 1.5), (+height_x, +width_y * 1.5) ] RIGHT_SENSOR = [ (-height_x, 0), (0, 0), (-height_x, +width_y * 1.5), (0, +width_y * 1.5) ] N_SENSOR_SHAPE = CAR_HULL_POLY4 SENSOR = N_SENSOR_BOT if bot else N_SENSOR_SHAPE self.world = world # SIZE *= 0.5 self._hull = self.world.CreateDynamicBody( position=(init_x, init_y), angle=init_angle, fixtures=[ fixtureDef(shape=polygonShape( vertices=[(x * SIZE, y * SIZE) for x, y in CAR_HULL_POLY4]), density=1.0, userData='body', ), fixtureDef(shape=polygonShape( vertices=[(x * SIZE, y * SIZE) for x, y in SENSOR]), isSensor=True, userData='sensor', ), fixtureDef(shape=polygonShape( vertices=[(x * SIZE, y * SIZE) for x, y in RIGHT_SENSOR]), isSensor=True, userData='right_sensor', ), fixtureDef(shape=polygonShape( vertices=[(x * SIZE, y * SIZE) for x, y in LEFT_SENSOR]), isSensor=True, userData='left_sensor', ), ] ) # SIZE *= 2 self._hull.name = 'bot_car' if bot else 'car' self._hull.cross_time = float('inf') self._hull.stop = False self._hull.left_sensor = False self._hull.right_sensor = False self._hull.collision = False self._hull.userData = self._hull self.wheels = [] self.fuel_spent = 0.0 WHEEL_POLY = [ (-WHEEL_W, +WHEEL_R), (+WHEEL_W, +WHEEL_R), (+WHEEL_W, -WHEEL_R), (-WHEEL_W, -WHEEL_R) ] for wx, wy in WHEELPOS: front_k = 1.0 if wy > 0 else 1.0 w = self.world.CreateDynamicBody( position=(init_x + wx * SIZE, init_y + wy * SIZE), angle=init_angle, fixtures=fixtureDef( shape=polygonShape(vertices=[(x * front_k * SIZE, y * front_k * SIZE) for x, y in WHEEL_POLY]), density=0.1, categoryBits=0x0020, maskBits=0x001, restitution=0.0) ) w.wheel_rad = front_k * WHEEL_R * SIZE w.is_front = front_k w.gas = 0.0 w.brake = 0.0 w.steer = 0.0 w.phase = 0.0 # wheel angle w.omega = 0.0 # angular velocity rjd = revoluteJointDef( bodyA=self._hull, bodyB=w, localAnchorA=(wx * SIZE, wy * SIZE), localAnchorB=(0, 0), enableMotor=True, enableLimit=True, maxMotorTorque=180 * 900 * SIZE * SIZE, motorSpeed=0, lowerAngle=-0.4, upperAngle=+0.4, ) w.joint = self.world.CreateJoint(rjd) w.tiles = set() w.name = 'wheel' w.collision = False w.penalty = False w.userData = w self.wheels.append(w) self.drawlist = self.wheels + [self._hull] self.target = (0, 0) self._time: int = 0 self.userData = self self._track_point: int = 0 self._old_track_point: int = 0 self._state_data = None self._last_action = [0, 0, 0] self._flush_stats()
def _reset(self): self._destroy() self.world.contactListener = ContactDetector(self) self.game_over = False self.prev_shaping = None self.scroll = 0.0 self.lidar_render = 0 W = VIEWPORT_W/SCALE H = VIEWPORT_H/SCALE self._generate_terrain(self.hardcore) self._generate_clouds() init_x = TERRAIN_STEP*TERRAIN_STARTPAD/2 init_y = TERRAIN_HEIGHT+2*LEG_H self.hull = self.world.CreateDynamicBody( position = (init_x, init_y), fixtures = fixtureDef( shape=polygonShape(vertices=[ (x/SCALE,y/SCALE) for x,y in HULL_POLY ]), density=5.0, friction=0.1, categoryBits=0x0020, maskBits=0x001, # collide only with ground restitution=0.0) # 0.99 bouncy ) self.hull.color1 = (0.5,0.4,0.9) self.hull.color2 = (0.3,0.3,0.5) self.hull.ApplyForceToCenter((np.random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM), 0), True) self.legs = [] self.joints = [] for i in [-1,+1]: leg = self.world.CreateDynamicBody( position = (init_x, init_y - LEG_H/2 - LEG_DOWN), angle = (i*0.05), fixtures = fixtureDef( shape=polygonShape(box=(LEG_W/2, LEG_H/2)), density=1.0, restitution=0.0, categoryBits=0x0020, maskBits=0x001) ) leg.color1 = (0.6-i/10., 0.3-i/10., 0.5-i/10.) leg.color2 = (0.4-i/10., 0.2-i/10., 0.3-i/10.) rjd = revoluteJointDef( bodyA=self.hull, bodyB=leg, localAnchorA=(0, LEG_DOWN), localAnchorB=(0, LEG_H/2), enableMotor=True, enableLimit=True, maxMotorTorque=MOTORS_TORQUE, motorSpeed = i, lowerAngle = -0.8, upperAngle = 1.1, ) self.legs.append(leg) self.joints.append(self.world.CreateJoint(rjd)) lower = self.world.CreateDynamicBody( position = (init_x, init_y - LEG_H*3/2 - LEG_DOWN), angle = (i*0.05), fixtures = fixtureDef( shape=polygonShape(box=(0.8*LEG_W/2, LEG_H/2)), density=1.0, restitution=0.0, categoryBits=0x0020, maskBits=0x001) ) lower.color1 = (0.6-i/10., 0.3-i/10., 0.5-i/10.) lower.color2 = (0.4-i/10., 0.2-i/10., 0.3-i/10.) rjd = revoluteJointDef( bodyA=leg, bodyB=lower, localAnchorA=(0, -LEG_H/2), localAnchorB=(0, LEG_H/2), enableMotor=True, enableLimit=True, maxMotorTorque=MOTORS_TORQUE, motorSpeed = 1, lowerAngle = -1.6, upperAngle = -0.1, ) lower.ground_contact = False self.legs.append(lower) self.joints.append(self.world.CreateJoint(rjd)) self.drawlist = self.terrain + self.legs + [self.hull] class LidarCallback(Box2D.b2.rayCastCallback): def ReportFixture(self, fixture, point, normal, fraction): if (fixture.filterData.categoryBits & 1) == 0: return 1 self.p2 = point self.fraction = fraction return 0 self.lidar = [LidarCallback() for _ in range(10)] return self._step(np.array([0,0,0,0]))[0]
def _generate_terrain(self): y = self.terrain_CPPN.generate(self.CPPN_input_vector) y = y / self.TERRAIN_CPPN_SCALE ground_y = y[:, 0] ceiling_y = y[:, 1] # Align ground with startpad offset = TERRAIN_HEIGHT - ground_y[0] ground_y = np.add(ground_y, offset) # Align ceiling from startpad ceiling offset = TERRAIN_HEIGHT + self.ceiling_offset - ceiling_y[0] ceiling_y = np.add(ceiling_y, offset) self.terrain = [] self.terrain_x = [] self.terrain_ground_y = [] self.terrain_ceiling_y = [] terrain_creepers = [] water_body = None x = 0 max_x = TERRAIN_LENGTH * TERRAIN_STEP + self.TERRAIN_STARTPAD * TERRAIN_STEP # Generation of terrain i = 0 while x < max_x: self.terrain_x.append(x) if i < self.TERRAIN_STARTPAD: self.terrain_ground_y.append(TERRAIN_HEIGHT) self.terrain_ceiling_y.append(TERRAIN_HEIGHT + self.ceiling_offset) else: self.terrain_ground_y.append( ground_y[i - self.TERRAIN_STARTPAD].item()) # Clip ceiling if ceiling_y[i - self.TERRAIN_STARTPAD] >= ground_y[ i - self.TERRAIN_STARTPAD] + self.ceiling_clip_offset: ceiling_val = ceiling_y[i - self.TERRAIN_STARTPAD] else: ceiling_val = ground_y[ i - self.TERRAIN_STARTPAD] + self.ceiling_clip_offset self.terrain_ceiling_y.append(ceiling_val.item()) x += TERRAIN_STEP i += 1 # Draw terrain space_from_precedent_creeper = self.creepers_spacing self.terrain_poly = [] for i in range(len(self.terrain_x) - 1): # Ground poly = [(self.terrain_x[i], self.terrain_ground_y[i]), (self.terrain_x[i + 1], self.terrain_ground_y[i + 1])] self.fd_edge.shape.vertices = poly t = self.world.CreateStaticBody( fixtures=self.fd_edge, userData=CustomUserData("grass", CustomUserDataObjectTypes.TERRAIN)) color = (0.3, 1.0 if (i % 2) == 0 else 0.8, 0.3) t.color1 = color t.color2 = color self.terrain.append(t) color = (0.4, 0.6, 0.3) poly += [(poly[1][0], self.GROUND_LIMIT), (poly[0][0], self.GROUND_LIMIT)] self.terrain_poly.append((poly, color)) # Ceiling poly = [(self.terrain_x[i], self.terrain_ceiling_y[i]), (self.terrain_x[i + 1], self.terrain_ceiling_y[i + 1])] self.fd_edge.shape.vertices = poly t = self.world.CreateStaticBody( fixtures=self.fd_edge, userData=CustomUserData( "rock", CustomUserDataObjectTypes.GRIP_TERRAIN)) color = (0, 0.25, 0.25) t.color1 = color t.color2 = color self.terrain.append(t) color = (0.5, 0.5, 0.5) poly += [(poly[1][0], self.CEILING_LIMIT), (poly[0][0], self.CEILING_LIMIT)] self.terrain_poly.append((poly, color)) # Creepers if self.creepers_width is not None and self.creepers_height is not None: if space_from_precedent_creeper >= self.creepers_spacing: creeper_height = max( 0.2, self.np_random.normal(self.creepers_height, 0.1)) creeper_width = max(0.2, self.creepers_width) creeper_step_size = np.floor(creeper_width / TERRAIN_STEP).astype(int) creeper_step_size = max(1, creeper_step_size) creeper_y_init_pos = max( self.terrain_ceiling_y[i], self.terrain_ceiling_y[min(i + creeper_step_size, len(self.terrain_x) - 1)]) if self.movable_creepers: # Break down creepers into multiple objects linked by joints previous_creeper_part = t for w in range(math.ceil(creeper_height)): if w == creeper_height // 1: h = max(0.2, creeper_height % 1) else: h = 1 poly = [(self.terrain_x[i] + creeper_width, creeper_y_init_pos - (w * 1) - h), (self.terrain_x[i] + creeper_width, creeper_y_init_pos - (w * 1)), (self.terrain_x[i], creeper_y_init_pos - (w * 1)), (self.terrain_x[i], creeper_y_init_pos - (w * 1) - h)] self.fd_creeper.shape.vertices = poly t = self.world.CreateDynamicBody( fixtures=self.fd_creeper, userData=CustomUserData( "creeper", CustomUserDataObjectTypes. SENSOR_GRIP_TERRAIN)) c = (0.437, 0.504, 0.375) t.color1 = c t.color2 = tuple([_c + 0.1 for _c in c]) self.terrain.append(t) rjd = revoluteJointDef( bodyA=previous_creeper_part, bodyB=t, anchor=(self.terrain_x[i] + creeper_width / 2, creeper_y_init_pos - (w * 1)), enableLimit=True, lowerAngle=-0.4 * np.pi, upperAngle=0.4 * np.pi, ) self.world.CreateJoint(rjd) previous_creeper_part = t else: poly = [ (self.terrain_x[i], creeper_y_init_pos), (self.terrain_x[i] + creeper_width, creeper_y_init_pos), (self.terrain_x[i] + creeper_width, creeper_y_init_pos - creeper_height), (self.terrain_x[i], creeper_y_init_pos - creeper_height), ] self.fd_creeper.shape.vertices = poly t = self.world.CreateStaticBody( fixtures=self.fd_creeper, userData=CustomUserData( "creeper", CustomUserDataObjectTypes.SENSOR_GRIP_TERRAIN)) c = (0.437, 0.504, 0.375) t.color1 = c t.color2 = c terrain_creepers.append(t) space_from_precedent_creeper = 0 else: space_from_precedent_creeper += self.terrain_x[ i] - self.terrain_x[i - 1] # Water # Fill water from GROUND_LIMIT to highest point of the current ceiling air_max_distance = max(self.terrain_ceiling_y) - self.GROUND_LIMIT water_y = self.GROUND_LIMIT + self.water_level * air_max_distance self.water_y = water_y water_poly = [(self.terrain_x[0], self.GROUND_LIMIT), (self.terrain_x[0], water_y), (self.terrain_x[len(self.terrain_x) - 1], water_y), (self.terrain_x[len(self.terrain_x) - 1], self.GROUND_LIMIT)] self.fd_water.shape.vertices = water_poly t = self.world.CreateStaticBody(fixtures=self.fd_water, userData=CustomUserData( "water", CustomUserDataObjectTypes.WATER)) c = (0.465, 0.676, 0.898) t.color1 = c t.color2 = c water_body = t self.terrain.extend(terrain_creepers) if water_body is not None: self.terrain.append(water_body) self.terrain.reverse()
def draw(self, world, init_x, init_y, force_to_center): head = world.CreateDynamicBody( position=(init_x, init_y + self.BODY_HEIGHT / self.SCALE / 2 + self.HEAD_HEIGHT / self.SCALE / 2 + 0.2), fixtures=fixtureDef(shape=polygonShape( vertices=[(x / self.SCALE, y / self.SCALE) for x, y in [(-5, +10), (+5, +10), (+5, -10), (-5, -10)]]), density=5.0, friction=0.1, categoryBits=0x20, maskBits=0x1)) head.color1 = (0.5, 0.4, 0.9) head.color2 = (0.3, 0.3, 0.5) head.ApplyForceToCenter((force_to_center, 0), True) head.userData = CustomBodyUserData(True, is_contact_critical=True, name="head") self.body_parts.append(head) self.reference_head_object = head body = world.CreateDynamicBody( position=(init_x, init_y), fixtures=fixtureDef( shape=polygonShape( vertices=[(x / self.SCALE, y / self.SCALE) for x, y in [(-12, +25), (+12, +25), (+8, -20), (-8, -20)]]), density=5.0, friction=0.1, categoryBits=0x20, maskBits=0x1 # collide only with ground )) body.color1 = (0.5, 0.4, 0.9) body.color2 = (0.3, 0.3, 0.5) body.userData = CustomBodyUserData( True, is_contact_critical=self.reference_head_object, name="body") self.body_parts.append(body) rjd = revoluteJointDef( bodyA=head, bodyB=body, anchor=(init_x, init_y + self.BODY_HEIGHT / self.SCALE / 2), enableMotor=False, enableLimit=True, lowerAngle=-0.1 * np.pi, upperAngle=0.1 * np.pi, ) world.CreateJoint(rjd) UPPER_LIMB_FD = fixtureDef(shape=polygonShape(box=(self.LIMB_W / 2, self.LIMB_H / 2)), density=1.0, restitution=0.0, categoryBits=0x20, maskBits=0x1) LOWER_LIMB_FD = fixtureDef( shape=polygonShape(box=(0.8 * self.LIMB_W / 2, self.LIMB_H / 2)), density=1.0, restitution=0.0, categoryBits=0x20, maskBits=0x1) HAND_PART_FD = fixtureDef( shape=polygonShape(box=(self.HAND_PART_W / 2, self.HAND_PART_H / 2)), density=1.0, restitution=0.0, categoryBits=0x20, maskBits=0x1) # LEGS for i in [+1, +1]: upper = world.CreateDynamicBody( position=(init_x, init_y - self.LIMB_H / 2 - self.LEG_DOWN), # angle=(i * 0.05), fixtures=UPPER_LIMB_FD) upper.color1 = (0.6 - i / 10., 0.3 - i / 10., 0.5 - i / 10.) upper.color2 = (0.4 - i / 10., 0.2 - i / 10., 0.3 - i / 10.) rjd = revoluteJointDef( bodyA=body, bodyB=upper, anchor=(init_x, init_y - self.LEG_DOWN), enableMotor=True, enableLimit=True, maxMotorTorque=self.MOTORS_TORQUE, motorSpeed=1, lowerAngle=-0.3 * np.pi, upperAngle=0.6 * np.pi, ) upper.userData = CustomBodyUserData(False, name="upper_leg") self.body_parts.append(upper) joint_motor = world.CreateJoint(rjd) joint_motor.userData = CustomMotorUserData(SPEED_HIP, False) self.motors.append(joint_motor) lower = world.CreateDynamicBody( position=(init_x, init_y - self.LIMB_H * 3 / 2 - self.LEG_DOWN), # angle=(i * 0.05), fixtures=LOWER_LIMB_FD) lower.color1 = (0.6 - i / 10., 0.3 - i / 10., 0.5 - i / 10.) lower.color2 = (0.4 - i / 10., 0.2 - i / 10., 0.3 - i / 10.) rjd = revoluteJointDef( bodyA=upper, bodyB=lower, anchor=(init_x, init_y - self.LIMB_H - self.LEG_DOWN), enableMotor=True, enableLimit=True, maxMotorTorque=self.MOTORS_TORQUE, motorSpeed=1, lowerAngle=-0.75 * np.pi, upperAngle=-0.1, ) lower.userData = CustomBodyUserData(True, name="lower_leg") self.body_parts.append(lower) joint_motor = world.CreateJoint(rjd) joint_motor.userData = CustomMotorUserData(SPEED_KNEE, True, contact_body=lower, angle_correction=1.0) self.motors.append(joint_motor) # ARMS for j in [-1, -1]: upper = world.CreateDynamicBody( position=(init_x, init_y - self.LIMB_H / 2 + self.ARM_UP), # angle=(i * 0.05), fixtures=UPPER_LIMB_FD) upper.color1 = (0.6 - j / 10., 0.3 - j / 10., 0.5 - j / 10.) upper.color2 = (0.4 - j / 10., 0.2 - j / 10., 0.3 - j / 10.) rjd = revoluteJointDef( bodyA=body, bodyB=upper, anchor=(init_x, init_y + self.ARM_UP), enableMotor=True, enableLimit=True, maxMotorTorque=self.MOTORS_TORQUE, motorSpeed=1, lowerAngle=-0.5 * np.pi, upperAngle=0.8 * np.pi, ) upper.userData = CustomBodyUserData(False, name="upper_arm") self.body_parts.append(upper) joint_motor = world.CreateJoint(rjd) joint_motor.userData = CustomMotorUserData(SPEED_HIP, False) self.motors.append(joint_motor) lower = world.CreateDynamicBody( position=(init_x, init_y - self.LIMB_H * 3 / 2 + self.ARM_UP), # angle=(i * 0.05), fixtures=LOWER_LIMB_FD) lower.color1 = (0.6 - j / 10., 0.3 - j / 10., 0.5 - j / 10.) lower.color2 = (0.4 - j / 10., 0.2 - j / 10., 0.3 - j / 10.) rjd = revoluteJointDef( bodyA=upper, bodyB=lower, anchor=(init_x, init_y - self.LIMB_H + self.ARM_UP), enableMotor=True, enableLimit=True, maxMotorTorque=self.MOTORS_TORQUE, motorSpeed=1, lowerAngle=0, upperAngle=0.75 * np.pi, ) lower.userData = CustomBodyUserData(False, name="lower_arm") self.body_parts.append(lower) joint_motor = world.CreateJoint(rjd) joint_motor.userData = CustomMotorUserData(SPEED_HIP, False) self.motors.append(joint_motor) # hand prev_part = lower initial_y = init_y - self.LIMB_H * 2 + self.ARM_UP angle_boundaries = [[-0.4, 0.35], [-0.5, 0], [-0.8, 0]] nb_hand_parts = 3 for u in range(nb_hand_parts): hand_part = world.CreateDynamicBody( position=(init_x, initial_y - self.HAND_PART_H / 2 - self.HAND_PART_H * u), fixtures=HAND_PART_FD) hand_part.color1 = (0.6 - j / 10., 0.3 - j / 10., 0.5 - j / 10.) hand_part.color2 = (0.4 - j / 10., 0.2 - j / 10., 0.3 - j / 10.) rjd = revoluteJointDef( bodyA=prev_part, bodyB=hand_part, anchor=(init_x, initial_y - self.HAND_PART_H * u), enableMotor=True, enableLimit=True, maxMotorTorque=self.MOTORS_TORQUE, motorSpeed=1, lowerAngle=angle_boundaries[u][0] * np.pi, upperAngle=angle_boundaries[u][1] * np.pi, ) hand_part.userData = CustomBodyUserData(True, name="hand") self.body_parts.append(hand_part) joint_motor = world.CreateJoint(rjd) joint_motor.userData = CustomMotorUserData( SPEED_HAND, True, contact_body=hand_part) self.motors.append(joint_motor) prev_part = hand_part
def draw(self, world, init_x, init_y, force_to_center): init_y = init_y + 1 #### HULL #### HULL_FD = fixtureDef( shape=polygonShape(vertices=[(x / self.SCALE, y / self.SCALE) for x, y in HULL_POLYGONS]), density=self.DENSITY, friction=0.1, categoryBits=0x20, maskBits=0x000F) # 0.99 bouncy hull = world.CreateDynamicBody(position=(init_x, init_y), fixtures=HULL_FD) hull.color1 = (0.5, 0.4, 0.9) hull.color2 = (0.3, 0.3, 0.5) # hull.ApplyForceToCenter((force_to_center, 0), True) hull.userData = CustomBodyUserData(True, is_contact_critical=False, name="head") self.body_parts.append(hull) self.reference_head_object = hull #### P1 #### body_p1_x = init_x - 35 / 2 / self.SCALE - 16 / 2 / self.SCALE BODY_P1_FD = fixtureDef( shape=polygonShape(vertices=[(x / self.SCALE, y / self.SCALE) for x, y in BODY_P1]), density=self.DENSITY, restitution=0.0, categoryBits=0x20, maskBits=0x000F) body_p1 = world.CreateDynamicBody(position=(body_p1_x, init_y), fixtures=BODY_P1_FD) body_p1.color1 = (0.5, 0.4, 0.9) body_p1.color2 = (0.3, 0.3, 0.5) rjd = revoluteJointDef( bodyA=hull, bodyB=body_p1, anchor=(init_x - 35 / 2 / self.SCALE, init_y), enableMotor=True, enableLimit=True, maxMotorTorque=self.MOTORS_TORQUE, motorSpeed=1, lowerAngle=-0.1 * np.pi, upperAngle=0.2 * np.pi, ) body_p1.userData = CustomBodyUserData(True, name="body") self.body_parts.append(body_p1) joint_motor = world.CreateJoint(rjd) joint_motor.userData = CustomMotorUserData(SPEED_KNEE, True, contact_body=body_p1) self.motors.append(joint_motor) #### P2 #### body_p2_x = body_p1_x - 16 / 2 / self.SCALE - 16 / 2 / self.SCALE BODY_P2_FD = fixtureDef( shape=polygonShape(vertices=[(x / self.SCALE, y / self.SCALE) for x, y in BODY_P2]), density=self.DENSITY, restitution=0.0, categoryBits=0x20, maskBits=0x000F) body_p2 = world.CreateDynamicBody(position=(body_p2_x, init_y), fixtures=BODY_P2_FD) body_p2.color1 = (0.5, 0.4, 0.9) body_p2.color2 = (0.3, 0.3, 0.5) rjd = revoluteJointDef( bodyA=body_p1, bodyB=body_p2, anchor=(body_p1_x - 16 / 2 / self.SCALE, init_y), enableMotor=True, enableLimit=True, maxMotorTorque=self.MOTORS_TORQUE, motorSpeed=1, lowerAngle=-0.15 * np.pi, upperAngle=0.15 * np.pi, ) body_p2.userData = CustomBodyUserData(True, name="body") self.body_parts.append(body_p2) joint_motor = world.CreateJoint(rjd) joint_motor.userData = CustomMotorUserData(SPEED_KNEE, True, contact_body=body_p2) self.motors.append(joint_motor) #### P3 #### body_p3_x = body_p2_x - 16 / 2 / self.SCALE - 8 / 2 / self.SCALE BODY_P3_FD = fixtureDef( shape=polygonShape(vertices=[(x / self.SCALE, y / self.SCALE) for x, y in BODY_P3]), density=self.DENSITY, restitution=0.0, categoryBits=0x20, maskBits=0x000F) body_p3 = world.CreateDynamicBody(position=(body_p3_x, init_y), fixtures=BODY_P3_FD) body_p3.color1 = (0.5, 0.4, 0.9) body_p3.color2 = (0.3, 0.3, 0.5) rjd = revoluteJointDef( bodyA=body_p2, bodyB=body_p3, anchor=(body_p2_x - 16 / 2 / self.SCALE, init_y), enableMotor=True, enableLimit=True, maxMotorTorque=self.MOTORS_TORQUE, motorSpeed=1, lowerAngle=-0.3 * np.pi, upperAngle=0.3 * np.pi, ) body_p3.userData = CustomBodyUserData(True, name="body") self.body_parts.append(body_p3) self.tail = body_p3 joint_motor = world.CreateJoint(rjd) joint_motor.userData = CustomMotorUserData(SPEED_KNEE, True, contact_body=body_p3) self.motors.append(joint_motor) #### FIN #### FIN_FD = fixtureDef(shape=polygonShape(vertices=[(x / self.SCALE, y / self.SCALE) for x, y in FIN]), density=self.DENSITY, restitution=0.0, categoryBits=0x20, maskBits=0x000F) fin_positions = [ # [init_x + 35 / 2 / self.SCALE / 2, init_y], # [init_x - 35 / 2 / self.SCALE / 2, init_y], [init_x, init_y - 22 / 2 / self.SCALE + 0.2], # [init_x - 35 / 2 / self.SCALE / 2, init_y - 22 / 2 / self.SCALE + 0.1], ] fin_angle = -0.2 * np.pi middle_fin_x_distance = np.sin(fin_angle) * 20 / 2 / self.SCALE middle_fin_y_distance = np.cos(fin_angle) * 20 / 2 / self.SCALE for fin_pos in fin_positions: current_fin_x = fin_pos[0] + middle_fin_x_distance current_fin_y = fin_pos[1] - middle_fin_y_distance fin = world.CreateDynamicBody(position=(current_fin_x, current_fin_y), fixtures=FIN_FD, angle=fin_angle) fin.color1 = (0.5, 0.4, 0.9) fin.color2 = (0.3, 0.3, 0.5) rjd = revoluteJointDef( bodyA=hull, bodyB=fin, anchor=(fin_pos[0], fin_pos[1]), enableMotor=True, enableLimit=True, maxMotorTorque=self.MOTORS_TORQUE, motorSpeed=1, lowerAngle=-0.3 * np.pi, upperAngle=0.2 * np.pi, ) fin.userData = CustomBodyUserData(True, name="fin") self.body_parts.append(fin) self.fins.append(fin) joint_motor = world.CreateJoint(rjd) joint_motor.userData = CustomMotorUserData(SPEED_KNEE, True, contact_body=fin) self.motors.append(joint_motor)
def _create_rocket(self, initial_coordinates=(W / 2, H / 1.2)): body_color = (1, 1, 1) # ---------------------------------------------------------------------------------------- # LANDER initial_x, initial_y = initial_coordinates self.lander = self.world.CreateDynamicBody( position=(initial_x, initial_y), angle=0, fixtures=fixtureDef( shape=polygonShape(vertices=[(x / SCALE, y / SCALE) for x, y in LANDER_POLY]), density=5.0, friction=0.1, categoryBits=0x0010, maskBits=0x001, # collide only with ground restitution=0.0) # 0.99 bouncy ) self.lander.color1 = body_color self.lander.color2 = (0, 0, 0) if isinstance(self.settings['Initial Force'], str): self.lander.ApplyForceToCenter( (self.np_random.uniform(-INITIAL_RANDOM * 0.3, INITIAL_RANDOM * 0.3), self.np_random.uniform(-1.3 * INITIAL_RANDOM, -INITIAL_RANDOM)), True) else: self.lander.ApplyForceToCenter(self.settings['Initial Force'], True) # COG is set in the middle of the polygon by default. x = 0 = middle. # self.lander.mass = 25 # self.lander.localCenter = (0, 3) # COG # ---------------------------------------------------------------------------------------- # LEGS self.legs = [] for i in [-1, +1]: leg = self.world.CreateDynamicBody( position=(initial_x - i * LEG_AWAY / SCALE, initial_y), angle=(i * 0.05), fixtures=fixtureDef(shape=polygonShape(box=(LEG_W / SCALE, LEG_H / SCALE)), density=5.0, restitution=0.0, categoryBits=0x0020, maskBits=0x005)) leg.ground_contact = False leg.color1 = body_color leg.color2 = (0, 0, 0) rjd = revoluteJointDef( bodyA=self.lander, bodyB=leg, localAnchorA=(-i * 0.3 / LANDER_CONSTANT, 0), localAnchorB=(i * 0.5 / LANDER_CONSTANT, LEG_DOWN), enableMotor=True, enableLimit=True, maxMotorTorque=LEG_SPRING_TORQUE, motorSpeed=+0.3 * i # low enough not to jump back into the sky ) if i == -1: rjd.lowerAngle = 40 * DEGTORAD rjd.upperAngle = 45 * DEGTORAD else: rjd.lowerAngle = -45 * DEGTORAD rjd.upperAngle = -40 * DEGTORAD leg.joint = self.world.CreateJoint(rjd) self.legs.append(leg) # ---------------------------------------------------------------------------------------- # NOZZLE self.nozzle = self.world.CreateDynamicBody( position=(initial_x, initial_y), angle=0.0, fixtures=fixtureDef( shape=polygonShape(vertices=[(x / SCALE, y / SCALE) for x, y in NOZZLE_POLY]), density=5.0, friction=0.1, categoryBits=0x0040, maskBits=0x003, # collide only with ground restitution=0.0) # 0.99 bouncy ) self.nozzle.color1 = (0, 0, 0) self.nozzle.color2 = (0, 0, 0) rjd = revoluteJointDef( bodyA=self.lander, bodyB=self.nozzle, localAnchorA=(0, 0), localAnchorB=(0, 0.2), enableMotor=True, enableLimit=True, maxMotorTorque=NOZZLE_TORQUE, motorSpeed=0, referenceAngle=0, lowerAngle=-13 * DEGTORAD, # +- 15 degrees limit applied in practice upperAngle=13 * DEGTORAD) # The default behaviour of a revolute joint is to rotate without resistance. self.nozzle.joint = self.world.CreateJoint(rjd) # ---------------------------------------------------------------------------------------- # self.drawlist = [self.nozzle] + [self.lander] + self.legs self.drawlist = self.legs + [self.nozzle] + [self.lander] self.initial_mass = self.lander.mass self.remaining_fuel = INITIAL_FUEL_MASS_PERCENTAGE * self.initial_mass return
def reset(self): self._destroy() self.world.contactListener_keepref = ContactDetector(self) self.world.contactListener = self.world.contactListener_keepref self.game_over = False self.prev_shaping = None self.throttle = 0 self.gimbal = 0.0 self.landed_ticks = 0 self.stepnumber = 0 self.smoke = [] # self.terrainheigth = self.np_random.uniform(H / 20, H / 10) self.terrainheigth = H / 20 self.shipheight = self.terrainheigth + SHIP_HEIGHT # ship_pos = self.np_random.uniform(0, SHIP_WIDTH / SCALE) + SHIP_WIDTH / SCALE ship_pos = W / 2 self.helipad_x1 = ship_pos - SHIP_WIDTH / 2 self.helipad_x2 = self.helipad_x1 + SHIP_WIDTH self.helipad_y = self.terrainheigth + SHIP_HEIGHT self.water = self.world.CreateStaticBody( fixtures=fixtureDef(shape=polygonShape( vertices=((0, 0), (W, 0), (W, self.terrainheigth), (0, self.terrainheigth))), friction=0.1, restitution=0.0)) self.water.color1 = rgb(70, 96, 176) self.ship = self.world.CreateStaticBody( fixtures=fixtureDef(shape=polygonShape( vertices=((self.helipad_x1, self.terrainheigth), (self.helipad_x2, self.terrainheigth), (self.helipad_x2, self.terrainheigth + SHIP_HEIGHT), (self.helipad_x1, self.terrainheigth + SHIP_HEIGHT))), friction=0.5, restitution=0.0)) self.containers = [] for side in [-1, 1]: self.containers.append( self.world.CreateStaticBody( fixtures=fixtureDef(shape=polygonShape( vertices=((ship_pos + side * 0.95 * SHIP_WIDTH / 2, self.helipad_y), (ship_pos + side * 0.95 * SHIP_WIDTH / 2, self.helipad_y + SHIP_HEIGHT), (ship_pos + side * 0.95 * SHIP_WIDTH / 2 - side * SHIP_HEIGHT, self.helipad_y + SHIP_HEIGHT), (ship_pos + side * 0.95 * SHIP_WIDTH / 2 - side * SHIP_HEIGHT, self.helipad_y))), friction=0.2, restitution=0.0))) self.containers[-1].color1 = rgb(206, 206, 2) self.ship.color1 = (0.2, 0.2, 0.2) initial_x = W / 2 + W * np.random.uniform(-0.3, 0.3) initial_y = H * 0.95 self.lander = self.world.CreateDynamicBody( position=(initial_x, initial_y), angle=0.0, fixtures=fixtureDef(shape=polygonShape( vertices=((-ROCKET_WIDTH / 2, 0), (+ROCKET_WIDTH / 2, 0), (ROCKET_WIDTH / 2, +ROCKET_HEIGHT), (-ROCKET_WIDTH / 2, +ROCKET_HEIGHT))), density=1.0, friction=0.5, categoryBits=0x0010, maskBits=0x001, restitution=0.0)) self.lander.color1 = rgb(230, 230, 230) for i in [-1, +1]: leg = self.world.CreateDynamicBody( position=(initial_x - i * LEG_AWAY, initial_y + ROCKET_WIDTH * 0.2), angle=(i * BASE_ANGLE), fixtures=fixtureDef(shape=polygonShape( vertices=((0, 0), (0, LEG_LENGTH / 25), (i * LEG_LENGTH, 0), (i * LEG_LENGTH, -LEG_LENGTH / 20), (i * LEG_LENGTH / 3, -LEG_LENGTH / 7))), density=1, restitution=0.0, friction=0.2, categoryBits=0x0020, maskBits=0x001)) leg.ground_contact = False leg.color1 = (0.25, 0.25, 0.25) rjd = revoluteJointDef(bodyA=self.lander, bodyB=leg, localAnchorA=(i * LEG_AWAY, ROCKET_WIDTH * 0.2), localAnchorB=(0, 0), enableLimit=True, maxMotorTorque=2500.0, motorSpeed=-0.05 * i, enableMotor=True) djd = distanceJointDef(bodyA=self.lander, bodyB=leg, anchorA=(i * LEG_AWAY, ROCKET_HEIGHT / 8), anchorB=leg.fixtures[0].body.transform * (i * LEG_LENGTH, 0), collideConnected=False, frequencyHz=0.01, dampingRatio=0.9) if i == 1: rjd.lowerAngle = -SPRING_ANGLE rjd.upperAngle = 0 else: rjd.lowerAngle = 0 rjd.upperAngle = +SPRING_ANGLE leg.joint = self.world.CreateJoint(rjd) leg.joint2 = self.world.CreateJoint(djd) self.legs.append(leg) self.lander.linearVelocity = ( -self.np_random.uniform(0, INITIAL_RANDOM) * START_SPEED * (initial_x - W / 2) / W, -START_SPEED) self.lander.angularVelocity = (1 + INITIAL_RANDOM) * np.random.uniform( -1, 1) self.drawlist = self.legs + [self.water] + [ self.ship ] + self.containers + [self.lander] if CONTINUOUS: return self.step([0, 0, 0])[0] else: return self.step(6)[0]
def draw(self, world, init_x, init_y, force_to_center): HULL_FIXTURES = [ fixtureDef(shape=polygonShape(vertices=[(x / self.SCALE, y / self.SCALE) for x, y in polygon]), density=5.0, friction=0.1, categoryBits=0x20, maskBits=0x000F) for polygon in HULL_POLYGONS ] LEG_FD = fixtureDef(shape=polygonShape(box=(self.LEG_W / 2, self.LEG_H / 2)), density=1.0, restitution=0.0, categoryBits=0x20, maskBits=0x000F) LOWER_FD = fixtureDef(shape=polygonShape(box=(0.8 * self.LEG_W / 2, self.LEG_H / 2)), density=1.0, restitution=0.0, categoryBits=0x20, maskBits=0x000F) hull = world.CreateDynamicBody(position=(init_x, init_y), fixtures=HULL_FIXTURES) hull.color1 = (0.5, 0.4, 0.9) hull.color2 = (0.3, 0.3, 0.5) hull.ApplyForceToCenter((force_to_center, 0), True) hull.userData = CustomBodyUserData( True, is_contact_critical=self.reset_on_hull_critical_contact, name="hull") self.body_parts.append(hull) self.reference_head_object = hull for x_anchor in [-1.2, 0.85]: absolute_x = init_x + np.interp(x_anchor, [-1, 1], [ -HULL_BOTTOM_WIDTH / 2 / self.SCALE, HULL_BOTTOM_WIDTH / 2 / self.SCALE ]) for i in [-1, +1]: leg = world.CreateDynamicBody( position=(absolute_x, init_y - self.LEG_H / 2 - self.LEG_DOWN), angle=(i * 0.05), fixtures=LEG_FD) leg.color1 = (0.6 - i / 10., 0.3 - i / 10., 0.5 - i / 10.) leg.color2 = (0.4 - i / 10., 0.2 - i / 10., 0.3 - i / 10.) rjd = revoluteJointDef( bodyA=hull, bodyB=leg, localAnchorA=(x_anchor, self.LEG_DOWN), localAnchorB=(0, self.LEG_H / 2), enableMotor=True, enableLimit=True, maxMotorTorque=self.MOTORS_TORQUE, motorSpeed=i, lowerAngle=-0.8, upperAngle=1.1, ) leg.userData = CustomBodyUserData(False, name="leg") self.body_parts.append(leg) joint_motor = world.CreateJoint(rjd) joint_motor.userData = CustomMotorUserData(SPEED_HIP, False) self.motors.append(joint_motor) lower = world.CreateDynamicBody( position=(absolute_x, init_y - self.LEG_H * 3 / 2 - self.LEG_DOWN), angle=(i * 0.05), fixtures=LOWER_FD) lower.color1 = (0.6 - i / 10., 0.3 - i / 10., 0.5 - i / 10.) lower.color2 = (0.4 - i / 10., 0.2 - i / 10., 0.3 - i / 10.) rjd = revoluteJointDef( bodyA=leg, bodyB=lower, localAnchorA=(0, -self.LEG_H / 2), localAnchorB=(0, self.LEG_H / 2), enableMotor=True, enableLimit=True, maxMotorTorque=self.MOTORS_TORQUE, motorSpeed=1, lowerAngle=-1.6, upperAngle=-0.1, ) lower.userData = CustomBodyUserData(True, name="lower") self.body_parts.append(lower) joint_motor = world.CreateJoint(rjd) joint_motor.userData = CustomMotorUserData( SPEED_KNEE, True, contact_body=lower, angle_correction=1.0) self.motors.append(joint_motor)
def _reset(self): self._destroy() self.game_over = False self.prev_shaping = None W = VIEWPORT_W/SCALE H = VIEWPORT_H/SCALE # terrain CHUNKS = 11 height = np.random.uniform(0, H/2, size=(CHUNKS+1,) ) chunk_x = [W/(CHUNKS-1)*i for i in xrange(CHUNKS)] self.helipad_x1 = chunk_x[CHUNKS//2-1] self.helipad_x2 = chunk_x[CHUNKS//2+1] self.helipad_y = H/4 height[CHUNKS//2-2] = self.helipad_y height[CHUNKS//2-1] = self.helipad_y height[CHUNKS//2+0] = self.helipad_y height[CHUNKS//2+1] = self.helipad_y height[CHUNKS//2+2] = self.helipad_y smooth_y = [0.33*(height[i-1] + height[i+0] + height[i+1]) for i in xrange(CHUNKS)] self.moon = self.world.CreateStaticBody( shapes=edgeShape(vertices=[(0, 0), (W, 0)]) ) self.sky_polys = [] for i in xrange(CHUNKS-1): p1 = (chunk_x[i], smooth_y[i]) p2 = (chunk_x[i+1], smooth_y[i+1]) self.moon.CreateEdgeFixture( vertices=[p1,p2], density=0, friction=0.1) self.sky_polys.append( [p1, p2, (p2[0],H), (p1[0],H)] ) self.moon.color1 = (0.0,0.0,0.0) self.moon.color2 = (0.0,0.0,0.0) initial_y = VIEWPORT_H/SCALE self.lander = self.world.CreateDynamicBody( position = (VIEWPORT_W/SCALE/2, initial_y), angle=0.0, fixtures = fixtureDef( shape=polygonShape(vertices=[ (x/SCALE,y/SCALE) for x,y in LANDER_POLY ]), density=5.0, friction=0.1, categoryBits=0x0010, maskBits=0x001, # collide only with ground restitution=0.0) # 0.99 bouncy ) self.lander.color1 = (0.5,0.4,0.9) self.lander.color2 = (0.3,0.3,0.5) self.lander.ApplyForceToCenter( ( np.random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM), np.random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM) ), True) self.legs = [] for i in [-1,+1]: leg = self.world.CreateDynamicBody( position = (VIEWPORT_W/SCALE/2 - i*LEG_AWAY/SCALE, initial_y), angle = (i*0.05), fixtures = fixtureDef( shape=polygonShape(box=(LEG_W/SCALE, LEG_H/SCALE)), density=1.0, restitution=0.0, categoryBits=0x0020, maskBits=0x001) ) leg.color1 = (0.5,0.4,0.9) leg.color2 = (0.3,0.3,0.5) rjd = revoluteJointDef( bodyA=self.lander, bodyB=leg, localAnchorA=(0, 0), localAnchorB=(i*LEG_AWAY/SCALE, LEG_DOWN/SCALE), enableMotor=True, enableLimit=True, maxMotorTorque=LEG_SPRING_TORQUE, motorSpeed=+0.3*i # low enough not to jump back into the sky ) if i==-1: rjd.lowerAngle = +0.9 - 0.5 # Yes, the most esoteric numbers here, angles legs have freedom to travel within rjd.upperAngle = +0.9 else: rjd.lowerAngle = -0.9 rjd.upperAngle = -0.9 + 0.5 leg.joint = self.world.CreateJoint(rjd) self.legs.append(leg) self.drawlist = [self.lander] + self.legs return self._step(0)[0]
def draw(self, world, init_x, init_y, force_to_center): MAIN_BODY_FIXTURES = [ fixtureDef(shape=polygonShape(vertices=[(x / self.SCALE, y / self.SCALE) for x, y in polygon]), density=5.0, friction=0.1, categoryBits=0x20, maskBits=0x000F) for polygon in MAIN_BODY_POLYGONS ] LEG_FD = fixtureDef(shape=polygonShape(box=(self.LEG_W / 2, self.LEG_H / 2)), density=1.0, restitution=0.0, categoryBits=0x20, maskBits=0x000F) LOWER_FD = fixtureDef(shape=polygonShape(box=(0.8 * self.LEG_W / 2, self.LEG_H / 2)), density=1.0, restitution=0.0, categoryBits=0x20, maskBits=0x000F) init_x = init_x - MAIN_BODY_BOTTOM_WIDTH / self.SCALE * self.nb_of_bodies / 2 # initial init_x is the middle of the whole body previous_main_body = None for j in range(self.nb_of_bodies): main_body_x = init_x + j * (MAIN_BODY_BOTTOM_WIDTH / self.SCALE) main_body = world.CreateDynamicBody(position=(main_body_x, init_y), fixtures=MAIN_BODY_FIXTURES) main_body.color1 = (0.5, 0.4, 0.9) main_body.color2 = (0.3, 0.3, 0.5) main_body.ApplyForceToCenter((force_to_center, 0), True) # self.body_parts.append(BodyPart(main_body, True, True)) main_body.userData = CustomBodyUserData(True, is_contact_critical=False, name="body") self.body_parts.append(main_body) for i in [-1, +1]: leg = world.CreateDynamicBody( position=(main_body_x, init_y - self.LEG_H / 2 - self.LEG_DOWN), #angle=(i * 0.05), fixtures=LEG_FD) leg.color1 = (0.6 - i / 10., 0.3 - i / 10., 0.5 - i / 10.) leg.color2 = (0.4 - i / 10., 0.2 - i / 10., 0.3 - i / 10.) rjd = revoluteJointDef( bodyA=main_body, bodyB=leg, anchor=(main_body_x, init_y - self.LEG_DOWN), enableMotor=True, enableLimit=True, maxMotorTorque=self.MOTORS_TORQUE, motorSpeed=i, lowerAngle=-0.8, upperAngle=1.1, ) leg.userData = CustomBodyUserData(False, name="leg") self.body_parts.append(leg) joint_motor = world.CreateJoint(rjd) joint_motor.userData = CustomMotorUserData(SPEED_HIP, False) self.motors.append(joint_motor) lower = world.CreateDynamicBody( position=(main_body_x, init_y - self.LEG_H * 3 / 2 - self.LEG_DOWN), #angle=(i * 0.05), fixtures=LOWER_FD) lower.color1 = (0.6 - i / 10., 0.3 - i / 10., 0.5 - i / 10.) lower.color2 = (0.4 - i / 10., 0.2 - i / 10., 0.3 - i / 10.) rjd = revoluteJointDef( bodyA=leg, bodyB=lower, anchor=(main_body_x, init_y - self.LEG_DOWN - self.LEG_H), enableMotor=True, enableLimit=True, maxMotorTorque=self.MOTORS_TORQUE, motorSpeed=1, lowerAngle=-1.6, upperAngle=-0.1, ) lower.userData = CustomBodyUserData(True, name="lower") self.body_parts.append(lower) joint_motor = world.CreateJoint(rjd) joint_motor.userData = CustomMotorUserData( SPEED_KNEE, True, contact_body=lower, angle_correction=1.0) self.motors.append(joint_motor) if previous_main_body is not None: rjd = revoluteJointDef( bodyA=previous_main_body, bodyB=main_body, anchor=(main_body_x - MAIN_BODY_BOTTOM_WIDTH / self.SCALE / 2, init_y), enableMotor=False, enableLimit=True, lowerAngle=-0.05 * np.pi, upperAngle=0.05 * np.pi, ) world.CreateJoint(rjd) previous_main_body = main_body self.reference_head_object = previous_main_body # set as head the rightmost body self.reference_head_object.is_contact_critical = self.reference_head_object
def _reset(self): self._destroy() self.world.contactListener_bug_workaround = ContactDetector(self) self.world.contactListener = self.world.contactListener_bug_workaround self.game_over = False self.prev_shaping = None self.scroll = 0.0 self.lidar_render = 0 W = VIEWPORT_W / SCALE H = VIEWPORT_H / SCALE self._generate_terrain(self.hardcore) # self._generate_clouds() init_x = TERRAIN_STEP * TERRAIN_STARTPAD / 2 init_y = TERRAIN_HEIGHT + 2 * LEG_H self.hull = self.world.CreateDynamicBody( position=(init_x, init_y), fixtures=fixtureDef( shape=polygonShape(vertices=[(x / SCALE, y / SCALE) for x, y in HULL_POLY]), density=11.0, friction=0.1, categoryBits=0x0020, maskBits=0x001, # collide only with ground restitution=0.0) # 0.99 bouncy ) self.hull.color1 = (0.5, 0.4, 0.9) self.hull.color2 = (0.3, 0.3, 0.5) self.hull.ApplyForceToCenter( (self.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM), 0), True) self.head = self.world.CreateDynamicBody( position=(init_x - 2 / SCALE, init_y + (HULL_H + 10) / SCALE), fixtures=fixtureDef( shape=circleShape(pos=[0, 0], radius=7.0 / SCALE), density=1.0, friction=0.1, categoryBits=0x0020, maskBits=0x001, # collide only with ground restitution=0.0) # 0.99 bouncy ) self.head.color1 = (1.0, 0.4, 0.9) self.head.color2 = (0.3, 1.0, 0.5) #self.head.ApplyForceToCenter((self.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM), 0), True) self.legs = [] self.arms = [] self.joints = [] rjd1 = revoluteJointDef( bodyA=self.head, bodyB=self.hull, localAnchorA=(0, 0), localAnchorB=(0, +(HULL_H) / SCALE), # enableMotor=True, # enableLimit=True, # maxMotorTorque=MOTORS_TORQUE, # motorSpeed = -1, # lowerAngle = -0.8, # upperAngle = 1.1, ) for i in [-1, +1]: leg = self.world.CreateDynamicBody( position=(init_x, init_y - LEG_H / 2 - LEG_DOWN), angle=(i * 0.05), fixtures=fixtureDef(shape=polygonShape(box=(LEG_W / 2, LEG_H / 2)), density=1.0, restitution=0.0, categoryBits=0x0020, maskBits=0x001)) leg.color1 = (0.6 - i / 10., 0.3 - i / 10., 0.5 - i / 10.) leg.color2 = (0.4 - i / 10., 0.2 - i / 10., 0.3 - i / 10.) rjd = revoluteJointDef( bodyA=self.hull, bodyB=leg, localAnchorA=(0, LEG_DOWN), localAnchorB=(0, LEG_H / 2), enableMotor=True, enableLimit=True, maxMotorTorque=MOTORS_TORQUE, motorSpeed=i, lowerAngle=-0.8, upperAngle=1.1, ) self.legs.append(leg) self.joints.append(self.world.CreateJoint(rjd)) lower = self.world.CreateDynamicBody( position=(init_x, init_y - LEG_H * 3 / 2 - LEG_DOWN), angle=(i * 0.05), fixtures=fixtureDef(shape=polygonShape(box=(0.8 * LEG_W / 2, LEG_H / 2)), density=1.0, restitution=0.0, categoryBits=0x0020, maskBits=0x001)) lower.color1 = (0.6 - i / 10., 0.3 - i / 10., 0.5 - i / 10.) lower.color2 = (0.4 - i / 10., 0.2 - i / 10., 0.3 - i / 10.) rjd = revoluteJointDef( bodyA=leg, bodyB=lower, localAnchorA=(0, -LEG_H / 2), localAnchorB=(0, LEG_H / 2), enableMotor=True, enableLimit=True, maxMotorTorque=MOTORS_TORQUE, motorSpeed=1, lowerAngle=-1.6, upperAngle=-0.1, ) lower.ground_contact = False self.legs.append(lower) self.joints.append(self.world.CreateJoint(rjd)) for i in [-1, +1]: arm = self.world.CreateDynamicBody( position=(init_x, init_y - arm_H / 2 + (HULL_H) / SCALE), angle=(i * 0.05), fixtures=fixtureDef(shape=polygonShape(box=(arm_W / 2, arm_H / 2)), density=1.0, restitution=0.0, categoryBits=0x0020, maskBits=0x001)) arm.color1 = (0.6 - i / 10., 0.3 - i / 10., 0.5 - i / 10.) arm.color2 = (0.4 - i / 10., 0.2 - i / 10., 0.3 - i / 10.) rjd = revoluteJointDef( bodyA=self.hull, bodyB=arm, localAnchorA=(0, (HULL_H) / SCALE), localAnchorB=(0, arm_H / 2), enableMotor=True, enableLimit=True, maxMotorTorque=MOTORS_TORQUE, motorSpeed=-i, lowerAngle=-0.8, upperAngle=0.8, ) self.arms.append(arm) self.joints.append(self.world.CreateJoint(rjd)) # lower_arm = self.world.CreateDynamicBody( # position = (init_x, init_y - arm_H*3/2 + (HULL_H)/SCALE), # angle = (i*0.05), # fixtures = fixtureDef( # shape=polygonShape(box=(0.8*arm_W/2, arm_H/2)), # density=1.0, # restitution=0.0, # categoryBits=0x0020, # maskBits=0x001) # ) # lower_arm.color1 = (0.6-i/10., 0.3-i/10., 0.5-i/10.) # lower_arm.color2 = (0.4-i/10., 0.2-i/10., 0.3-i/10.) # rjd = revoluteJointDef( # bodyA=arm, # bodyB=lower_arm, # localAnchorA=(0, -arm_H/2), # localAnchorB=(0, arm_H/2), # enableMotor=True, # enableLimit=True, # maxMotorTorque=MOTORS_TORQUE, # motorSpeed = 1, # lowerAngle = -1.6, # upperAngle = -0.1, # ) # lower_arm.ground_contact = False # self.arms.append(lower_arm) # self.joints.append(self.world.CreateJoint(rjd)) self.joints.append(self.world.CreateJoint(rjd1)) self.drawlist = self.terrain + self.legs + self.arms + [self.hull] + [ self.head ] class LidarCallback(Box2D.b2.rayCastCallback): def ReportFixture(self, fixture, point, normal, fraction): if (fixture.filterData.categoryBits & 1) == 0: return 1 self.p2 = point self.fraction = fraction return 0 self.lidar = [LidarCallback() for _ in range(10)] self.ss = 1 self.a = np.array([0, 0, 0, 0]) return self._step([0]) class LidarCallback(Box2D.b2.rayCastCallback): def ReportFixture(self, fixture, point, normal, fraction): if (fixture.filterData.categoryBits & 1) == 0: return 1 self.p2 = point self.fraction = fraction return 0 self.lidar = [LidarCallback() for _ in range(10)] self.a = np.array([0, 0, 0, 0]) return self._step(0)
def _reset(self): self._destroy() self.world.contactListener_bug_workaround = ContactDetector(self) self.world.contactListener = self.world.contactListener_bug_workaround self.game_over = False self.prev_shaping = None self.scroll = 0.0 self.lidar_render = 0 W = VIEWPORT_W/SCALE H = VIEWPORT_H/SCALE self._generate_terrain(self.hardcore) self._generate_clouds() self.hull = self.world.CreateDynamicBody( position = (hull_x, hull_y), fixtures = fixtureDef( shape=polygonShape(vertices=[ (x/SCALE,y/SCALE) for x,y in HULL_POLY ]), density=5.0, friction=0.1, categoryBits=0x0020, maskBits=0x001, # collide only with ground restitution=0.0) # 0.99 bouncy ) self.hull.color1 = (0.5,0.4,0.9) self.hull.color2 = (0.3,0.3,0.5) #self.hull.ApplyForceToCenter((self.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM), 0), True) self.legs = [] self.joints = [] for i in [-1,+1]: leg = self.world.CreateDynamicBody( position = (leg_x, leg_y), angle = gamma_i, fixtures = fixtureDef( shape=polygonShape(box=(LEG_W/2, LEG_H/2)), density=1.0, restitution=0.0, categoryBits=0x0020, maskBits=0x001) ) leg.color1 = (0.6-i/10., 0.3-i/10., 0.5-i/10.) leg.color2 = (0.4-i/10., 0.2-i/10., 0.3-i/10.) rjd = revoluteJointDef( bodyA=self.hull, bodyB=leg, localAnchorA=(0, 0), localAnchorB=(0, LEG_H/2), enableMotor=True, enableLimit=True, maxMotorTorque=MOTORS_TORQUE, motorSpeed = 0.0, referenceAngle = 0.0, lowerAngle = -1.4, # -80 degree upperAngle = 1.4, # 80 degree ) self.legs.append(leg) self.joints.append(self.world.CreateJoint(rjd)) lower = self.world.CreateDynamicBody( position = (lower_x, lower_y), angle = (-math.pi/2+a_i), fixtures = fixtureDef( shape=polygonShape(box=(LEG_W/2, LEG_H/2)), density=1.0, restitution=0.0, categoryBits=0x0020, maskBits=0x001) ) lower.color1 = (0.6-i/10., 0.3-i/10., 0.5-i/10.) lower.color2 = (0.4-i/10., 0.2-i/10., 0.3-i/10.) rjd = revoluteJointDef( bodyA=leg, bodyB=lower, localAnchorA=(0, -LEG_H/2), localAnchorB=(0, LEG_H/2), enableMotor=True, enableLimit=True, maxMotorTorque=MOTORS_TORQUE, motorSpeed = 0.0, referenceAngle = -1*math.pi, lowerAngle = 0.35, # 20 degree upperAngle = 3.14, # 180 degree ) lower.ground_contact = False self.legs.append(lower) self.joints.append(self.world.CreateJoint(rjd)) self.drawlist = self.terrain + self.legs + [self.hull] class LidarCallback(Box2D.b2.rayCastCallback): def ReportFixture(self, fixture, point, normal, fraction): if (fixture.filterData.categoryBits & 1) == 0: return 1 self.p2 = point self.fraction = fraction return 0 self.lidar = [LidarCallback() for _ in range(10)]
def reset(self): self._destroy() self.world.contactListener_bug_workaround = ContactDetector(self) self.world.contactListener = self.world.contactListener_bug_workaround self.game_over = False self.prev_shaping = None self.scroll = 0.0 self.lidar_render = 0 W = VIEWPORT_W / SCALE H = VIEWPORT_H / SCALE self._generate_terrain(self.hardcore) self._generate_clouds() init_x = TERRAIN_STEP * TERRAIN_STARTPAD / 2 init_y = TERRAIN_HEIGHT + 2 * LEG_H self.hull = self.world.CreateDynamicBody(position=(init_x, init_y), fixtures=HULL_FD) self.hull.color1 = (0.5, 0.4, 0.9) self.hull.color2 = (0.3, 0.3, 0.5) self.hull.ApplyForceToCenter( (self.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM), 0), True) self.legs = [] self.joints = [] for i in [-1, +1]: leg = self.world.CreateDynamicBody(position=(init_x, init_y - LEG_H / 2 - LEG_DOWN), angle=(i * 0.05), fixtures=LEG_FD) leg.color1 = (0.6 - i / 10., 0.3 - i / 10., 0.5 - i / 10.) leg.color2 = (0.4 - i / 10., 0.2 - i / 10., 0.3 - i / 10.) rjd = revoluteJointDef( bodyA=self.hull, bodyB=leg, localAnchorA=(0, LEG_DOWN), localAnchorB=(0, LEG_H / 2), enableMotor=True, enableLimit=True, maxMotorTorque=MOTORS_TORQUE, motorSpeed=i, lowerAngle=-0.8, upperAngle=1.1, ) self.legs.append(leg) self.joints.append(self.world.CreateJoint(rjd)) lower = self.world.CreateDynamicBody( position=(init_x, init_y - LEG_H * 3 / 2 - LEG_DOWN), angle=(i * 0.05), fixtures=LOWER_FD) lower.color1 = (0.6 - i / 10., 0.3 - i / 10., 0.5 - i / 10.) lower.color2 = (0.4 - i / 10., 0.2 - i / 10., 0.3 - i / 10.) rjd = revoluteJointDef( bodyA=leg, bodyB=lower, localAnchorA=(0, -LEG_H / 2), localAnchorB=(0, LEG_H / 2), enableMotor=True, enableLimit=True, maxMotorTorque=MOTORS_TORQUE, motorSpeed=1, lowerAngle=-1.6, upperAngle=-0.1, ) lower.ground_contact = False self.legs.append(lower) self.joints.append(self.world.CreateJoint(rjd)) self.drawlist = self.terrain + self.legs + [self.hull] class LidarCallback(Box2D.b2.rayCastCallback): def ReportFixture(self, fixture, point, normal, fraction): if (fixture.filterData.categoryBits & 1) == 0: return 1 self.p2 = point self.fraction = fraction return 0 self.lidar = [LidarCallback() for _ in range(10)] return self.step(np.array([0, 0, 0, 0]))[0]
def reset(self): self.update_initials() self._destroy() self.world.contactListener_keepref = ContactDetector(self) self.world.contactListener = self.world.contactListener_keepref self.game_over = False self.prev_shaping = None # ship self.ship = self.world.CreateStaticBody( position=self.origin, shapes=polygonShape( vertices=[(x / SCALE, y / SCALE) for x, y in SHIP['vertices'](self.ship_width)])) self.ship.color1 = SHIP['color1'] self.ship.color2 = SHIP['color2'] # lander self.lander = self.world.CreateDynamicBody( position=(self.initial_pos_x, self.initial_pos_y), angle=self.initial_dir, fixtures=fixtureDef( shape=polygonShape(vertices=[(x / SCALE, y / SCALE) for x, y in ROCKET['vertices']]), density=5.0, friction=0.1, categoryBits=0x0010, maskBits=0x001, # collide only with ground restitution=0.0) # 0.99 bouncy ) self.lander.color1 = ROCKET['body_color1'] self.lander.color2 = ROCKET['body_color2'] self.lander.ApplyForceToCenter( (self.initial_vel * math.sin(self.initial_dir), -self.initial_vel * math.cos(self.initial_dir)), True) self.lander.ApplyAngularImpulse(self.initial_vdir, True) self.legs = [] for i in [-1, +1]: leg = self.world.CreateDynamicBody( position=(self.initial_pos_x - i * ROCKET['leg_away'] / SCALE, self.initial_pos_y), angle=self.initial_dir + i * 0.05, fixtures=fixtureDef( shape=polygonShape(box=(ROCKET['leg_w'] / SCALE, ROCKET['leg_h'] / SCALE)), density=1.0, restitution=0.0, categoryBits=0x0020, maskBits=0x001)) leg.ground_contact = False leg.color1 = ROCKET['leg_color1'] leg.color2 = ROCKET['leg_color2'] rjd = revoluteJointDef( bodyA=self.lander, bodyB=leg, localAnchorA=(0, 0), localAnchorB=(i * ROCKET['leg_away'] / SCALE, ROCKET['leg_down'] / SCALE), enableMotor=True, enableLimit=True, maxMotorTorque=ROCKET['leg_spring_torque'], motorSpeed=+0.3 * i # low enough not to jump back into the sky ) if i == -1: rjd.lowerAngle = +0.9 - 0.5 # Yes, the most esoteric numbers here, angles legs have freedom to travel within rjd.upperAngle = +0.9 else: rjd.lowerAngle = -0.9 rjd.upperAngle = -0.9 + 0.5 leg.joint = self.world.CreateJoint(rjd) self.legs.append(leg) self.drawlist = [self.lander, self.ship] + self.legs return self.step(np.array([0, 0]) if self.continuous else 0)[0]
def __init__(self, world, init_angle, init_x, init_y, car_number, birth_place_index): # Handle the position of multiplayer, avoid overlapping at the beginning init_x -= birth_place_index % 2 * 5 init_y -= math.floor(birth_place_index / 2) * 10 self.world = world self.hull = self.world.CreateDynamicBody( position=(init_x, init_y), angle=init_angle, fixtures=[ fixtureDef(shape=polygonShape(vertices=[(x * SIZE, y * SIZE) for x, y in HULL_POLY1]), density=1.0), fixtureDef(shape=polygonShape(vertices=[(x * SIZE, y * SIZE) for x, y in HULL_POLY2]), density=1.0), fixtureDef(shape=polygonShape(vertices=[(x * SIZE, y * SIZE) for x, y in HULL_POLY3]), density=1.0), fixtureDef(shape=polygonShape(vertices=[(x * SIZE, y * SIZE) for x, y in HULL_POLY4]), density=1.0) ] ) self.hull.color = (0.8, 0.0, 0.0) # self.hull.color = get_a_color(car_number) self.hull.car_number = car_number self.hull.userData = self.hull self.wheels = [] self.fuel_spent = 0.0 self.image = pygame.image.load(f"{os.path.dirname(__file__)}/car.png") self.image2 = pygame.image.load(f"{os.path.dirname(__file__)}/car2.png") self.ob_image = pygame.transform.scale(self.image, (3, 5)) WHEEL_POLY = [ (-WHEEL_W, +WHEEL_R), (+WHEEL_W, +WHEEL_R), (+WHEEL_W, -WHEEL_R), (-WHEEL_W, -WHEEL_R) ] for wx, wy in WHEELPOS: front_k = 1.0 if wy > 0 else 1.0 w = self.world.CreateDynamicBody( position=(init_x + wx * SIZE, init_y + wy * SIZE), angle=init_angle, fixtures=fixtureDef( shape=polygonShape(vertices=[(x * front_k * SIZE, y * front_k * SIZE) for x, y in WHEEL_POLY]), density=0.1, categoryBits=0x0020, maskBits=0x001, restitution=0.0) ) w.car_number = car_number w.wheel_rad = front_k * WHEEL_R * SIZE w.color = WHEEL_COLOR w.gas = 0.0 w.brake = 0.0 w.steer = 0.0 w.phase = 0.0 # wheel angle w.omega = 0.0 # angular velocity w.skid_start = None w.skid_particle = None rjd = revoluteJointDef( bodyA=self.hull, bodyB=w, localAnchorA=(wx * SIZE, wy * SIZE), localAnchorB=(0, 0), enableMotor=True, enableLimit=True, maxMotorTorque=180 * 900 * SIZE * SIZE, motorSpeed=0, lowerAngle=-0.4, upperAngle=+0.4, ) w.joint = self.world.CreateJoint(rjd) w.tiles = set() w.userData = w self.wheels.append(w) self.drawlist = self.wheels + [self.hull] self.drawlist_colors = { item: tuple([255 * c for c in item.color]) for item in self.drawlist } self.particles = []
def reset(self): self._destroy() self.world.contactListener_keepref = ContactDetector(self) self.world.contactListener = self.world.contactListener_keepref self.game_over = False self.prev_shaping = None W = VIEWPORT_W / SCALE H = VIEWPORT_H / SCALE # terrain CHUNKS = 11 height = self.np_random.uniform(0, H / 2, size=(CHUNKS + 1, )) chunk_x = [W / (CHUNKS - 1) * i for i in range(CHUNKS)] self.helipad_x1 = chunk_x[CHUNKS // 2 - 1] self.helipad_x2 = chunk_x[CHUNKS // 2 + 1] self.helipad_y = H / 4 height[CHUNKS // 2 - 2] = self.helipad_y height[CHUNKS // 2 - 1] = self.helipad_y height[CHUNKS // 2 + 0] = self.helipad_y height[CHUNKS // 2 + 1] = self.helipad_y height[CHUNKS // 2 + 2] = self.helipad_y smooth_y = [ 0.33 * (height[i - 1] + height[i + 0] + height[i + 1]) for i in range(CHUNKS) ] self.moon = self.world.CreateStaticBody(shapes=edgeShape( vertices=[(0, 0), (W, 0)])) self.sky_polys = [] for i in range(CHUNKS - 1): p1 = (chunk_x[i], smooth_y[i]) p2 = (chunk_x[i + 1], smooth_y[i + 1]) self.moon.CreateEdgeFixture(vertices=[p1, p2], density=0, friction=0.1) self.sky_polys.append([p1, p2, (p2[0], H), (p1[0], H)]) self.moon.color1 = (0.0, 0.0, 0.0) self.moon.color2 = (0.0, 0.0, 0.0) initial_y = VIEWPORT_H / SCALE self.lander = self.world.CreateDynamicBody( position=(VIEWPORT_W / SCALE / 2, initial_y), angle=0.0, fixtures=fixtureDef( shape=polygonShape(vertices=[(x / SCALE, y / SCALE) for x, y in LANDER_POLY]), density=5.0, friction=0.1, categoryBits=0x0010, maskBits=0x001, # collide only with ground restitution=0.0) # 0.99 bouncy ) self.lander.color1 = (0.5, 0.4, 0.9) self.lander.color2 = (0.3, 0.3, 0.5) self.lander.ApplyForceToCenter( (self.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM), self.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM)), True) self.legs = [] for i in [-1, +1]: leg = self.world.CreateDynamicBody( position=(VIEWPORT_W / SCALE / 2 - i * LEG_AWAY / SCALE, initial_y), angle=(i * 0.05), fixtures=fixtureDef(shape=polygonShape(box=(LEG_W / SCALE, LEG_H / SCALE)), density=1.0, restitution=0.0, categoryBits=0x0020, maskBits=0x001)) leg.ground_contact = False leg.color1 = (0.5, 0.4, 0.9) leg.color2 = (0.3, 0.3, 0.5) rjd = revoluteJointDef( bodyA=self.lander, bodyB=leg, localAnchorA=(0, 0), localAnchorB=(i * LEG_AWAY / SCALE, LEG_DOWN / SCALE), enableMotor=True, enableLimit=True, maxMotorTorque=LEG_SPRING_TORQUE, motorSpeed=+0.3 * i # low enough not to jump back into the sky ) if i == -1: rjd.lowerAngle = +0.9 - 0.5 # Yes, the most esoteric numbers here, angles legs have freedom to travel within rjd.upperAngle = +0.9 else: rjd.lowerAngle = -0.9 rjd.upperAngle = -0.9 + 0.5 leg.joint = self.world.CreateJoint(rjd) self.legs.append(leg) self.drawlist = [self.lander] + self.legs return self.step(np.array([0, 0]) if self.continuous else 0)[0]
def draw_walker(self): self._generate_terrain() self._generate_clouds() init_x = self.TERRAIN_STEP*self.TERRAIN_STARTPAD/2 init_y = self.TERRAIN_HEIGHT+2*self.LEG_H self.hull = self.world.CreateDynamicBody( position = (init_x, init_y), fixtures = self.HULL_FD ) self.hull.color1 = (0.5,0.4,0.9) self.hull.color2 = (0.3,0.3,0.5) self.hull.ApplyForceToCenter((self.np_random.uniform(-self.INITIAL_RANDOM, self.INITIAL_RANDOM), 0), True) self.legs = [] self.joints = [] for x_anchor in self.leg_pairs_x: absolute_x = init_x + np.interp(x_anchor,[-1,1],[-self.HULL_WIDTH/2/self.SCALE,self.HULL_WIDTH/2/self.SCALE]) for i in [-1, +1]: leg = self.world.CreateDynamicBody( position=(absolute_x, init_y - self.LEG_H / 2 - self.LEG_DOWN), angle=(i * 0.05), fixtures=self.LEG_FD ) leg.color1 = (0.6 - i / 10., 0.3 - i / 10., 0.5 - i / 10.) leg.color2 = (0.4 - i / 10., 0.2 - i / 10., 0.3 - i / 10.) rjd = revoluteJointDef( bodyA=self.hull, bodyB=leg, localAnchorA=(x_anchor, self.LEG_DOWN), localAnchorB=(0, self.LEG_H / 2), enableMotor=True, enableLimit=True, maxMotorTorque=self.MOTORS_TORQUE, motorSpeed=i, lowerAngle=-0.8, upperAngle=1.1, ) self.legs.append(leg) self.joints.append(self.world.CreateJoint(rjd)) lower = self.world.CreateDynamicBody( position=(absolute_x, init_y - self.LEG_H * 3 / 2 - self.LEG_DOWN), angle=(i * 0.05), fixtures=self.LOWER_FD ) lower.color1 = (0.6 - i / 10., 0.3 - i / 10., 0.5 - i / 10.) lower.color2 = (0.4 - i / 10., 0.2 - i / 10., 0.3 - i / 10.) rjd = revoluteJointDef( bodyA=leg, bodyB=lower, localAnchorA=(0, -self.LEG_H / 2), localAnchorB=(0, self.LEG_H / 2), enableMotor=True, enableLimit=True, maxMotorTorque=self.MOTORS_TORQUE, motorSpeed=1, lowerAngle=-1.6, upperAngle=-0.1, ) lower.ground_contact = False self.legs.append(lower) self.joints.append(self.world.CreateJoint(rjd))