# -*- coding:utf-8 -*- # title :哈雷彗星场景半真实模拟 # description :哈雷彗星运行轨道使用了万有引力,其他天体使用 astropy 包的真实数据 # author :Python超人 # date :2023-10-28 # link :https://gitcode.net/pythoncr/ # python_version :3.9 # ============================================================================== from ursina import camera, application from common.celestial_data_service import init_bodies_reality_pos_vels, conv_to_astropy_time, \ set_solar_system_celestial_position from common.consts import SECONDS_PER_YEAR, AU from common.func import calculate_distance from objs import HalleComet from sim_scenes.func import create_text_panel from sim_scenes.func import ursina_run, create_sphere_sky from sim_scenes.solar_system.halley_comet_lib import HalleyCometSimBase, HalleyCometParams, \ create_halley_comet, create_orbit_line from simulators.ursina.entities.body_timer import TimeData from simulators.ursina.entities.entity_utils import get_value_direction_vectors from simulators.ursina.ui.control_ui import ControlUI from simulators.ursina.ursina_config import UrsinaConfig from simulators.ursina.ursina_event import UrsinaEvent from simulators.ursina.ursina_mesh import create_label class HalleyCometSim(HalleyCometSimBase): """ 哈雷彗星场景模拟 """ def __init__(self, params=None): super(HalleyCometSim, self).__init__() if params is None: self.params = HalleyCometParams() else: self.params = params if isinstance(params.start_time, str): self.start_time = conv_to_astropy_time(params.start_time) else: self.start_time = params.start_time # print("北京时间:", dt.to_datetime(timezone=pytz.timezone('Asia/Shanghai'))) def build(self): self.build_solar_system(ignore_gravity=True, start_time=self.start_time) # self.bodies = [ # self.sun, # 太阳 # self.mars, # 火星 # self.neptune, # 海王星 # ] # 创建哈雷彗星创建哈雷彗星 self.halley_comet = create_halley_comet(self.params.init_velocity, self.params.init_position) self.bodies.append(self.halley_comet) def on_ready(self): """ 事件绑定后,模拟器运行前会触发 @return: """ # 创建天空 create_sphere_sky(scale=200000) # UrsinaConfig.trail_type = "curve_line" # UrsinaConfig.trail_thickness_factor = 3 UrsinaConfig.trail_type = "line" UrsinaConfig.trail_thickness_factor = 3 # UrsinaConfig.trail_length = 91 UrsinaConfig.trail_length = 180 UrsinaConfig.trail_factor = 3 # camera.clip_plane_near = 0.1 camera.clip_plane_far = 1000000 # WorldGrid().draw_axises(10) application.time_scale = 5 self.orbit_lines = [] for body in self.bodies[1:]: if isinstance(body, HalleComet): continue orbit_line = create_orbit_line(self.sun, body, self.start_time) self.orbit_lines.append(orbit_line) self.text_panel = create_text_panel() def show_clock(self, dt): """ 显示时钟 @param dt: 时间 datetime @return: """ # if self.clock_position_center: # position, origin = (0, .25), (0, 0), # else: position, origin = (0.60, -0.465), (-0.5, 0.5), ControlUI.current_ui.show_message(dt.strftime('%Y-%m-%d %H:%M:%S'), position=position, origin=origin, font="verdana.ttf", close_time=-1) def set_bodies_position(self, time_data: TimeData): """ 设置天体的位置(包含速度和加速度的信息) @param time_data: @return: """ t = self.start_time + time_data.total_days set_solar_system_celestial_position(self.bodies, t, False) def create_year_label(self, trail, year, halley_comet_pos): if trail is None: pos = halley_comet_pos else: pos = (0, 0, 0) label = create_label(trail, label=year, pos=pos, color=(255, 255, 255), scale=40, alpha=1.0) # label.udpate label.set_light_off() def on_timer_changed(self, time_data): """ @param time_data: @return: """ dt = time_data.get_datetime(str(self.start_time)) year = dt.strftime("%Y") if hasattr(self, "halley_comet"): # 哈雷彗星飞行的翻转效果 if self.halley_comet.planet.enabled: # self.halley_comet.planet.rotation_x += 0.1 # self.halley_comet.planet.rotation_y += 1 self.halley_comet.planet.look_at(self.sun.planet) d_sun = calculate_distance(self.halley_comet.position, self.sun.position) d_earth = calculate_distance(self.halley_comet.position, self.earth.position) trail_keys = self.halley_comet.planet.trails.keys() last_trail = list(trail_keys)[-1] # self.halley_comet.planet.trails[list(trail_keys)[-1]] if hasattr(last_trail, "entity_infos"): # print(last_trail.entity_infos) last_trail.entity_infos["distance_from_sun"] = d_sun last_trail.entity_infos["distance_from_earth"] = d_earth last_trail.entity_infos["time"] = dt.strftime("%Y-%m-%d") # print(last_trail.entity_infos) pos = self.halley_comet.planet.position import copy if not hasattr(self, "last_year"): self.create_year_label(last_trail, year, pos) elif self.last_year != year: if not hasattr(self, "last_label_pos"): self.create_year_label(last_trail, year, pos) self.last_label_pos = copy.deepcopy(self.halley_comet.position) else: # 防止标签非常紧密 d = calculate_distance(self.halley_comet.position, self.last_label_pos) if d > AU: self.create_year_label(last_trail, year, pos) self.last_label_pos = copy.deepcopy(self.halley_comet.position) self.last_year = year # 哈雷彗星离太阳最近的点称为 "perihelion of Halley's Comet"(近日点:comet_peri), if not hasattr(self, "comet_peri"): self.comet_peri = d_sun self.comet_peri_dt = dt.strftime("%Y-%m-%d") elif d_sun < self.comet_peri: self.comet_peri = d_sun self.comet_peri_dt = dt.strftime("%Y-%m-%d") # 哈雷彗星离太阳最远的点称为 "aphelion of Halley's Comet"(远日点) if not hasattr(self, "comet_aphel"): self.comet_aphel = d_sun self.comet_aphel_dt = dt.strftime("%Y-%m-%d") elif d_sun > self.comet_aphel: self.comet_aphel = d_sun self.comet_aphel_dt = dt.strftime("%Y-%m-%d") panel_text = "哈雷彗星:\n距离太阳:%.3f AU" % (d_sun / AU) panel_text += "\n离日最远:%.3f AU(%s)" % (self.comet_aphel / AU, self.comet_aphel_dt) panel_text += "\n离日最近:%.3f AU(%s)" % (self.comet_peri / AU, self.comet_peri_dt) panel_text += "\n距离地球:%.3f AU" % (d_earth / AU) velocity, _ = get_value_direction_vectors(self.halley_comet.velocity) panel_text += "\n当前速度:%.3f km/s" % velocity self.text_panel.text = panel_text self.set_bodies_position(time_data) self.show_clock(dt) for i, orbit_line in enumerate(self.orbit_lines): orbit_line.position = self.sun.planet.position def on_body_trail_clicked(self, e): if e["key"] == "right mouse up": trail = e["sender"] d_sun = trail.entity_infos["distance_from_sun"] d_earth = trail.entity_infos["distance_from_earth"] t = trail.entity_infos["time"] # print("key:", e["key"]) msg = "哈雷彗星:\n距离太阳:%.3f AU" % (d_sun / AU) msg += "\n距离地球:%.3f AU" % (d_earth / AU) msg += "\n当前日期:[%s]" % t ControlUI.current_ui.show_message(msg, close_time=3) if __name__ == '__main__': """ 哈雷彗星场景模拟 """ # 近日点 0.586 AU # 上次通过近日点: 1986年2月9日 # 下次通过近日点: 2061年7月28日 # 远日点 35.1 AU 2023年12月9日 # [3.34, 0, 10.7] 2060-4- # [3.34, 0, 10.712] 2061-5 # [3.34, 0, 10.715] 2061-6-24 # [3.34, 0, 10.718] 2061-8 # init_velocity=[3.34, 0, 10.718], # init_position=[0, 0.5 * AU, -10 * AU] # init_velocity=[-3.34, 3, 10.718], # init_position=[0, -2 * AU, -10 * AU]) \ # 远日点: 35.1 AU(2023年12月9日) # 近日点: 0.586 AU 上次通过近日点:1986年2月9日 下次通过近日点:2061年7月28日 # 2019年5月6日 34.772 params = HalleyCometParams( start_time='1982-09-24 00:00:00', # init_velocity=[-2.835, 4.72, 8.847], init_velocity=[-2.826, 4.695, 8.86], # init_velocity=[-2.836, 4.705, 8.85], init_position=[0, -5 * AU, -10 * AU] ) # start_time='1982-09-24 00:00:00', # init_velocity=[-2.836, 4.705, 8.85], # init_position=[0, -5 * AU, -10 * AU] # 34.801 AU(2019-06-10) # 35.086 AU(2023-09-01) # 0.586 AU(1986-02-09) # start_time='1982-09-24 00:00:00', # init_velocity=[-2.837, 4.71, 8.852], # init_position=[0, -5 * AU, -10 * AU] # 34.840AU34.840 AU(2019-05-10) # 35.149 AU(2023-10-08) # 0.586 AU(1986-02-09) # 35.198 AU(2023-11-07) # 0.588 AU(1986-02-09) # start_time='1982-09-24 00:00:00', # init_velocity=[-2.841, 4.7, 8.86], # init_position=[0, -5 * AU, -10 * AU] sim = HalleyCometSim(params) sim.build() # 订阅事件后,上面2个函数功能才会起作用 # 运行中,每时每刻都会触发 on_timer_changed UrsinaEvent.on_timer_changed_subscription(sim.on_timer_changed) # 运行前会触发 on_ready UrsinaEvent.on_ready_subscription(sim.on_ready) # 天体拖尾点击事件 UrsinaEvent.on_body_trail_clicked_subscription(sim.on_body_trail_clicked) # 使用 ursina 查看的运行效果 # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 # position = 左-右+、上+下-、前+后- ursina_run(sim.bodies, SECONDS_PER_YEAR, # position=(0, 2 * AU, -11 * AU), position=(0, 0.5 * AU, -5 * AU), cosmic_bg='', show_trail=True, # bg_music='sounds/no_glory.mp3', show_camera_info=False, show_control_info=False, timer_enabled=True, show_grid=False )