def car(self, space): pos = Vec2d(100, 100) wheel_color = 0.2, 0.86, 0.47 shovel_color = 0.86, 0.47, 0.2 mass = 100 radius = 25 moment = pymunk.moment_for_circle(mass, 20, radius) wheel1_b = pymunk.Body(mass, moment) wheel1_s = pymunk.Circle(wheel1_b, radius) wheel1_s.friction = 1.5 wheel1_s.color = wheel_color space.add(wheel1_b, wheel1_s) mass = 100 radius = 25 moment = pymunk.moment_for_circle(mass, 20, radius) wheel2_b = pymunk.Body(mass, moment) wheel2_s = pymunk.Circle(wheel2_b, radius) wheel2_s.friction = 1.5 wheel2_s.color = wheel_color space.add(wheel2_b, wheel2_s) mass = 100 size = (50, 30) moment = pymunk.moment_for_box(mass, size) chassi_b = pymunk.Body(mass, moment) chassi_s = pymunk.Poly.create_box(chassi_b, size) space.add(chassi_b, chassi_s) vs = [(0, 0), (0, -45), (25, -45)] shovel_s = pymunk.Poly(chassi_b, vs, transform=pymunk.Transform(tx=85)) shovel_s.friction = 0.5 shovel_s.color = shovel_color space.add(shovel_s) wheel1_b.position = pos - (55, 0) wheel2_b.position = pos + (55, 0) chassi_b.position = pos + (0, 25) space.add( pymunk.PinJoint(wheel1_b, chassi_b, (0, 0), (-25, -15)), pymunk.PinJoint(wheel1_b, chassi_b, (0, 0), (-25, 15)), pymunk.PinJoint(wheel2_b, chassi_b, (0, 0), (25, -15)), pymunk.PinJoint(wheel2_b, chassi_b, (0, 0), (25, 15)), ) speed = -4 space.add( pymunk.SimpleMotor(wheel1_b, chassi_b, speed), pymunk.SimpleMotor(wheel2_b, chassi_b, speed), ) with self.canvas: Color(*wheel_color) wheel1_s.ky = self.ellipse_from_circle(wheel1_s) Color(*wheel_color) wheel2_s.ky = self.ellipse_from_circle(wheel2_s) Color(*shovel_color) chassi_s.ky = Quad(points=self.points_from_poly(chassi_s)) shovel_s.ky = Triangle(points=self.points_from_poly(shovel_s))
def create_motors(self, space): self.speed = 0 self.motors = [ pymunk.SimpleMotor(self.wheel1_body, self.chassi_body, self.speed), pymunk.SimpleMotor(self.wheel2_body, self.chassi_body, self.speed) ] space.add(self.motors[0], self.motors[1])
def add_rot(self): rotation_center_body = pymunk.Body(body_type=pymunk.Body.STATIC) rotation_center_body.position = (500, 410) # 1st rotation body = pymunk.Body(10, 10000) body.position = (500, 410) l1 = pymunk.Segment(body, (-55, 0), (55.0, 0.0), 2.0) # 2nd rotation rotation_center_body2 = pymunk.Body(body_type=pymunk.Body.STATIC) rotation_center_body2.position = (770, 410) body2 = pymunk.Body(10, 10000) body2.position = (770, 410) l2 = pymunk.Segment(body2, (-55, 0), (55.0, 0.0), 2.0) rotation_center_joint = pymunk.PinJoint(body, rotation_center_body, (0, 0), (0, 0)) rotation_center_joint2 = pymunk.PinJoint(body2, rotation_center_body2, (0, 0), (0, 0)) tw = pymunk.SimpleMotor(body, rotation_center_body, 0.7) tw2 = pymunk.SimpleMotor(body2, rotation_center_body2, -0.7) self.space.add(l1, l2, body, body2, rotation_center_joint, rotation_center_joint2, tw, tw2) return l1, l2
def add_robot(space, seat): #define robot upper body and upper leg ubt = Vec2d(3.5, 0) robot_u_points = [ Vec2d(-4, 31.64) + ubt, Vec2d(-4, 0) + ubt, Vec2d(4, 0) + ubt, Vec2d(4, 31.64) + ubt ] robot_body = pymunk.Body() robot_body.position = seat.position + (0, 3) robot_u = pymunk.Poly(robot_body, robot_u_points) robot_u.mass = 3.311 robot_u.color = THECOLORS["red"] robot_u.filter = pymunk.ShapeFilter(mask=pymunk.ShapeFilter.ALL_MASKS ^ 1) robot_u_leg = pymunk.Poly(seat, [(1, 3), (-10.5, 3), (-10.5, 8), (1, 8)]) robot_u_leg.color = THECOLORS["red"] robot_u_leg.mass = 0.603 #robot_u_leg_extra = pymunk.Circle(seat,2,(5,0)) #robot_u_leg_extra.mass = 2 #define robot lower leg robot_leg = pymunk.Body() robot_leg.position = seat.position robot_l_leg = pymunk.Poly(robot_leg, [(-10.5, 5), (-10.5, -11.8), (-4.0, -11.8), (-4.0, 5)]) robot_l_leg.mass = 1.214 robot_l_leg.color = THECOLORS["red"] space.add(robot_body, robot_u, robot_u_leg, robot_leg, robot_l_leg) #motor and pivot for hip #uses measured values of angles rather than given in program seat_motor = pymunk.SimpleMotor(seat, robot_body, 0) seat_motor.max_force = 1e6 seat_pivot = pymunk.PivotJoint(seat, robot_body, robot_body.position + ubt) seat_pivot._set_collide_bodies(False) seat_pivot_lim = pymunk.RotaryLimitJoint(robot_body, seat, 0, 0.575959) space.add(seat_motor, seat_pivot, seat_pivot_lim) #motor and pivot for knee max_knee_ang = 5 max_knee_ang = np.deg2rad(max_knee_ang) knee_motor = pymunk.SimpleMotor(seat, robot_leg, 0) knee_motor.max_force = 1e5 knee_pivot = pymunk.PivotJoint(seat, robot_leg, seat.position + (-8, 7)) knee_pivot._set_collide_bodies(False) knee_pivot_lim = pymunk.RotaryLimitJoint(seat, robot_leg, max_knee_ang - np.deg2rad(69), max_knee_ang) space.add(knee_motor, knee_pivot, knee_pivot_lim) return seat_motor, knee_motor, robot_body, robot_leg, robot_u_leg
def car(space): import pymunk from pymunk.vec2d import Vec2d space.gravity = 0, -9.8 pos = Vec2d(300, 200) wheel_color = 52, 219, 119 shovel_color = 219, 119, 52 mass = 100 radius = 25 moment = pymunk.moment_for_circle(mass, 20, radius) wheel1_b = pymunk.Body(mass, moment) wheel1_s = pymunk.Circle(wheel1_b, radius) wheel1_s.friction = 1.5 wheel1_s.color = wheel_color space.add(wheel1_b, wheel1_s) mass = 100 radius = 25 moment = pymunk.moment_for_circle(mass, 20, radius) wheel2_b = pymunk.Body(mass, moment) wheel2_s = pymunk.Circle(wheel2_b, radius) wheel2_s.friction = 1.5 wheel2_s.color = wheel_color space.add(wheel2_b, wheel2_s) mass = 100 size = (50, 30) moment = pymunk.moment_for_box(mass, size) chassi_b = pymunk.Body(mass, moment) chassi_s = pymunk.Poly.create_box(chassi_b, size) space.add(chassi_b, chassi_s) vs = [(0, 0), (25, -45), (0, -45)] shovel_s = pymunk.Poly(chassi_b, vs, transform=pymunk.Transform(tx=85)) shovel_s.friction = 0.5 shovel_s.color = shovel_color space.add(shovel_s) wheel1_b.position = pos - (55, 0) wheel2_b.position = pos + (55, 0) chassi_b.position = pos + (0, 25) space.add(pymunk.PinJoint(wheel1_b, chassi_b, (0, 0), (-25, -15)), pymunk.PinJoint(wheel1_b, chassi_b, (0, 0), (-25, 15)), pymunk.PinJoint(wheel2_b, chassi_b, (0, 0), (25, -15)), pymunk.PinJoint(wheel2_b, chassi_b, (0, 0), (25, 15))) speed = 4 space.add(pymunk.SimpleMotor(wheel1_b, chassi_b, speed), pymunk.SimpleMotor(wheel2_b, chassi_b, speed)) floor = pymunk.Segment(space.static_body, (-100, 100), (1000, 100), 5) floor.friction = 1.0 space.add(floor)
def car(space): pos = Vec2d(100, 200) wheel_color = 52, 219, 119, 255 shovel_color = 219, 119, 52, 255 mass = 100 radius = 25 moment = pymunk.moment_for_circle(mass, 20, radius) wheel1_b = pymunk.Body(mass, moment) wheel1_s = pymunk.Circle(wheel1_b, radius) wheel1_s.friction = 1.5 wheel1_s.color = wheel_color space.add(wheel1_b, wheel1_s) mass = 100 radius = 25 moment = pymunk.moment_for_circle(mass, 20, radius) wheel2_b = pymunk.Body(mass, moment) wheel2_s = pymunk.Circle(wheel2_b, radius) wheel2_s.friction = 1.5 wheel2_s.color = wheel_color space.add(wheel2_b, wheel2_s) mass = 100 size = (50, 30) moment = pymunk.moment_for_box(mass, size) chassi_b = pymunk.Body(mass, moment) chassi_s = pymunk.Poly.create_box(chassi_b, size) space.add(chassi_b, chassi_s) vs = [(0, 0), (25, 45), (0, 45)] shovel_s = pymunk.Poly(chassi_b, vs, transform=pymunk.Transform(tx=85)) shovel_s.friction = 0.5 shovel_s.color = shovel_color space.add(shovel_s) wheel1_b.position = pos - (55, 0) wheel2_b.position = pos + (55, 0) chassi_b.position = pos + (0, -25) space.add( pymunk.PinJoint(wheel1_b, chassi_b, (0, 0), (-25, -15)), pymunk.PinJoint(wheel1_b, chassi_b, (0, 0), (-25, 15)), pymunk.PinJoint(wheel2_b, chassi_b, (0, 0), (25, -15)), pymunk.PinJoint(wheel2_b, chassi_b, (0, 0), (25, 15)), ) speed = 4 space.add( pymunk.SimpleMotor(wheel1_b, chassi_b, speed), pymunk.SimpleMotor(wheel2_b, chassi_b, speed), )
def _agent_grasps(self, arbiter, space, data): agent = self._get_agent_from_shape(arbiter.shapes[0]) body_part = agent.get_bodypart_from_shape(arbiter.shapes[0]) interacting_entity = self._get_scene_element_from_shape( arbiter.shapes[1]) if interacting_entity is None: return True if body_part.is_grasping and not body_part.is_holding: body_part.is_holding = True j_1 = pymunk.PinJoint(body_part.pm_body, interacting_entity.pm_body, (0, 5), (0, 0)) j_2 = pymunk.PinJoint(body_part.pm_body, interacting_entity.pm_body, (0, -5), (0, 0)) motor = pymunk.SimpleMotor(body_part.pm_body, interacting_entity.pm_body, 0) self.space.add(j_1, j_2, motor) body_part.grasped = [j_1, j_2, motor] self._grasped_scene_elements[interacting_entity] = body_part return True
def __init__(self, space, base_body, body_pos, length, angle, mass=1, thickness=5): b = body_pos dif = (math.sin(angle) * length, math.cos(angle) * length) v = b + dif base_dif = body_pos - base_body.position link_body = pymunk.Body(mass, mass * length * length / 3) link_body.position = body_pos self.link = pymunk.Segment(link_body, (0,0), dif, thickness) self.link.filter = pymunk.ShapeFilter(categories=0b1, mask=0b0) link_body.moment = pymunk.moment_for_segment(mass, self.link.a, self.link.b, thickness) link_body.center_of_gravity = (self.link.a + self.link.b)/2.0 base_motor = pymunk.SimpleMotor(base_body, link_body, 0) base_joint = pymunk.PinJoint(base_body, link_body, base_dif, (0,0)) base_joint.error_bias = 0.0001 base_clamp = pymunk.RotaryLimitJoint(base_body, link_body, (-3*math.pi/4), (3*math.pi/4)) space.add(self.link, link_body, base_motor, base_joint, base_clamp) print("Body pos: " + str(link_body.position)) print("COG pos: " + str(link_body.center_of_gravity)) print("a" + str(self.link.a)) print("b" + str(self.link.b)) self.motor = base_motor self.body = link_body self.start_tip = v
def generatePerson(self): body = pymunk.Body( 0.75 * self.mass, 100000000000000 ) # assumes the body from the quads up make up 75% of mass body.position = self.pos legs = pymunk.Body(0.25 * self.mass, 100) legs.position = self.pos torso_shape = pymunk.Segment(body, (0, 0), (0, -30), 3) bottom_shape = pymunk.Segment(body, (0, 0), (20, 0), 3) legs_shape = pymunk.Segment(legs, (20, 0), (20, 20), 3) knee_joint = pymunk.PinJoint(legs, body, (20, 0), (20, 0)) knee_motor = pymunk.SimpleMotor(legs, body, 0) knee_joint.collide_bodies = False self.space.add(body, torso_shape, bottom_shape, legs, legs_shape, knee_joint, knee_motor) return { 'body': [(body, torso_shape, legs_shape), (legs, legs_shape)], 'pivots': [knee_joint, knee_motor] }
def __init__(self, space, chromosome=None): self.lifespan = 1000 self.is_alive = True self.checkpoint_index = 0 # Generate the chromosome for the car. self.chromosome = chromosome if self.chromosome is None: self.chromosome = Chromosome.generate_inital_chromosome() # Create a chassis. position_main = pymunk.Vec2d(START_POSITION) vs = self.chromosome.get_vertices() self.chassis = Chassis(space, position_main, vs) self.position = self.chassis.body.position # Create the wheels. self.wheels = [] genes = self.chromosome.get_genes() for position, [radius, speed] in genes: if radius == -1 or speed == -1: continue position = pymunk.Vec2d(position) + position_main wheel = Wheel(space, position, radius) c = pymunk.constraint.PivotJoint(self.chassis.body, wheel.body, position) c.collide_bodies = False m = pymunk.SimpleMotor(self.chassis.body, wheel.body, speed) space.add(c, m) self.wheels.append(wheel) self.__setup_draw()
def generateSwingSystem(self): swingConfig = config["LswingConfig"] #Generate top pivot top = pymunk.Body(10, 1000000, pymunk.Body.STATIC) top.position = Vec2d(*swingConfig['topPosition']) top_shape = pymunk.Poly.create_box(top, (20, 20)) top_shape.filter = pymunk.ShapeFilter( categories=0b1, mask=pymunk.ShapeFilter.ALL_MASKS() ^ 0b1) space.add(top, top_shape) #Generate swing p = top.position b = top jointVector = (0, 0) segmentArray = [] for i, j in enumerate(swingConfig["joints"]): print(i) angle = j[0] + self.theta v = j[1] * Vec2d(np.cos(angle * np.pi / 180), np.sin(angle * np.pi / 180)) shape = Segment(p, v, j[2]) segmentArray.append(shape) joint = PivotJoint(b, shape.body, jointVector) motor = pymunk.SimpleMotor(b0, shape.body, rate=0) space.add(motor) jointVector = v b = shape.body p = p + v self.swingSegmentArray = segmentArray
def add_arm(self, segment_lengths, anchor_position): anchor = pymunk.Body(body_type=pymunk.Body.STATIC) anchor.position = anchor_position self.space.add(anchor) segment_anchor = anchor next_anchor_pos = anchor_position for i, segment_length in enumerate(segment_lengths): segment_size = segment_length, 10 segment_body = pymunk.Body(10, pymunk.moment_for_box(10, segment_size)) end_effector_shape = pymunk.Poly.create_box(segment_body, segment_size) end_effector_shape.collision_type = NO_COLLISION_TYPE end_effector_shape.friction = 1.0 end_effector_shape.elasticity = 0.1 alpha = random.random() * math.pi * 2 dx = np.cos(alpha) * segment_length / 2 dy = np.sin(alpha) * segment_length / 2 segment_body.position = next_anchor_pos[0] - dx, next_anchor_pos[1] - dy next_anchor_pos = (next_anchor_pos[0] - 2 * dx, next_anchor_pos[1] - 2 * dy) segment_body.angle = alpha anchor_pin_pos = (0 if i == 0 else -segment_lengths[i - 1] / 2, 0) pin = pymunk.PinJoint(segment_anchor, segment_body, anchor_pin_pos, (segment_length / 2, 0)) self.space.add(pin) self.space.add(segment_body, end_effector_shape) motor = pymunk.SimpleMotor(segment_anchor, segment_body, 0) motor.max_force = self.max_motor_force self.space.add(motor) self.motors.append(motor) self.segment_bodies.append(segment_body) segment_anchor = segment_body return segment_anchor, next_anchor_pos
def spider_create(space): pos = Vec2d(100, 200) first_arm_color = 0, 0, 0 second_arm_color = 50, 50, 50 wheel_color = 52, 219, 119 mass = 100 radius = 10 moment = pymunk.moment_for_circle(mass, 20, radius) wheel1_b = pymunk.Body(mass, moment) wheel1_s = pymunk.Circle(wheel1_b, radius) wheel1_s.friction = 1.5 wheel1_s.color = wheel_color space.add(wheel1_b, wheel1_s) mass = 100 radius = 10 moment = pymunk.moment_for_circle(mass, 20, radius) wheel2_b = pymunk.Body(mass, moment) wheel2_s = pymunk.Circle(wheel2_b, radius) wheel2_s.friction = 1.5 wheel2_s.color = wheel_color space.add(wheel2_b, wheel2_s) mass = 100 size = (50, 30) moment = pymunk.moment_for_box(mass, size) chassi_b = pymunk.Body(mass, moment) chassi_s = pymunk.Poly.create_box(chassi_b, size) space.add(chassi_b, chassi_s) wheel1_b.position = pos - (55, 0) wheel2_b.position = pos + (55, 0) chassi_b.position = pos + (0, -25) space.add(pymunk.PinJoint(wheel1_b, chassi_b, (0, 0), (-25, -15)), pymunk.PinJoint(wheel1_b, chassi_b, (0, 0), (-25, 15)), pymunk.PinJoint(wheel2_b, chassi_b, (0, 0), (25, -15)), pymunk.PinJoint(wheel2_b, chassi_b, (0, 0), (25, 15))) first_arm = pymunk.Segment(chassi_b, (25, -15), (30, -40), 2) space.add(first_arm) speed = 4 space.add(pymunk.SimpleMotor(wheel1_b, chassi_b, speed), pymunk.SimpleMotor(wheel2_b, chassi_b, speed))
def __init__(self, space, shape1, speed): # Associate the motor with the location of one of the bodies so # it is removed when that body is out of the simulation self.body = shape1.body self.shape = pymunk.SimpleMotor(shape1.body, space.static_body, speed) super().__init__() space.add(self.shape)
def add_motor(self): angular_velocity = 5 #motor_joint=pymunk.SimpleMotor(self.torso[0],self.crank_arm[0],angular_velocity) #pin_joint=pymunk.PinJoint(self.torso[0],self.crank_arm[0],(0,0),(0,0)) #motor_joint.max_force=50 motor_joint = pymunk.SimpleMotor(self.crank_arm[0], self.torso[0], angular_velocity) return motor_joint
def __init__(self): moment = pymunk.moment_for_box(100, (100, 30)) self.body = pymunk.Body(100, moment) self.shape = pymunk.Poly.create_box(self.body, (100, 30)) self.body.position = (1200, 200) self.shape.body = self.body self.shape.color = (150, 150, 0, 255) space.add(self.body, self.shape) wheel_moment = pymunk.moment_for_circle(100, 20, 25) self.wheel1_body = pymunk.Body(100, wheel_moment) self.wheel1_body.position = self.body.position + (-80, -20) self.wheel1_shape = pymunk.Circle(self.wheel1_body, 25) self.wheel1_shape.friction = 1.5 self.wheel1_shape.color = (200, 200, 200) self.wheel1_joint = pymunk.PinJoint(self.body, self.wheel1_body, (-50, 0), (0, 0)) self.wheel1_joint2 = pymunk.PinJoint(self.body, self.wheel1_body, (-50, -15), (0, 0)) self.wheel1_motor = pymunk.SimpleMotor(self.wheel1_body, self.body, -5) space.add(self.wheel1_body, self.wheel1_shape, self.wheel1_joint, self.wheel1_joint2, self.wheel1_motor) wheel_moment = pymunk.moment_for_circle(100, 20, 25) self.wheel2_body = pymunk.Body(100, wheel_moment) self.wheel2_body.position = self.body.position + (80, -20) self.wheel2_shape = pymunk.Circle(self.wheel2_body, 25) self.wheel2_shape.friction = 1.5 self.wheel2_shape.color = (200, 200, 200) self.wheel2_joint = pymunk.PinJoint(self.body, self.wheel2_body, (50, 0), (0, 0)) self.wheel2_joint2 = pymunk.PinJoint(self.body, self.wheel2_body, (50, -15), (0, 0)) self.wheel2_motor = pymunk.SimpleMotor(self.wheel2_body, self.body, -5) space.add(self.wheel2_body, self.wheel2_shape, self.wheel2_joint, self.wheel2_joint2, self.wheel2_motor) self.human = pygame.image.load('human.png') self.human = pygame.transform.scale(self.human, (100, 150)) self.huk = pygame.image.load('huk.png') self.huk = pygame.transform.scale(self.huk, (100, 150)) self.is_dead = False self.catch = 0
def __init__(self, a, b, position, range=(-pi, pi), max_rate=2.0): self.pivot = pymunk.PivotJoint(a, b, position) self.motor = pymunk.SimpleMotor(a, b, 0) self.motor.max_force = 1e7 self.limit = pymunk.RotaryLimitJoint(a, b, range[0], range[1]) self.limit.max_force = 1e1 self.limit.max_bias = 0.5 self.max_rate = max_rate
def __init__(self): c1 = Circle((100, 100), 30) c2 = Circle((300, 100), 30) b = Rectangle((200, 150), size=(100, 60), color=GREEN) j0 = pymunk.PinJoint(c1.body, b.body, (0, 0), (-50, -30)) j0.color = RED j1 = pymunk.PinJoint(c1.body, b.body, (0, 0), (-50, 30)) j1.color = BLUE j2 = pymunk.PinJoint(c2.body, b.body, (0, 0), (50, -30)) j3 = pymunk.PinJoint(c2.body, b.body, (0, 0), (50, 30)) App.current.space.add(j0, j1, j2, j3) speed = -1 App.current.space.add(pymunk.SimpleMotor(c1.body, b.body, speed), pymunk.SimpleMotor(c2.body, b.body, speed))
def build(space): global _collisions_added logger.info('building zombie model') if not _collisions_added: _collisions_added = True add_collision_handler(space) model = Model() # build body layers = 1 body_rect = pygame.Rect(0, 0, 32, 47) body_body, body_shape = make_body(body_rect) body_shape.elasticity = 0 body_shape.layers = layers body_shape.friction = 1 body_sprite = Sprite(body_shape) space.add(body_body, body_shape) # build feet layers = 2 feet_body, feet_shape = make_feet(body_rect) feet_shape.elasticity = 0 feet_shape.layers = layers feet_shape.friction = pymunk.inf feet_sprite = SanicForeverSprite(feet_shape) space.add(feet_body, feet_shape) # jump/collision sensor size = body_rect.width, body_rect.height * 1.05 offset = 0, -body_rect.height * .05 sensor = pymunk.Poly.create_box(body_body, size, offset) sensor.collision_type = collisions.enemy sensor.sensor = True sensor.model = model space.add(sensor) # attach feet to body feet_body.position = (body_body.position.x, body_body.position.y - feet_shape.radius * .7) # motor and joint for feet motor = pymunk.SimpleMotor(body_body, feet_body, 0.0) joint = pymunk.PivotJoint( body_body, feet_body, feet_body.position, (0, 0)) space.add(motor, joint) # the model is used to gameplay logic model.sprite = body_sprite model.feet = feet_sprite model.motor = motor model.joint = joint model.sensor = sensor return model
def create_wheel(self, wheel_side): if wheel_side not in ['rear', 'front']: raise Exception('Wheel position must be front or rear') wheel_objects = [] wheel_mass = getattr(self, wheel_side + '_wheel_mass') wheel_radius = getattr(self, wheel_side + '_wheel_radius') wheel_position = getattr(self, wheel_side + '_wheel_position') wheel_friction = getattr(self, wheel_side + '_wheel_friction') wheel_elasticity = getattr(self, wheel_side + '_wheel_elasticity') wheel_damp_position = getattr(self, wheel_side + '_wheel_damp_position') wheel_damp_length = getattr(self, wheel_side + '_wheel_damp_length') wheel_damp_stiffness = getattr(self, wheel_side + '_wheel_damp_stiffness') wheel_damp_damping = getattr(self, wheel_side + '_wheel_damp_damping') wheel_body = pymunk.Body( wheel_mass, pymunk.moment_for_box(wheel_mass, (wheel_radius * 2, wheel_radius * 2))) wheel_body.position = (wheel_position[0] * self.x_modification, wheel_position[1]) wheel_shape = pymunk.Poly.create_box( wheel_body, (wheel_radius * 2, wheel_radius * 2)) wheel_shape.filter = pymunk.ShapeFilter(group=self.car_group) wheel_shape.color = 255, 34, 150 wheel_shape.friction = wheel_friction wheel_shape.elasticity = wheel_elasticity wheel_objects.append(wheel_shape) wheel_grove = pymunk.GrooveJoint( self.car_body, wheel_body, (wheel_damp_position[0] * self.x_modification, wheel_damp_position[1]), (wheel_damp_position[0] * self.x_modification, wheel_damp_position[1] - wheel_damp_length * 1.5), (0, 0)) wheel_objects.append(wheel_grove) wheel_damp = pymunk.DampedSpring( wheel_body, self.car_body, anchor_a=(0, 0), anchor_b=(wheel_damp_position[0] * self.x_modification, wheel_damp_position[1]), rest_length=wheel_damp_length, stiffness=wheel_damp_stiffness, damping=wheel_damp_damping) wheel_objects.append(wheel_damp) wheel_motor = None if (wheel_side == 'rear' and self.drive in [self.AWD, self.FR]) or ( wheel_side == 'front' and self.drive in [self.AWD, self.FF]): wheel_motor = pymunk.SimpleMotor(wheel_body, self.car_body, 0) return wheel_body, wheel_motor, wheel_objects
def build(space, group): # type: (pymunk.Space, pygame.sprite.AbstractGroup) -> CatModel """ Builds our unicycle cat. :param space: The pymunk space to put the cat in. :param group: The pygame sprite group to put the cat in. """ scale = 2 normal_rect = pygame.Rect(0, 0, 32 * scale, 40 * scale) cat_model = CatModel() sprites = [] pymunk_objects = [] filter1 = pymunk.ShapeFilter(group=0b000001) # Main body body_body = pymunk.Body(0.00001, pymunk.inf) body_body.position = normal_rect.topleft cat_model.main_body = body_body pymunk_objects.append(body_body) # seat seat_body = build_seat(filter1, normal_rect, pymunk_objects, sprites) # build feet cat_model.feet, feet_sprite = build_feet(filter1, normal_rect, pymunk_objects, body_body, seat_body) sprites.append(feet_sprite) # Add motor cat_model.motor = pymunk.SimpleMotor(body_body, cat_model.feet, 0.0) pymunk_objects.append(cat_model.motor) # cat cat_body, cat_rect = build_cat(normal_rect, pymunk_objects, sprites) # hold cat the the seat spring = pymunk.DampedSpring(seat_body, cat_body, normal_rect.midtop, cat_rect.midbottom, 0, 1, 0) pymunk_objects.append(spring) # tilt corrector spring = pymunk.DampedRotarySpring(body_body, seat_body, radians(0), 60000, 20000) spring.collide_bodies = False pymunk_objects.append(spring) cat_model.sprites = sprites cat_model.pymunk_objects = pymunk_objects space.add(pymunk_objects) group.add(*sprites) return cat_model
def __init__(self, mass=DEFAULT_MASS, position=(G_SCREEN_WIDTH / 2, G_SCREEN_HEIGHT / 4)): self._fuel = 500 self.force = DEFAULT_FORCE self.wheels = [] self.flying_missiles = [] # Anti-spacecraft wheels self.wheel1_b, self.wheel1_s = self.create_body( mass, (position[0] - ANTI_SPACECRAFT_CHASSIS[0] / 1.5, position[1]), ANTI_SPACECRAFT_WHEEL_SIZE) self.wheel2_b, self.wheel2_s = self.create_body( mass, (position[0] + ANTI_SPACECRAFT_CHASSIS[0] / 1.5, position[1]), ANTI_SPACECRAFT_WHEEL_SIZE) self.wheels.extend((self.wheel1_b, self.wheel2_b)) # Anti-spacecraft chassis self.chassis_b, self.chassis_s = self.create_body( mass / 5, position, ANTI_SPACECRAFT_CHASSIS) self.chassis_s.color = 255, 155, 0 # Create cannon self.cannon_b, self.cannon_s = self.create_body( 0.01, (position[0] - ANTI_SPACECRAFT_CHASSIS[0] / 2, position[1] + ANTI_SPACECRAFT_CHASSIS[1] / 2), ANTI_SPACECRAFT_CANNON) # Create missiles self.missile_body, self.missile_shape = self.create_missile() # Anti-spacecraft joints # TODO: Use for-loop (?) self.pin1 = pymunk.PinJoint(self.wheel1_b, self.chassis_b, (0, 0), (-ANTI_SPACECRAFT_CHASSIS[0] / 2, 0)) self.pin2 = pymunk.PinJoint(self.wheel2_b, self.chassis_b, (0, 0), (ANTI_SPACECRAFT_CHASSIS[0] / 2, 0)) self.pin3 = pymunk.PinJoint(self.wheel1_b, self.chassis_b, (0, 0), (0, ANTI_SPACECRAFT_CHASSIS[1] / 2)) self.pin4 = pymunk.PinJoint(self.wheel2_b, self.chassis_b, (0, 0), (0, ANTI_SPACECRAFT_CHASSIS[1] / 2)) self.pin5 = pymunk.PinJoint(self.wheel1_b, self.chassis_b, (0, 0), (0, -ANTI_SPACECRAFT_CHASSIS[1] / 2)) self.pin6 = pymunk.PinJoint(self.wheel2_b, self.chassis_b, (0, 0), (0, -ANTI_SPACECRAFT_CHASSIS[1] / 2)) self.pin7 = pymunk.PinJoint(self.wheel2_b, self.chassis_b, (0, 0), (0, -ANTI_SPACECRAFT_CHASSIS[1] / 2)) self.pin8 = pymunk.PinJoint(self.cannon_b, self.chassis_b, (ANTI_SPACECRAFT_CANNON[0] / 2, 0), (0, ANTI_SPACECRAFT_CHASSIS[1] / 2)) self.cannon_mt = pymunk.SimpleMotor(self.cannon_b, self.chassis_b, 0) self.cannon_mt.collide_bodies = False
def __init__(self, floor, leg_mass=5., leg_width=2., leg_length=40., leg_friction=.4, init_leg_angle=30): self.leg_length = leg_length self.leg_mass = leg_mass self.leg_width = leg_width self.leg_friction = leg_friction self.legs_min_angle = 0. self.legs_max_angle = .9 * math.pi self.leg1, self.joint_pos_l1 = self.create_leg( self.leg_mass, self.leg_length, self.leg_width, math.radians(init_leg_angle), leg_friction, pygame.color.THECOLORS['red']) self.leg2, self.joint_pos_l2 = self.create_leg( self.leg_mass, self.leg_length, self.leg_width, math.radians(-init_leg_angle), leg_friction, pygame.color.THECOLORS['green']) leg_position_x = floor.body.position.x / 8. leg_position_y = floor.body.position.y self.place_leg(self.leg1, leg_position_x, leg_position_y, math.radians(init_leg_angle)) self.place_leg(self.leg2, leg_position_x, leg_position_y, math.radians(-init_leg_angle)) self.rotation_center_legs_joint = pymunk.PivotJoint( self.leg1.body, self.leg2.body, self.joint_pos_l2, self.joint_pos_l1) self.rotation_center_legs_joint.collide_bodies = False self.leg1_floor_motor = pymunk.SimpleMotor(self.leg1.body, floor.body, 0.) self.leg2_floor_motor = pymunk.SimpleMotor(self.leg2.body, floor.body, 0.) self.legs_rotary_limit_joint = pymunk.RotaryLimitJoint( self.leg1.body, self.leg2.body, self.legs_min_angle, self.legs_max_angle)
def testPickle(self): a, b = p.Body(10, 10), p.Body(20, 20) j = p.SimpleMotor(a, b, 1) s = pickle.dumps(j) j2 = pickle.loads(s) self.assertEqual(j.rate, j2.rate) self.assertEqual(j.a.mass, j2.a.mass) self.assertEqual(j.b.mass, j2.b.mass)
def __init__(self, pos, colour, sim): self.sim = sim self.colour = colour self.group = self.sim.groups self.sim.groups += 1 space = self.sim.space self.robot = self.add_robot(space, pos) self.kickzone = self.add_kickzone(space) self.wheel_left = self.add_wheel_left(space) self.wheel_right = self.add_wheel_right(space) self.cons1 = pymunk.SimpleMotor(self.robot.body, self.wheel_left.body, 0) self.cons2 = pymunk.SimpleMotor(self.robot.body, self.wheel_right.body, 0) space.add(self.cons1, self.cons2) SimRobotInterface.__init__(self)
def createSqr(self, position): mass = 5 inertia = pymunk.moment_for_box(mass, (15, 15)) body = pymunk.Body(mass, inertia) body.position = position shape = pymunk.Poly.create_box(body, (15, 15)) shape.elasticity = 0.95 shape.friction = 0.5 self.space.add(body, shape) self.space.add(pymunk.SimpleMotor(body, self.space.static_body, -4)) self.objects.append(shape)
def _attach_to_anchor(self): self.joint = pymunk.PivotJoint(self.anchor.pm_body, self.pm_body, self._rel_coord_anchor, self._rel_coord_part) self.joint.collide_bodies = False self.limit = pymunk.RotaryLimitJoint(self.anchor.pm_body, self.pm_body, self.angle_offset - self.rotation_range / 2, self.angle_offset + self.rotation_range / 2) self.motor = pymunk.SimpleMotor(self.anchor.pm_body, self.pm_body, 0) self.pm_elements += [self.joint, self.motor, self.limit]
def testSimpleMotor(self): a, b = p.Body(10, 10), p.Body(20, 20) j = p.SimpleMotor(a, b, 0.3) self.assertEqual(j.rate, 0.3) j.rate = 0.4 self.assertEqual(j.rate, 0.4) j.max_bias = 30 j.bias_coef = 40 j.max_force = 50 self.assertEqual(j.max_bias, 30) self.assertEqual(j.bias_coef, 40) self.assertEqual(j.max_force, 50)
def createPoly(self, position): mass = 5 vertices = [(20, 0), (0, 20), (0, -20)] inertia = pymunk.moment_for_poly(mass, vertices) body = pymunk.Body(mass, inertia) body.position = position shape = pymunk.Poly(body, vertices) shape.elasticity = 0.95 shape.friction = 0.5 self.space.add(body, shape) self.space.add(pymunk.SimpleMotor(body, self.space.static_body, -4)) self.objects.append(shape)
def createCirc(self, position, radius, motor=False): mass = 5 inertia = pymunk.moment_for_circle(mass, 0, radius) body = pymunk.Body(mass, inertia) body.position = position shape = pymunk.Circle(body, radius) shape.elasticity = 0.95 shape.friction = 0.5 self.space.add(body, shape) if motor: self.space.add(pymunk.SimpleMotor(body, self.space.static_body, -8)) self.objects.append(shape)