# -*- coding:utf-8 -*- # title :哈雷彗星场景模拟 # description :哈雷彗星场景模拟 # author :Python超人 # date :2023-10-25 # link :https://gitcode.net/pythoncr/ # python_version :3.9 # ============================================================================== import time import math from ursina import camera, application from bodies import Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto from common.celestial_data_service import get_init_pos_vels, init_bodies_reality_pos_vels from common.color_utils import trail_color_brightest from common.consts import SECONDS_PER_YEAR, AU from common.func import calculate_distance from objs import HalleComet, Obj from sim_scenes.func import camera_look_at, two_bodies_colliding, create_text_panel from sim_scenes.func import ursina_run, create_sphere_sky from simulators.ursina.entities.world_grid import WorldGrid 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: """ 哈雷彗星场景模拟 """ def __init__(self): pass def build_solar_system(self): # region 构建太阳系 show_trail = True self.sun = Sun(size_scale=0.8e2, show_trail=show_trail) self.mercury = Mercury(size_scale=5e3, show_trail=show_trail) self.venus = Venus(size_scale=5e3, show_trail=show_trail) self.earth = Earth(size_scale=5e3, rotate_angle=0, show_trail=show_trail) self.mars = Mars(size_scale=5e3, show_trail=show_trail) self.jupiter = Jupiter(size_scale=2.5e3, show_trail=show_trail) self.saturn = Saturn(size_scale=2.5e3, show_trail=show_trail) self.uranus = Uranus(size_scale=6e3, show_trail=show_trail) self.neptune = Neptune(size_scale=6e3, show_trail=show_trail) self.pluto = Pluto(size_scale=1e5, show_trail=show_trail) self.bodies = [ self.sun, # 太阳 self.mercury, # 水星 self.venus, # 金星 self.earth, # 地球 self.mars, # 火星 self.jupiter, # 木星 self.saturn, # 土星 self.uranus, # 天王星 self.neptune, # 海王星 # self.pluto, # 冥王星 ] self.bodies = [ self.sun, # 太阳 self.mars, # 火星 self.neptune, # 海王星 ] # endregion trail_color_brightest(self.bodies) init_bodies_reality_pos_vels(self.bodies) def build_halley_comet(self): """ 创建哈雷彗星 @return: """ # 哈雷彗星的平均运行速度约为每小时 70,000 英里或每小时 126,000 公里 。(35公里/秒) # 每76.1年环绕太阳一周的周期彗星 # 3.335, 0, 10.7 73 # 3.33, 0, 10.7 73 # [3.335, 0, 10.699] 71 # [3.33, 0, 10.655] 68 # [3.33, 0, 10.66] 69 self.halley_comet = HalleComet( # size_scale=4e7, size_scale=1e8, init_velocity=[3.34, 0, 10.7], # [3.33, 0, 10.6] < ? <[3.34, 0, 10.7] init_position=[0, 0.5 * AU, -10 * AU]) \ .set_light_disable(True) self.bodies.append(self.halley_comet) def build(self): self.build_solar_system() # self.build_halley_comet() def calculate_rotation_angles(self, point1, point2): dx = point2.x - point1.x dy = point2.y - point1.y dz = point2.z - point1.z roll = math.atan2(dy, dz) pitch = math.atan2(dx, math.sqrt(dy ** 2 + dz ** 2)) yaw = math.atan2(math.sin(roll), math.cos(roll)) return roll, pitch, yaw def calculate_angles(self, point1, point2): import numpy as np # 计算向量AB AB = point1 - point2 # 计算向量AB与x轴、y轴和z轴之间的夹角 angle_x = np.arctan2(AB.y, AB.x) * 180 / np.pi angle_y = np.arctan2(AB.z, np.sqrt(AB.x ** 2 + AB.y ** 2)) * 180 / np.pi angle_z = np.arctan2(np.sqrt(AB.x ** 2 + AB.y ** 2), AB.z) * 180 / np.pi return angle_x, angle_y, angle_z def calculate_angles(self, point1, point2): # 计算向量AB AB = point2 - point1 # 计算向量AB与x轴、y轴和z轴之间的夹角 angle_x = math.degrees(math.atan2(AB.z, AB.y)) angle_y = math.degrees(math.atan2(AB.z, AB.x)) angle_z = math.degrees(math.atan2(AB.y, AB.x)) return angle_x, angle_y, angle_z def on_ready(self): """ 事件绑定后,模拟器运行前会触发 @return: """ # 创建天空 from ursina import scene UrsinaConfig.trail_type = "line" UrsinaConfig.trail_length = 91 UrsinaConfig.trail_length = 1000 UrsinaConfig.trail_thickness_factor = 3 # camera.clip_plane_near = 0.1 camera.clip_plane_far = 1000000 create_sphere_sky(scale=200000) WorldGrid().draw_axises(10) application.time_scale = 5 self.orbit_lines = [] for body in self.bodies[1:]: if isinstance(body, HalleComet): continue print("create_orbit_line", body) """ # 在 ursina 中,我以 sun.position 为中心, body.position 的所在位置(x,y,z) 画一个圆环(orbit_line)。 到 sun 中心点(x,y,z) 为半径,返回一个圆形轨道 orbit_line orbit_line = create_orbit_line(sun, body) # 帮我解决下面的问题,怎么修改下面的值,让 orbit_line 轨道线的倾斜和 body 匹配 orbit_line.rotation_x = ? orbit_line.rotation_y = ? orbit_line.rotation_z = ? body.position - sun.position """ orbit_line = create_orbit_line(self.sun, body) angle_x, angle_y, angle_z = self.calculate_angles(self.sun.planet.position,body.planet.position) # # 获取body相对于self.sun的位置向量 # relative_position = body.planet.position - self.sun.planet.position # # rotation_x = -math.degrees( # math.atan2(relative_position.y, math.sqrt(relative_position.x ** 2 + relative_position.z ** 2))) # rotation_y = math.degrees(math.atan2(relative_position.x, relative_position.z)) # # 计算旋转角度 # orbit_line.rotation_x = rotation_x + 90 # orbit_line.rotation_z = 0 # orbit_line.rotation_y = rotation_y + 120 # # # angle = math.atan2(relative_position.y, relative_position.x) orbit_line.rotation_x = angle_x orbit_line.rotation_y = angle_y # - 50 orbit_line.rotation_z = -angle_z 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 hasattr(self, "self.halley_comet"): # 哈雷彗星飞行的翻转效果 if self.halley_comet.planet.enabled: self.halley_comet.planet.rotation_x += 0.1 self.halley_comet.planet.rotation_y += 1 # 摄像机始终看向哈雷彗星 # 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 )