def create_drag_params(self, target): center = target.get_rel_position_to(self.observer.camera_global_pos) self.dragCenter = self.observer.camera_frame.get_rel_position(center) dragPosition = self.observer.get_frame_camera_pos() self.dragDir = self.dragCenter - dragPosition self.dragOrientation = self.observer.get_frame_camera_rot() self.dragZAxis = self.dragOrientation.xform(LVector3d.up()) self.dragXAxis = self.dragOrientation.xform(LVector3d.right())
def decode(self, data): if 'angle' in data: angle = float(data['angle']) axis = data.get("axis", LVector3d.up()) rot = utils.LQuaternionromAxisAngle(axis, angle, units.Deg) else: rot = LQuaterniond() rotation = FixedRotation(rot) return rotation
def __init__(self): self.skybox = None self.sun_color = None self.skybox_color = None self.light_angle = None self.light_dir = LVector3d.up() self.light_quat = LQuaternion() self.light_color = (1.0, 1.0, 1.0, 1.0) self.fog = None
def go_to(self, target, duration, position, direction, up): if up is None: up = LVector3d.up() frame = CelestiaBodyFixedReferenceFrame(target) up = frame.get_orientation().xform(up) if isclose(abs(up.dot(direction)), 1.0): print("Warning: lookat vector identical to up vector") orientation = LQuaterniond() lookAt(orientation, direction, up) self.move_and_rotate_camera_to(position, orientation, duration=duration)
def get_normals_under(self, position): if self.surface is not None: (x, y, distance) = self.spherical_to_longlat(self.cartesian_to_spherical(position)) vectors = self.surface.get_normals_at(x, y) else: vectors = (LVector3d.up(), LVector3d.forward(), LVector3d.left()) sync_frame = SynchroneReferenceFrame(self) return (sync_frame.get_orientation().xform(vectors[0]), sync_frame.get_orientation().xform(vectors[1]), sync_frame.get_orientation().xform(vectors[2]))
def OnSelectClick(self): if self.base.mouseWatcherNode.hasMouse(): self.mouseSelectClick = True mpos = self.base.mouseWatcherNode.getMouse() self.startX = mpos.getX() self.startY = mpos.getY() self.dragCenter = self.observer.get_camera_pos() self.dragOrientation = self.observer.get_camera_rot() self.dragZAxis = self.dragOrientation.xform(LVector3d.up()) self.dragXAxis = self.dragOrientation.xform(LVector3d.right())
def set_magnitude(self, owner, shape, shader, abs_magnitude, app_magnitude, visible_size): if shape.instance is not None: axis = owner.scene_orientation.xform(LVector3d.up()) cosa = abs(axis.dot(owner.vector_to_obs)) coef = cosa * self.correction + (1.0 - self.correction) scale = float(self.color_scale) / 255 * coef * mag_to_scale_nolimit(app_magnitude) size = owner.get_apparent_radius() / owner.distance_to_obs if size > 1.0: scale = scale / size shape.instance.set_color_scale(LColor(scale, scale, scale, scale)) shader.set_size_scale(size)
def calc_axis_ra_de(self, time): rotation = self.get_equatorial_orientation_at(time) axis = rotation.xform(LVector3d.up()) axis = self.frame.get_orientation().xform(axis) projected = J2000EquatorialReferenceFrame.orientation.conjugate( ).xform(axis) declination = asin(projected[2]) right_asc = atan2(projected[1], projected[0]) if right_asc < 0: right_asc += 2 * pi return (right_asc, declination)
def update(self, dt): if self.keyMap['up']: self.step(self.speed * self.speed_factor * dt) if self.keyMap['down']: self.step(-self.speed * self.speed_factor * dt) if self.keyMap['left']: self.turn(LVector3d.up(), self.rot_step_per_sec * dt) if self.keyMap['right']: self.turn(LVector3d.up(), -self.rot_step_per_sec * dt) if self.keyMap['shift-up']: self.turn(LVector3d.right(), self.rot_step_per_sec * dt) if self.keyMap['shift-down']: self.turn(LVector3d.right(), -self.rot_step_per_sec * dt) if self.keyMap['shift-left']: self.turn(LVector3d.up(), self.rot_step_per_sec * dt) if self.keyMap['shift-right']: self.turn(LVector3d.up(), -self.rot_step_per_sec * dt) if self.keyMap['control-left']: self.turn(LVector3d.forward(), self.rot_step_per_sec * dt) if self.keyMap['control-right']: self.turn(LVector3d.forward(), -self.rot_step_per_sec * dt) if self.keyMap['home']: self.change_altitude(self.distance_speed * dt) if self.keyMap['end']: self.change_altitude(-self.distance_speed * dt) if self.wheel_event_time + self.wheel_event_duration > globalClock.getRealTime( ): distance = self.wheel_direction self.change_altitude(distance * self.distance_speed * dt)
def instanciate_body(universe, item_type, item_name, item_data): ra = None decl = None distance = None type = None radius = None axis = None angle = None abs_magnitude = None app_magnitude = None orbit = None axis = LVector3d.up() angle = 0.0 names = names_list(item_name) for (key, value) in item_data.items(): if key == 'RA': ra = value elif key == 'Dec': decl = value elif key == 'Distance': distance = value elif key == 'Type': type = value elif key == 'Radius': radius = value elif key == 'Axis': axis = LVector3d(*value) elif key == 'Angle': angle = value elif key == 'AbsMag': abs_magnitude = value elif key == 'AppMag': app_magnitude = value elif key == 'InfoURL': pass # = value else: print("Key of", item_type, key, "not supported") orbit = FixedPosition(right_asc=ra, right_asc_unit=units.HourAngle, declination=decl, distance=distance, distance_unit=units.Ly) rot = utils.LQuaternionromAxisAngle(axis, angle, units.Deg) rotation = FixedRotation(rot, J2000EquatorialReferenceFrame()) if app_magnitude != None and distance != None: abs_magnitude = units.app_to_abs_mag(app_magnitude, distance) dso = Galaxy(names, abs_magnitude=abs_magnitude, radius=radius, orbit=orbit, rotation=rotation) return dso
def align_on_equatorial(self, duration=None): if duration is None: duration = settings.fast_move ecliptic_normal = self.camera.camera_rot.conjugate().xform( J2000EquatorialReferenceFrame.orientation.xform(LVector3d.up())) angle = acos(ecliptic_normal.dot(LVector3d.right())) direction = ecliptic_normal.cross(LVector3d.right()).dot( LVector3d.forward()) if direction < 0: angle = 2 * pi - angle rot = LQuaterniond() rot.setFromAxisAngleRad(pi / 2 - angle, LVector3d.forward()) self.camera.step_turn_camera(rot, absolute=False)
def go_to(self, target, duration, position, direction, up): if up is None: up = LVector3d.up() frame = SynchroneReferenceFrame(target) up = frame.get_orientation().xform(up) if isclose(abs(up.dot(direction)), 1.0): print("Warning: lookat vector identical to up vector") else: # Make the up vector orthogonal to the direction using Gram-Schmidt up = up - direction * up.dot(direction) orientation = LQuaterniond() lookAt(orientation, direction, up) self.move_and_rotate_to(position, orientation, duration=duration)
def set_magnitude(self, owner, shape, shader, abs_magnitude, app_magnitude, visible_size): if shape.instance is not None: if shape.is_flat(): axis = owner.scene_orientation.xform(LVector3d.up()) cosa = abs(axis.dot(owner.vector_to_obs)) coef = max(self.min_coef, sqrt(cosa)) else: coef = 1.0 scale = self.color_scale / 255.0 * coef * mag_to_scale_nolimit(app_magnitude) size = owner.get_apparent_radius() / owner.distance_to_obs if size > 1.0: scale = max(1.0/255, scale / size) shape.instance.set_color_scale(LColor(scale, scale, scale, scale))
def update(self, dt): if self.keyMap['up']: self.step(self.speed * self.speed_factor * dt) if self.keyMap['down']: self.step(-self.speed * self.speed_factor * dt) if self.keyMap['left']: self.turn(LVector3d.up(), self.rot_step_per_sec * dt) if self.keyMap['right']: self.turn(LVector3d.up(), -self.rot_step_per_sec * dt) if self.keyMap['shift-up']: self.turn(LVector3d.right(), self.rot_step_per_sec * dt) if self.keyMap['shift-down']: self.turn(LVector3d.right(), -self.rot_step_per_sec * dt) if self.keyMap['shift-left']: self.turn(LVector3d.up(), self.rot_step_per_sec * dt) if self.keyMap['shift-right']: self.turn(LVector3d.up(), -self.rot_step_per_sec * dt) if self.keyMap['control-left']: self.turn(LVector3d.forward(), self.rot_step_per_sec * dt) if self.keyMap['control-right']: self.turn(LVector3d.forward(), -self.rot_step_per_sec * dt) if self.keyMap['home']: self.onChangeAltitude(0.1) if self.keyMap['end']: self.onChangeAltitude(-0.1)
def update_obs(self, observer): self.rel_position = self._local_position - observer._local_position self.distance_to_obs = self.rel_position.length() self.vector_to_obs = self.rel_position / self.distance_to_obs if self.context.nearest_system is not None: self.star = self.context.nearest_system.star self.vector_to_star = (self.star._local_position - self._local_position).normalized() if self.light_source is None: self.create_light() else: self.star = None self.vector_to_star = LVector3d.up() if self.light_source is not None: self.remove_light() self.ship_object.update_obs(observer)
def decode(self, data, frame): if 'angle' in data: angle = float(data['angle']) axis = data.get("axis", LVector3d.up()) rot = utils.LQuaternionromAxisAngle(axis, angle, units.Deg) elif 'ra' in data: right_ascension = data.get('ra', None) ra_units = AngleUnitsYamlParser.decode(data.get('ra-units', 'Deg')) declination = data.get('de', 0.0) decl_units = AngleUnitsYamlParser.decode(data.get('de-units', 'Deg')) rot = EquatorialReferenceAxis(right_ascension * ra_units, declination * decl_units, False) else: rot = LQuaterniond() if data.get('frame') is not None or frame is None: frame = FrameYamlParser.decode(data.get('frame', 'J2000Equatorial')) rotation = FixedRotation(rot, frame) return rotation
def update_instance(self, camera_pos, orientation): position = self.parent.project(0, self.context.observer.camera_global_pos, self.context.observer.infinity) if position != None: self.instance.setPos(*position) scale = abs(self.context.observer.pixel_size * self.parent.get_label_size() * self.context.observer.infinity) else: scale = 0.0 self.label.setTextColor(self.parent.get_label_color()) if scale < 1e-7: print("Label too far", self.get_name()) scale = 1e-7 self.instance.setScale(scale) self.look_at.set_pos( LVector3(*(orientation.xform(LVector3d.forward())))) self.label_instance.look_at( self.look_at, LVector3(), LVector3(*(orientation.xform(LVector3d.up()))))
def update_instance(self, camera_pos, camera_rot): body = self.parent if body.is_emissive() and (not body.resolved or body.background): if body.scene_position != None: self.instance.setPos(*body.scene_position) scale = abs(self.context.observer.pixel_size * body.get_label_size() * body.scene_distance) else: scale = 0.0 else: offset = body.get_apparent_radius() * 1.01 rel_front_pos = body.rel_position - camera_rot.xform(LPoint3d(0, offset, 0)) vector_to_obs = LVector3d(-rel_front_pos) distance_to_obs = vector_to_obs.length() vector_to_obs /= distance_to_obs position, distance, scale_factor = self.calc_scene_params(rel_front_pos, rel_front_pos, distance_to_obs, vector_to_obs) self.instance.setPos(*position) scale = abs(self.context.observer.pixel_size * body.get_label_size() * distance) self.look_at.set_pos(LVector3(*(camera_rot.xform(LVector3d.forward())))) self.label_instance.look_at(self.look_at, LVector3(), LVector3(*(camera_rot.xform(LVector3d.up())))) self.instance.set_color_scale(LColor(self.fade, self.fade, self.fade, 1.0)) if scale < 1e-7: print("Label too far", self.get_name()) scale = 1e-7 self.instance.setScale(scale)
def turn_back(self): new_rot = utils.relative_rotation(self.get_rot(), LVector3d.up(), pi) self.set_rot(new_rot)
def camera_look_back(self): new_rot = utils.relative_rotation(self.get_camera_rot(), LVector3d.up(), pi) self.set_camera_rot(new_rot)
def get_normals_under_xy(self, x, y): if self.surface is not None: vectors = self.surface.get_normals_at(x, y) else: vectors = (LVector3d.up(), LVector3d.forward(), LVector3d.left()) return vectors
def update(self, dt): rot_x = 0.0 rot_y = 0.0 rot_z = 0.0 distance = 0.0 if self.mouseTrackClick and self.base.mouseWatcherNode.hasMouse(): mpos = self.base.mouseWatcherNode.getMouse() deltaX = mpos.getX() - self.startX deltaY = mpos.getY() - self.startY z_angle = -deltaX * self.dragAngleX x_angle = deltaY * self.dragAngleY self.do_drag(z_angle, x_angle, True) if self.mouseSelectClick and self.base.mouseWatcherNode.hasMouse(): mpos = self.base.mouseWatcherNode.getMouse() deltaX = mpos.getX() - self.startX deltaY = mpos.getY() - self.startY z_angle = deltaX * self.observer.realCamLens.getHfov( ) / 180 * pi / 2 x_angle = -deltaY * self.observer.realCamLens.getVfov( ) / 180 * pi / 2 self.do_drag(z_angle, x_angle) if settings.celestia_nav: if self.keyMap['up']: rot_x = -1 if self.keyMap['down']: rot_x = 1 else: if self.keyMap['up']: rot_x = 1 if self.keyMap['down']: rot_x = -1 if self.keyMap['left']: rot_y = 1 if self.keyMap['right']: rot_y = -1 if self.keyMap['control-left']: rot_z = 1 if self.keyMap['control-right']: rot_z = -1 if self.keyMap['home']: distance = 1 if self.keyMap['end']: distance = -1 if self.wheel_event_time + self.wheel_event_duration > globalClock.getRealTime( ): distance = self.wheel_direction if not self.keyboardTrack and (self.keyMap['shift-left'] or self.keyMap['shift-right'] or self.keyMap['shift-up'] or self.keyMap['shift-down']): target = self.select_target() if target is not None: self.keyboardTrack = True arc_length = pi * target.get_apparent_radius() apparent_size = arc_length / (target.distance_to_obs - target.height_under) if apparent_size != 0: self.drag_coef = min(pi, pi / 2 / apparent_size) else: self.drag_coef = pi self.drag_x = 0.0 self.drag_z = 0.0 self.create_drag_params(target) if self.keyboardTrack: if not (self.keyMap['shift-left'] or self.keyMap['shift-right'] or self.keyMap['shift-up'] or self.keyMap['shift-down']): self.keyboardTrack = False if self.keyMap['shift-left']: self.drag_z += self.drag_coef * dt self.do_drag(self.drag_z, self.drag_x, True) if self.keyMap['shift-right']: self.drag_z -= self.drag_coef * dt self.do_drag(self.drag_z, self.drag_x, True) if self.keyMap['shift-up']: self.drag_x += self.drag_coef * dt self.do_drag(self.drag_z, self.drag_x, True) if self.keyMap['shift-down']: self.drag_x -= self.drag_coef * dt self.do_drag(self.drag_z, self.drag_x, True) if self.keyMap['a']: if self.speed == 0: self.speed = 0.1 else: self.speed *= exp(dt * 3) if self.keyMap['z']: if self.speed < 1e-5: self.speed = 0 else: self.speed /= exp(dt * 3) y = self.speed * dt self.stepRelative(0, y, 0) if settings.damped_nav: #TODO: Refactor this wall of code... if rot_x > 0: self.current_rot_x_speed += self.incr_speed_rot_step_per_sec * dt self.current_rot_x_speed = min(self.current_rot_x_speed, self.rot_step_per_sec) elif rot_x < 0: self.current_rot_x_speed -= self.incr_speed_rot_step_per_sec * dt self.current_rot_x_speed = max(self.current_rot_x_speed, -self.rot_step_per_sec) elif self.current_rot_x_speed > 0.0: self.current_rot_x_speed -= self.decr_speed_rot_step_per_sec * dt self.current_rot_x_speed = max(0.0, self.current_rot_x_speed) elif self.current_rot_x_speed < 0.0: self.current_rot_x_speed += self.decr_speed_rot_step_per_sec * dt self.current_rot_x_speed = min(0.0, self.current_rot_x_speed) if rot_y > 0: self.current_rot_y_speed += self.incr_speed_rot_step_per_sec * dt self.current_rot_y_speed = min(self.current_rot_y_speed, self.rot_step_per_sec) elif rot_y < 0: self.current_rot_y_speed -= self.incr_speed_rot_step_per_sec * dt self.current_rot_y_speed = max(self.current_rot_y_speed, -self.rot_step_per_sec) elif self.current_rot_y_speed > 0.0: self.current_rot_y_speed -= self.decr_speed_rot_step_per_sec * dt self.current_rot_y_speed = max(0.0, self.current_rot_y_speed) elif self.current_rot_y_speed < 0.0: self.current_rot_y_speed += self.decr_speed_rot_step_per_sec * dt self.current_rot_y_speed = min(0.0, self.current_rot_y_speed) if rot_z > 0: self.current_rot_z_speed += self.incr_speed_rot_step_per_sec * dt self.current_rot_z_speed = min(self.current_rot_z_speed, self.rot_step_per_sec) elif rot_z < 0: self.current_rot_z_speed -= self.incr_speed_rot_step_per_sec * dt self.current_rot_z_speed = max(self.current_rot_z_speed, -self.rot_step_per_sec) elif self.current_rot_z_speed > 0.0: self.current_rot_z_speed -= self.decr_speed_rot_step_per_sec * dt self.current_rot_z_speed = max(0.0, self.current_rot_z_speed) elif self.current_rot_z_speed < 0.0: self.current_rot_z_speed += self.decr_speed_rot_step_per_sec * dt self.current_rot_z_speed = min(0.0, self.current_rot_z_speed) else: self.current_rot_x_speed = rot_x * self.rot_step_per_sec self.current_rot_y_speed = rot_y * self.rot_step_per_sec self.current_rot_z_speed = rot_z * self.rot_step_per_sec self.turn(LVector3d.right(), self.current_rot_x_speed * dt) self.turn(LVector3d.forward(), self.current_rot_y_speed * dt) self.turn(LVector3d.up(), self.current_rot_z_speed * dt) self.change_altitude(distance * self.distance_speed * dt)
def step(self, direction, distance): rotation = LQuaterniond() look_at(rotation, direction, LVector3d.up()) delta = rotation.xform(LVector3d(0, distance, 0)) self.delta(delta)
def get_normals_at(self, coord): vectors = (LVector3d.up(), LVector3d.forward(), LVector3d.left()) return vectors
def turn(self, angle): new_rotation = LQuaterniond() new_rotation.setFromAxisAngleRad(angle, LVector3d.up()) self.set_rot(new_rotation)
def update_instance(self, camera_pos, orientation): body = self.parent if body.is_emissive() and (not body.resolved or isinstance(body, DeepSpaceObject)): if body.scene_position != None: self.instance.setPos(*body.scene_position) scale = abs(self.context.observer.pixel_size * body.get_label_size() * body.scene_distance) else: scale = 0.0 else: offset = body.get_apparent_radius() * 1.01 rel_front_pos = body.rel_position - orientation.xform(LPoint3d(0, offset, 0)) vector_to_obs = LVector3d(-rel_front_pos) distance_to_obs = vector_to_obs.length() vector_to_obs /= distance_to_obs position, distance, scale_factor = self.get_real_pos_rel(rel_front_pos, distance_to_obs, vector_to_obs) self.instance.setPos(*position) scale = abs(self.context.observer.pixel_size * body.get_label_size() * distance) color = body.get_label_color() * self.fade self.look_at.set_pos(LVector3(*(orientation.xform(LVector3d.forward())))) self.label_instance.look_at(self.look_at, LVector3(), LVector3(*(orientation.xform(LVector3d.up())))) color[3] = 1.0 self.label.setTextColor(color) if scale < 1e-7: print("Label too far", self.get_name()) scale = 1e-7 self.instance.setScale(scale)
def turn_relative(self, step): rotation = self.get_rot() delta = LQuaterniond() delta.setFromAxisAngleRad(step, LVector3d.up()) new_rotation = delta * rotation self.set_rot(new_rotation)