def setup(self): self.ship_model = model_factory.manufacture("ship", position=(10, 0, 10), rotation=(0, 45, 0)) self.target = MutableForce(MutableOffsets(-1, 0, -1), MutableOffsets(0, 0, -1)) self.ship_model.mutate_force_to_global(self.target)
def _pre_manufacture(self, name): return self.manufacture(name, MutableOffsets(0, 0, 0), MutableDegrees(0, 0, 0), MutableOffsets(0, 0, 0), MutableDegrees(0, 0, 0), MutableOffsets(0, 0, 0), MutableDegrees(0, 0, 0))
def setup(self): spmf = ShipPartModelFactory() null_offset = MutableOffsets(0, 0, 0) null_rotation = MutableDegrees(0, 0, 0) target = spmf.manufacture("generator", position=(10, 0, 0)) cockpit = spmf.manufacture("cockpit") parts = {cockpit, target} self.ship = ShipModel("foo", parts, null_offset.__copy__(), null_rotation.__copy__(), null_offset.__copy__(), null_rotation.__copy__(), null_offset.__copy__(), null_rotation.__copy__(), null_offset.__copy__()) self.drydock = Drydock(0, 1000, 0, 1000, self.ship, view_factory=FakeFactory()) self.ship = self.drydock.ship self.target_item: DockableItem = [ item for item in self.drydock.items if item.model.name == "generator" ][0] self.target_item.held = True self.target_item._highlight = True self.target = self.target_item.model self.cockpit_item: DockableItem = [ item for item in self.drydock.items if item.model.name == "cockpit" ][0] self.cockpit = self.cockpit_item.model self.ship_rebuild_callback = MagicMock() self.ship.observe(self.ship_rebuild_callback, "rebuild") self.target_callback = MagicMock() self.target.observe(self.target_callback, "move")
def setup(self): spmf = ShipPartModelFactory() null_offset = MutableOffsets(0, 0, 0) null_rotation = MutableDegrees(0, 0, 0) self.target = spmf.manufacture("shield", position=(10, 0, 0)) self.other = spmf.manufacture("shield") parts = {self.other, self.target} self.ship = ShipModel("foo", parts, null_offset.__copy__(), null_rotation.__copy__(), null_offset.__copy__(), null_rotation.__copy__(), null_offset.__copy__(), null_rotation.__copy__(), null_offset.__copy__())
def setup(self): spmf = ShipPartModelFactory() null_offset = MutableOffsets(0, 0, 0) null_rotation = MutableDegrees(0, 0, 0) self.generator = spmf.manufacture("generator", position=(1.5, 0, 0)) self.cockpit = spmf.manufacture("cockpit") parts = {self.cockpit, self.generator} self.ship = ShipModel("foo", parts, null_offset.__copy__(), null_rotation.__copy__(), null_offset.__copy__(), null_rotation.__copy__(), null_offset.__copy__(), null_rotation.__copy__(), null_offset.__copy__()) self.target = list(self.ship._connections)[0]
def _test_force_applied_lateral_to_center_of_mass_rotates_and_moves(self): x_offset = 2 z_offset = -1 degrees_at_offset = round(degrees(atan2(z_offset, x_offset)), 2) force = MutableForce(MutableOffsets(x_offset, 0, 0), MutableOffsets(0, 0, z_offset)) assert force.diff_yaw_of_force_to_pos() == 90 assert round(force.delta_yaw, 2) == degrees_at_offset self.target.apply_global_force(force) pitch, yaw, roll = self.target.spin assert (round(pitch, 2), round(yaw, 2), round(roll, 2)) == \ (0, round(degrees_at_offset / self.target.inertia, 2), 0)
def setup(self): spmf = ShipPartModelFactory() null_offset = MutableOffsets(0, 0, 0) null_rotation = MutableDegrees(0, 0, 0) self.shield1 = spmf.manufacture("shield", position=(2, 0, 0)) self.obstacles = { spmf.manufacture("generator", position=(0, 0, y)) for y in range(-4, 4) } self.shield2 = spmf.manufacture("shield", position=(-2, 0, 0)) parts = self.obstacles | {self.shield1, self.shield2} self.ship = ShipModel("foo", parts, null_offset.__copy__(), null_rotation.__copy__(), null_offset.__copy__(), null_rotation.__copy__(), null_offset.__copy__(), null_rotation.__copy__(), null_offset.__copy__())
def _manufacture(self, name, ship_model: ShipModel, ship_part_model: ShipPartModel) -> PlasmaModel: yaw_radian = ship_model.rotation.yaw_radian + ship_part_model.rotation.yaw_radian position = ship_part_model.position.__copy__() position.set(position.x, position.y, position.z - 5) ship_model.mutate_offsets_to_global(position) rotation = ship_model.rotation.__copy__() movement = MutableOffsets(-sin(yaw_radian) * 125, 0, -cos(yaw_radian) * 125) #movement += ship_model.global_momentum_at(ship_part_model.position).forces movement += ship_model.movement spin = -ship_model.spin acceleration = MutableOffsets(0, 0, 0) torque = MutableDegrees(0, 0, 0) return self.factory.manufacture(name, position, rotation, movement, spin, acceleration, torque)
def __init__(self, view_class=BaseView): self.meshes = {} self.mesh_factory = factory self._view_class = view_class self.pre_factorized_views = [] if view_class == BaseView: above_camera = MutableOffsets(0, -100, 0) zero = MutableOffsets(0, 0, 0) no_angle = MutableDegrees(0, 0, 0) no_acceleration = MutableOffsets(0, 0, 0) no_torque = MutableDegrees(0, 0, 0) bb = Polygon.manufacture([(0, 0)]) self.dummy_model = BaseModel(position=above_camera, rotation=no_angle, movement=zero, spin=no_angle, acceleration=no_acceleration, torque=no_torque, bounding_box=bb) self.pre_factorized_views = [self._prefactorize() for i in range(200)]
def setup(self): spmf = ShipPartModelFactory() null_offset = MutableOffsets(0, 0, 0) null_rotation = MutableDegrees(0, 0, 0) self.shield1 = spmf.manufacture("shield", position=(2.5, 0, 0)) self.obstacles = {spmf.manufacture("generator", position=(0, 0, 0))} self.shield2 = spmf.manufacture("shield", position=(-2.5, 0, 0)) parts = self.obstacles | {self.shield1, self.shield2} self.ship = ShipModel("foo", parts, null_offset.__copy__(), null_rotation.__copy__(), null_offset.__copy__(), null_rotation.__copy__(), null_offset.__copy__(), null_rotation.__copy__(), null_offset.__copy__()) self.target = [ c for c in self.ship._connections if set(c._ship_parts) == {self.shield1, self.shield2} ][0]
def __init__(self, x=0, y=0, z=0, pitch=0, yaw=0, roll=0, name=None): Observable.__init__(self) self._position = MutableOffsets(x, y, z) self._rotation = MutableDegrees(pitch, yaw, roll) self._mesh_name = name self._name = name self.material_value = 0.0 self._mass = 1
def manufacture(self, name, position: MutableOffsets, rotation: MutableDegrees, movement: MutableOffsets, spin: MutableDegrees, acceleration: MutableOffsets, torque: MutableDegrees) -> PlasmaModel: if self.projectiles[name]: return self.repurpose(name, position, rotation, movement, spin, acceleration, torque) # config = deepcopy(self.projectiles[name]) bounding_box = MultiPolygon.manufacture([(-.1, -.1), (.1, -.1), (.1, .1), (-.1, .1)], x=position.x, y=position.z, rotation=rotation.yaw) projectile = PlasmaModel(position.__copy__(), rotation.__copy__(), movement.__copy__(), spin.__copy__(), acceleration.__copy__(), torque.__copy__(), bounding_box) return projectile
def manufacture(position, rotation=None): position = MutableOffsets(*position) rotation = rotation or (0, 0, 0) rotation = MutableDegrees(*rotation) movement = MutableOffsets(0, 0, 0) spin = MutableDegrees(0, 0, 0) acceleration = MutableOffsets(0, 0, 0) torque = MutableDegrees(0, 0, 0) coords = [(-sin(radians(d)), cos(radians(d))) for d in range(0, 360, 18)] distances = [abs(normalvariate(25, 5)) for _ in coords] coords = [(x * d, y * d) for (x, y), d in zip(coords, distances)] bounding_box = MultiPolygon.manufacture(coords=coords, x=position.x, y=position.z, rotation=rotation.yaw) return AsteroidModel(position, rotation, movement, spin, acceleration, torque, bounding_box)
def _test_force_on_right_side_is_the_spin_of_target_times_mass(self): yaw_degrees_per_second = 1 yaw_radians_per_second = radians(yaw_degrees_per_second) measure_point = MutableOffsets(2, 0, 0) distance_to_measure_point = measure_point.distance rotation_speed_at_distance = distance_to_measure_point * yaw_radians_per_second self.target.set_spin(0, yaw_degrees_per_second, 0) force = self.target.global_momentum_at(measure_point) assert round(force.forces.x, 2) == 0 assert round(force.forces.y, 2) == 0 assert round(force.forces.z, 2) == round( rotation_speed_at_distance * self.target.inertia, 2)
def manufacture(self, name, position=None, rotation=None, movement=None, spin=None, acceleration=None, torque=None) -> ShipModel: config = deepcopy(self.ships[name]) center_of_mass = MutableOffsets(0, 0, 0) parts = self.ship_part_model_factory.manufacture_all( config['parts'], center_of_mass) ship_id = "Ship {}".format(self.ship_id_counter) self.ship_id_counter += 1 position = position or (0, 0, 0) rotation = rotation or (0, 0, 0) movement = movement or (0, 0, 0) spin = spin or (0, 0, 0) acceleration = acceleration or (0, 0, 0) torque = torque or (0, 0, 0) position = MutableOffsets(*position) rotation = MutableDegrees(*rotation) movement = MutableOffsets(*movement) spin = MutableUnboundDegrees(*spin) acceleration = MutableOffsets(*acceleration) torque = MutableUnboundDegrees(*torque) ship = ShipModel(ship_id=ship_id, parts=parts, position=position, rotation=rotation, movement=movement, spin=spin, acceleration=acceleration, torque=torque, center_of_mass=center_of_mass) # for part in parts: # part.observe(self.jettison_exploding_part_function(ship, part), "explode") return ship
def __init__(self, left, right, bottom, top, model: BaseModel): super().__init__(left, right, bottom, top) self._model = None self._view_scale = 1. self._view: BaseView = None self._position = MutableOffsets(0, 0, 0) self._rotation = MutableDegrees(0, 0, 0) padding = 20 font_size = 12 x = left + padding y = top - (font_size + padding) self._info_label = Label("N/A", font_name='Courier New', font_size=font_size, x=x, y=y, multiline=True, width=self.width - padding * 2) self.set_model(model)
def momentum_at(self, local_coordinates: MutableOffsets) -> MutableForce: local_coordinates = MutableOffsets(*local_coordinates) momentum = self._movement # * self.mass momentum += self.tangent_momentum_at(local_coordinates) momentum_at = MutableForce(local_coordinates, momentum) return momentum_at
def test_force_applied_to_center_of_mass_translates(self): force = MutableForce(MutableOffsets(2, 0, 0), MutableOffsets(-1, 0, 0)) self.target.apply_global_force(force) x, y, z = self.target.movement assert (round(x, 1), round(y, 1), round(z, 1)) == (-0.1, 0, 0)
def setup(self): self.ship_model = model_factory.manufacture("ship", position=(10, 0, 10)) self.target = MutableForce(MutableOffsets(9, 0, 9), MutableOffsets(0, 0, -1)) self.ship_model.mutate_force_to_local(self.target)
def test_force_on_right_side_is_the_movement_of_target_times_mass(self): self.target.set_movement(1, 0, 0) force = self.target.global_momentum_at(MutableOffsets(2, 0, 0)) assert force.forces.x == 1 #* self.target.mass assert force.forces.y == 0 #* self.target.mass assert force.forces.z == 0 #* self.target.mass
def test_tangent_force_on_right_side_spins_clockwise_when_positive_yaw_is_applied( self): yaw_degrees_per_second = 1 self.target.set_spin(0, yaw_degrees_per_second, 0) momentum = self.target.tangent_momentum_at(MutableOffsets(2, 0, 0)) assert momentum.z > 0
def mutate_offsets_to_local(self, mo: MutableOffsets): mo -= self.position mo.rotate(self.yaw)
def test_force_applied_to_center_of_mass_does_not_rotate(self): force = MutableForce(MutableOffsets(2, 0, 0), MutableOffsets(-1, 0, 0)) self.target.apply_global_force(force) x, y, z = self.target.spin assert (round(x, 2), round(y, 2), round(z, 2)) == (0, 0, 0)
def mutate_offsets_to_global(self, mo: MutableOffsets): mo.rotate(-self.yaw) mo.translate(self.position)
def tangent_momentum_at(self, local_coordinates: Offsets) -> Offsets: if local_coordinates.distance == 0: return MutableOffsets(0, 0, 0) yaw = local_coordinates.rotated(90) * ( self._spin.yaw_radian * self.inertia / local_coordinates.distance) return yaw