From 7461139eaa58c17d48c1990890f4ada65ef490fe Mon Sep 17 00:00:00 2001 From: march3 Date: Thu, 9 Nov 2023 21:18:18 +0800 Subject: [PATCH] =?UTF-8?q?Python=E8=B6=85=E4=BA=BA-=E5=AE=87=E5=AE=99?= =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sim_lab/halley_comet_research_calc.py | 35 +++++ sim_scenes/solar_system/halley_comet_lib.py | 26 ++++ sim_scenes/solar_system/halley_comet_sim.py | 135 ++++++++------------ 3 files changed, 116 insertions(+), 80 deletions(-) diff --git a/sim_lab/halley_comet_research_calc.py b/sim_lab/halley_comet_research_calc.py index 6e32df6..7b59514 100644 --- a/sim_lab/halley_comet_research_calc.py +++ b/sim_lab/halley_comet_research_calc.py @@ -251,6 +251,41 @@ def target_function(x, y, z): if __name__ == '__main__': pass + + + # 近日点 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]) \ + # 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] + # 2023年12月9日 - 1986年2月9日 = 13817 # 35.1AU 0.586AU # target_function(-2.836, 4.705, 8.85) diff --git a/sim_scenes/solar_system/halley_comet_lib.py b/sim_scenes/solar_system/halley_comet_lib.py index ab67d6b..5fe6c5b 100644 --- a/sim_scenes/solar_system/halley_comet_lib.py +++ b/sim_scenes/solar_system/halley_comet_lib.py @@ -15,6 +15,7 @@ from common.celestial_data_service import get_reality_orbit_points, init_bodies_ from common.color_utils import trail_color_brightest from common.consts import AU from objs import HalleComet +from simulators.ursina.ui.control_ui import ControlUI from simulators.ursina.ursina_mesh import create_orbit_by_points @@ -100,3 +101,28 @@ class HalleyCometSimBase: init_bodies_reality_pos_vels(self.bodies, start_time) else: init_bodies_pos_vels(self.bodies) + + def show_grid_axises(self): + """ + 显示网格以及坐标线 + @return: + """ + from simulators.ursina.entities.world_grid import WorldGrid + WorldGrid().draw_axises(10) + + 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) diff --git a/sim_scenes/solar_system/halley_comet_sim.py b/sim_scenes/solar_system/halley_comet_sim.py index af086bd..9858b26 100644 --- a/sim_scenes/solar_system/halley_comet_sim.py +++ b/sim_scenes/solar_system/halley_comet_sim.py @@ -31,35 +31,33 @@ class HalleyCometSim(HalleyCometSimBase): 哈雷彗星场景模拟 """ - def __init__(self, params=None): + def __init__(self, _params=None): super(HalleyCometSim, self).__init__() - if params is None: + if _params is None: self.params = HalleyCometParams() else: - self.params = params + self.params = _params - if isinstance(params.start_time, str): - self.start_time = conv_to_astropy_time(params.start_time) + if isinstance(_params.start_time, str): + self.start_time = conv_to_astropy_time(_params.start_time) else: - self.start_time = params.start_time + self.start_time = _params.start_time # print("北京时间:", dt.to_datetime(timezone=pytz.timezone('Asia/Shanghai'))) def build(self): + """ + 构建太阳系系统以及哈雷彗星 + @return: + """ 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): + def init_settings(self): """ - 事件绑定后,模拟器运行前会触发 + 初始化设置 @return: """ # 创建天空 @@ -74,34 +72,35 @@ class HalleyCometSim(HalleyCometSimBase): # 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) + def on_ready(self): + """ + 事件绑定后,模拟器运行前会触发 + @return: + """ + # 初始化设置 + self.init_settings() + + # 显示网格以及坐标线 + # self.show_grid_axises() + + # 创建太阳系天体的真实轨迹(太阳和哈雷彗星除外) + self.create_orbit_lines() + # 创建信息显示面板 self.text_panel = create_text_panel() - def show_clock(self, dt): + def create_orbit_lines(self): """ - 显示时钟 - @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) + 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) def set_bodies_position(self, time_data: TimeData): """ @@ -113,28 +112,41 @@ class HalleyCometSim(HalleyCometSimBase): set_solar_system_celestial_position(self.bodies, t, False) def create_year_label(self, trail, year, halley_comet_pos): + """ + 在界面上创建年份的标签 + @param trail: + @param year: + @param halley_comet_pos: + @return: + """ 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 show_comet_trail(self, distance_sun): + def set_comet_trail_alpha(self, distance_sun): + """ + 根据彗哈雷星和太阳的距离,设置彗星尾巴的透明度来模仿接近太阳有慧尾,离开太阳到一定距离就渐渐消失 + @param distance_sun: 彗哈雷星和太阳的距离 + @return: + """ + # 距离转为天文单位 d_au = distance_sun / AU + # 渐渐消失的距离范围(开始消失距离, 完全消失距离) HIDE_DISTANCE = 1, 15 + # 彗星最大的透明度 MAX_ALPHA = 0.8 + + # 大于完全消失距离 if d_au >= HIDE_DISTANCE[1]: alpha = 0 - elif HIDE_DISTANCE[1] > d_au > HIDE_DISTANCE[0]: + elif HIDE_DISTANCE[1] > d_au > HIDE_DISTANCE[0]: # 渐渐消失的距离范围内,通过距离值大小确定透明度(慢慢消失的效果) alpha = MAX_ALPHA - (d_au - HIDE_DISTANCE[0]) / (HIDE_DISTANCE[1] - HIDE_DISTANCE[0]) * MAX_ALPHA else: alpha = MAX_ALPHA - - if alpha > MAX_ALPHA: - alpha = MAX_ALPHA - + # 修改彗星尾巴的透明度 c = self.halley_comet.planet.children[0] c.alpha = alpha @@ -148,12 +160,8 @@ class HalleyCometSim(HalleyCometSimBase): 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() @@ -200,7 +208,7 @@ class HalleyCometSim(HalleyCometSimBase): self.comet_aphel = d_sun self.comet_aphel_dt = dt.strftime("%Y-%m-%d") - self.show_comet_trail(d_sun) + self.set_comet_trail_alpha(d_sun) panel_text = "哈雷彗星:\n距离太阳:%.3f AU" % (d_sun / AU) # panel_text += "\n离日最远:%.3f AU(%s)" % (self.comet_aphel / AU, self.comet_aphel_dt) @@ -237,19 +245,6 @@ 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 @@ -261,26 +256,6 @@ if __name__ == '__main__': 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() -- GitLab