# -*- coding:utf-8 -*- # title :哈雷彗星场景模拟 # description :哈雷彗星场景模拟 # author :Python超人 # date :2023-10-25 # link :https://gitcode.net/pythoncr/ # python_version :3.9 # ============================================================================== from ursina import camera, application from common.celestial_data_service import init_bodies_pos_vels 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, create_halley_comet from simulators.ursina.ursina_config import UrsinaConfig from simulators.ursina.ursina_event import UrsinaEvent from simulators.ursina.ursina_mesh import create_orbit_line class HalleyCometSim(HalleyCometSimBase): """ 哈雷彗星场景模拟 """ def build(self): self.build_solar_system() self.halley_comet = create_halley_comet([-3.34, 3, 10.718], [0, -2 * AU, -10 * AU]) self.bodies.append(self.halley_comet) def on_ready(self): """ 事件绑定后,模拟器运行前会触发 @return: """ # 创建天空 UrsinaConfig.trail_type = "line" UrsinaConfig.trail_length = 91 UrsinaConfig.trail_thickness_factor = 3 # camera.clip_plane_near = 0.1 camera.clip_plane_far = 1000000 create_sphere_sky(scale=200000) application.time_scale = 5 self.orbit_lines = [] for body in self.bodies[1:]: if isinstance(body, HalleComet): continue print("create_orbit_line", body) orbit_line = create_orbit_line(self.sun, body) orbit_line.body = body self.orbit_lines.append(orbit_line) self.text_panel = create_text_panel() def on_timer_changed(self, time_data): """ @param time_data: @return: """ # 哈雷彗星飞行的翻转效果 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) # 摄像机始终看向哈雷彗星 # camera_look_at(self.halley_comet) d = calculate_distance(self.halley_comet.position, self.sun.position) self.text_panel.text = "哈雷彗星距离太阳:%.3f AU" % (d / AU) for i, orbit_line in enumerate(self.orbit_lines): if i < 4: adj_scale = False else: adj_scale = True # 由于天体运行不是标准的圆形,则需要动态调整轨道的大小,保证轨道线始终在天体的中心位置 orbit_line.auto_adjust(adj_scale=adj_scale) if __name__ == '__main__': """ 哈雷彗星场景模拟 """ sim = HalleyCometSim() sim.build() # 订阅事件后,上面2个函数功能才会起作用 # 运行中,每时每刻都会触发 on_timer_changed UrsinaEvent.on_timer_changed_subscription(sim.on_timer_changed) # 运行前会触发 on_ready UrsinaEvent.on_ready_subscription(sim.on_ready) # 使用 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, show_timer=True, show_grid=False)