def main(): car = Body(17500 / G, (30.5, 30.5)) driver = Body(850 / G, (31.5, 31)) fuel = Body(993 / G, (28, 30.5)) total_mass = sum([car.mass, driver.mass, fuel.mass]) total_weight = sum([car.weight, driver.weight, fuel.weight]) print('총 질량 (무게):\t{:.1f} kg ({:.1f} N)'.format(total_mass, total_weight)) center_gravity = sum( [car.first_moment, driver.first_moment, fuel.first_moment], Vector()) center_gravity /= total_mass print('질량 중심 (x, y):\t{}'.format(center_gravity)) model = {} model['car'] = RectCylinder(car, 1.80, 4.70) model['driver'] = RectCylinder(driver, 0.5, 0.9) model['fuel'] = RectCylinder(fuel, 0.9, 0.5) total_moment_inertia = Decimal(0) for name, model in model.items(): mass_element_inertia = model.moment_inertia(center_gravity) total_moment_inertia += mass_element_inertia print('\t{}: {:.1f} '.format(name, mass_element_inertia)) print('관성 모멘트:\t{:.2f}'.format(total_moment_inertia))
def on_destroy(self): # Create explosion for s in self.body.shapes: bodydef = Body() bodydef.ccd = True debris = self.melee.world.append_body(bodydef) debris.linear_velocity = Vec2(randrange(-100.0, 100.0), randrange(-100.0, 100.0)) junk = Debris(self.melee, debris) if isinstance(s, BoundPolygon): # Polygon debris.position = self.body.position + s.centroid polydef = Polygon() polydef.density = 10 polydef.vertices = s.vertices shape = debris.append_shape(polydef) # Register shapes for collision callbacks self.melee.contact_register[hash(shape)] = junk else: # Circle debris.position = self.body.position + s.local_position circdef = Circle() circdef.density = 10 circle.radis = s.radius shape = debris.append_shape(circdef) self.melee.contact_register[hash(shape)] = junk debris.set_mass_from_shapes()
def __init__(self): # Entities self.balls = [] self.bricks = [] self.paddles = [] # Other bodies self.bodies = [] ball_position = Vector2((WINDOW_WIDTH - BALL_WIDTH) * 0.5, WINDOW_HEIGHT - PADDLE_HEIGHT - BALL_HEIGHT) self.balls.append(entity.DefaultBall(ball_position.x, ball_position.y)) paddle_position = Vector2((WINDOW_WIDTH - PADDLE_WIDTH) * 0.5, WINDOW_HEIGHT - PADDLE_HEIGHT) self.paddles.append( entity.DefaultPaddle(paddle_position.x, paddle_position.y)) # Create bodies enclosing the game screen area top_rect = Rect(Vector2(0.0, -WALL_HEIGHT), WINDOW_WIDTH, WALL_HEIGHT) bottom_rect = Rect(Vector2(0.0, WINDOW_HEIGHT), WINDOW_WIDTH, WALL_HEIGHT) left_rect = Rect(Vector2(-WALL_WIDTH, -WALL_HEIGHT), WALL_WIDTH, WINDOW_HEIGHT + 2 * WALL_HEIGHT) right_rect = Rect(Vector2(WINDOW_WIDTH, -WALL_HEIGHT), WALL_WIDTH, WINDOW_HEIGHT + 2 * WALL_HEIGHT) self.bodies.append(Body(top_rect, ZERO2, 'top-wall', True)) self.bodies.append(Body(bottom_rect, ZERO2, 'bottom-wall', True)) self.bodies.append(Body(left_rect, ZERO2, 'left-wall', True)) self.bodies.append(Body(right_rect, ZERO2, 'right-wall', True))
def __init__(self, pos, pType, rotation): Body.__init__(self, type=pType.name, pos=pos.copy(), radius=pType.radius, rotation=rotation, density=pType.density) self.type = pType
def main(): elements = [Body(1, (1, 2, 3)), Body(2, (2, -3, 4))] for point_mass in elements: print(point_mass) print('weight: {}'.format(point_mass.weight)) total_mass = Decimal(0) for point_mass in elements: total_mass += point_mass.mass first_moment = sum([body.first_moment for body in elements], Vector()) combined_center = first_moment / total_mass print('Combined Center of Gravity: {}'.format(combined_center))
def __init__(self, melee): Actor.__init__(self, melee) # Randomize velocity ub = melee.aabb.upper_bound lb = melee.aabb.lower_bound x = randrange(lb.x, ub.x) y = randrange(lb.y, ub.y) av = 0.1 vx = randrange(-50.0, 50.0) vy = randrange(-50.0, 50.0) # Create body bodydef = Body() bodydef.ccd = True bodydef.position = Vec2(x, y) self.body = melee.world.append_body(bodydef) self.body.angular_velocity = av self.body.linear_velocity = Vec2(vx, vy) # Create shape self.radius = 1.0 density = 10.0 c1 = Circle() c1.radius = self.radius self.c1_local = -1.0, 1.0 c1.local_position = Vec2(*self.c1_local) c1.density = density s1 = self.body.append_shape(c1) c2 = Circle() c2.radius = self.radius self.c2_local = 1.0, 1.0 c2.local_position = Vec2(*self.c2_local) c2.density = density s2 = self.body.append_shape(c2) self.body.set_mass_from_shapes() # Register shapes for collision callbacks melee.contact_register[hash(s1)] = self melee.contact_register[hash(s2)] = self
def __init__(self, x, y): super(DefaultPaddle, self).__init__(Body(Rect(Vector2(x, y), PADDLE_WIDTH, PADDLE_HEIGHT), ZERO2, 'paddle', self, True), graphics.get_image(IMAGE_FILE_NAME), pygame.Rect(0, BRICK_NUMBER * BRICK_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT))
def __init__(self, melee): Ship.__init__(self, melee) ## ## Manually Create turret ## TODO: Eventually this will be autogenerated via SVG from utils.squirtle import SVG svg_turret = SVG('data/ships/nemesis-turret.svg') svg_turret.init((105.908, 106.821), self.scale) # Create body bodydef = Body() bodydef.ccd = True bodydef.position = self.body.position self.turret = melee.world.append_body(bodydef) self.turret.angular_velocity = self.body.angular_velocity self.turret.linear_velocity = self.body.linear_velocity # Create shapes self.radius = 0.85 density = 2.0 # Base base = Circle() base.collision_group = self.group base.radius = self.radius base.density = density # Barrel verts = [Vec2(0.15, -2), Vec2(0.15, 0), Vec2(-0.15, 0), Vec2( -0.15, -2)] barrel = Polygon() barrel.vertices = verts barrel.collision_group = self.group barrel.density = density s1 = self.turret.append_shape(base) s2 = self.turret.append_shape(barrel) self.turret.set_mass_from_shapes() # Create secondary SecondaryWeapon(self, melee, self.turret, svg_turret)
def __init__(self, x, y, damage_points=1): super(DefaultBall, self).__init__(Body(Rect(Vector2(x, y), BALL_WIDTH, BALL_HEIGHT), ZERO2, 'ball', self, # general purpose tag references the body's owner entity True), graphics.get_image(IMAGE_FILE_NAME), pygame.Rect(0, (BRICK_NUMBER * BRICK_HEIGHT) + PADDLE_HEIGHT, BALL_WIDTH, BALL_HEIGHT)) self.damage_points = damage_points
def fire(self): # This is a specalized function.. if not self.primary_time() or self.battery <= self.pEnergy: return # Drain battery self.battery_cost(self.pEnergy) # Create body and shape bodydef = Body() bodydef.ccd = True bodydef.angle = self.body.angle + (pi * 0.5) + self.turret_angle bodydef.position = self.turret.get_world_point(Vec2(0, -3)) shell = self.melee.world.append_body(bodydef) angle = vforangle(bodydef.angle) velocity = rotate(angle, (0.0, -150.0)) vb = self.body.linear_velocity shell.linear_velocity = Vec2(velocity[0]+vb.x, velocity[1]+vb.y) polydef = Polygon() verts = [Vec2(0.5, 0.8), Vec2(-0.5, 0.8), Vec2(-0.5, -0.8), Vec2(0.5, -0.8)] polydef.vertices = verts polydef.density = 5 polydef.collision_group = self.group shell.append_shape(polydef) shell.set_mass_from_shapes() # Create projectile projectile = PrimaryWeapon(self, self.melee, shell) projectile.group = self.group projectile.lifetime = 2.5 projectile.damage = 10 projectile.health = 5 projectile.shapes = verts
def __init__(self, melee): Actor.__init__(self, melee) # Set max linear and angular velocity self.max_linear_velocity = 50 self.max_angular_velocity = pi # Physics (based on SVG shapes) self.translate = calc_center(self.lines[self.parts.index(self.center_part)]) self.svg.init(self.translate, self.scale) bodydef = Body() bodydef.ccd = True bodydef.position = self.initial_position self.body = melee.world.append_body(bodydef) self.body.linear_velocity = self.initial_velocity self.body.angular_velocity = self.initial_ang_vel for p in self.lines: polygondef = Polygon() polygondef.density = self.density # Ensure points are oriented ccw ccw = convex_hull(p) # Translate and scale points verts = [] for v in ccw: x = (v[0] - self.translate[0]) * self.scale y = (v[1] - self.translate[1]) * self.scale verts.append(Vec2(x, y)) polygondef.vertices = verts polygondef.collision_group = self.group shape = self.body.append_shape(polygondef) # Register shapes for collision callbacks melee.contact_register[hash(shape)] = self self.body.set_mass_from_shapes()
def __init__(self, x, y, brick_color, health_points=1): surface_src_b = None for i, color in enumerate(BRICKS_COLORS): if color == brick_color: surface_src_b = pygame.Rect(0, BRICK_HEIGHT * i, BRICK_WIDTH, BRICK_HEIGHT) break super(DefaultBrick, self).__init__(Body(Rect(Vector2(x, y), BRICK_WIDTH, BRICK_HEIGHT), ZERO2, 'brick', self, # general purpose tag references the body's owner entity True), graphics.get_image(IMAGE_FILE_NAME), surface_src_b, health_points)