From 11ec7b03cfe26b4bcf206fd11127ff2f95e2d90d Mon Sep 17 00:00:00 2001 From: march3 Date: Thu, 6 Apr 2023 21:57:23 +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_scenes/func.py | 72 +++++++++++++++ sim_scenes/solar_system/speed_of_light.py | 101 +++++----------------- 2 files changed, 92 insertions(+), 81 deletions(-) diff --git a/sim_scenes/func.py b/sim_scenes/func.py index 8456dd7..bc07b45 100644 --- a/sim_scenes/func.py +++ b/sim_scenes/func.py @@ -154,6 +154,78 @@ def create_fig_ax(styles={}): return fig, ax +def create_solar_system_bodies(ignore_mass=False, init_velocity=None): + """ + 创建太阳系天体(忽略质量,引力无效,初速度全部为0) + 太阳、小行星环、 + 八大行星:木星(♃)、土星(♄)、天王星(♅)、海王星(♆)、地球(⊕)、金星(♀)、火星(♂)、水星(☿) + 冥王星 + 以下展示的效果为太阳系真实的距离 + @return: + """ + from bodies import Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto + sun = Sun(name="太阳", size_scale=0.6e2) # 太阳放大 60 倍,距离保持不变 + bodies = [ + sun, + Mercury(name="水星", size_scale=2e3), # 水星放大 2000 倍,距离保持不变 + Venus(name="金星", size_scale=2e3), # 金星放大 2000 倍,距离保持不变 + Earth(name="地球", size_scale=2e3), # 地球放大 2000 倍,距离保持不变 + Mars(name="火星", size_scale=2e3), # 火星放大 2000 倍,距离保持不变 + # Asteroids(name="小行星群", size_scale=3.2e2, + # parent=sun), # 小行星群模拟(仅 ursina 模拟器支持) + Jupiter(name="木星", size_scale=0.6e3), # 木星放大 600 倍,距离保持不变 + Saturn(name="土星", size_scale=0.6e3), # 土星放大 600 倍,距离保持不变 + Uranus(name="天王星", size_scale=0.7e3), # 天王星放大 700 倍,距离保持不变 + Neptune(name="海王星", size_scale=1e3), # 海王星放大 1000 倍,距离保持不变 + Pluto(name="冥王星", size_scale=10e3), # 冥王星放大 10000 倍,距离保持不变(从太阳系的行星中排除) + ] + + # 遍历所有天体, + for idx, body in enumerate(bodies): + body.set_ignore_mass(ignore_mass) # 忽略质量(引力无效) + if init_velocity is not None: + body.init_velocity = init_velocity + return bodies + + +def create_light(size_scale, init_position): + """ + 用天体模拟一个光子 + @return: + """ + from bodies import Body + return Body(name='光速', mass=0, size_scale=size_scale, color=(255, 255, 0), + init_position=init_position, + init_velocity=[0, 0, 299792.458]).set_light_disable(True) # 1光速=299792.458 千米/秒(km/秒) + + +def create_text_panel(width=0.35, height=.5): + # 创建一个 Panel 组件 + from ursina import Text, Panel, color, camera, Vec3 + panel = Panel( + parent=None, + model='quad', + # texture='white_cube', + color=color.gray, + origin=(-.48, .48), + scale=(width, height), + position=(-.88, 0.3, 0), + alpha=0.2 + ) + + # 创建一个 Text 组件用于显示消息 + text = Text( + parent=panel, + text='', + origin=(-.5, .5), + scale=(height * 5, width * 5), + font=UrsinaConfig.CN_FONT, + # background=True, + # background_color=color.clear + ) + return text + + if __name__ == '__main__': from bodies import Sun, Earth diff --git a/sim_scenes/solar_system/speed_of_light.py b/sim_scenes/solar_system/speed_of_light.py index 4984b05..0151366 100644 --- a/sim_scenes/solar_system/speed_of_light.py +++ b/sim_scenes/solar_system/speed_of_light.py @@ -8,112 +8,52 @@ # ============================================================================== from bodies import Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto, Moon, Asteroids, Body from common.consts import SECONDS_PER_WEEK, SECONDS_PER_DAY, SECONDS_PER_YEAR, AU -from sim_scenes.func import mayavi_run, ursina_run +from sim_scenes.func import mayavi_run, ursina_run, create_solar_system_bodies, create_text_panel, create_light from simulators.ursina.ursina_config import UrsinaConfig from simulators.ursina.ursina_event import UrsinaEvent from ursina import Text, Panel, color, camera, Vec3 - -def create_bodies(): - """ - 创建太阳系天体(忽略质量,引力无效,初速度全部为0) - 太阳、小行星环、 - 八大行星:木星(♃)、土星(♄)、天王星(♅)、海王星(♆)、地球(⊕)、金星(♀)、火星(♂)、水星(☿) - 冥王星 - 以下展示的效果为太阳系真实的距离 - @return: - """ - sun = Sun(name="太阳", size_scale=0.6e2) # 太阳放大 60 倍,距离保持不变 - bodies = [ - sun, - Mercury(name="水星", size_scale=2e3), # 水星放大 2000 倍,距离保持不变 - Venus(name="金星", size_scale=2e3), # 金星放大 2000 倍,距离保持不变 - Earth(name="地球", size_scale=2e3), # 地球放大 2000 倍,距离保持不变 - Mars(name="火星", size_scale=2e3), # 火星放大 2000 倍,距离保持不变 - # Asteroids(name="小行星群", size_scale=3.2e2, - # parent=sun), # 小行星群模拟(仅 ursina 模拟器支持) - Jupiter(name="木星", size_scale=0.6e3), # 木星放大 600 倍,距离保持不变 - Saturn(name="土星", size_scale=0.6e3), # 土星放大 600 倍,距离保持不变 - Uranus(name="天王星", size_scale=0.7e3), # 天王星放大 700 倍,距离保持不变 - Neptune(name="海王星", size_scale=1e3), # 海王星放大 1000 倍,距离保持不变 - Pluto(name="冥王星", size_scale=10e3), # 冥王星放大 10000 倍,距离保持不变(从太阳系的行星中排除) - ] - - # 遍历所有天体, - for idx, body in enumerate(bodies): - body.set_ignore_mass(True) # 忽略质量(引力无效) - body.init_velocity = [0, 0, 0] # 初速度为0 - return bodies - - -def create_light(size_scale=1e4): - """ - 用天体模拟一个光子 - @return: - """ - return Body(name='光速', mass=0, size_scale=size_scale, color=(255, 255, 0), - # init_position=[AU / 3, 0, 0], - init_position=[AU / 12, 0, 0], - init_velocity=[0, 0, 299792.458]).set_light_disable(True) # 1光速=299792.458 千米/秒(km/秒) - - -def create_text_panel(width=0.35, height=.5): - # 创建一个 Panel 组件 - panel = Panel( - parent=None, - model='quad', - # texture='white_cube', - color=color.gray, - origin=(-.48, .48), - scale=(width, height), - position=(-.88, 0.3, 0), - alpha=0.2 - ) - - # 创建一个 Text 组件用于显示消息 - text = Text( - parent=panel, - text='', - origin=(-.5, .5), - scale=(height * 5, width * 5), - font=UrsinaConfig.CN_FONT, - # background=True, - # background_color=color.clear - ) - return text - - # 已到达天体列表 arrived_bodies = [] text_panel = None arrived_info = "" CAMERA_FOLLOW_LIGHT = None # 不跟随光 -CAMERA_FOLLOW_LIGHT = 'ForwardView' # 向前看 +# CAMERA_FOLLOW_LIGHT = 'ForwardView' # 向前看 # CAMERA_FOLLOW_LIGHT = 'SideView' # 侧面看 def on_reset(): global arrived_info arrived_bodies.clear() - arrived_info = "" + arrived_info = "[00:00:00] 从 [太阳] 出发\n\n" if text_panel is not None: - text_panel.text = "" + text_panel.text = arrived_info + + +on_reset() + +# 订阅重新开始事件 +UrsinaEvent.on_reset_subscription(on_reset) def on_ready(): global text_panel text_panel = create_text_panel() + text_panel.text = arrived_info if CAMERA_FOLLOW_LIGHT == "SideView": camera.parent = light_body.planet camera.rotation_y = -85 elif CAMERA_FOLLOW_LIGHT == "ForwardView": - camera.parent = light_body.planet light_body.planet.enabled = False + camera.parent = light_body.planet camera.rotation_y = -15 +UrsinaEvent.on_ready_subscription(on_ready) + + def on_timer_changed(time_text, time_data): global arrived_info years, days, hours, minutes, seconds = time_data @@ -133,28 +73,27 @@ def on_timer_changed(time_text, time_data): # 订阅计时器事件(记录已到达天体列表) UrsinaEvent.on_timer_changed_subscription(on_timer_changed) -# 订阅重新开始事件 -UrsinaEvent.on_reset_subscription(on_reset) - -UrsinaEvent.on_ready_subscription(on_ready) if CAMERA_FOLLOW_LIGHT == "SideView": position = (2 * AU, 0, -AU / 8) show_trail = True light_size_scale = 1e3 + light_init_position = [AU / 3, 0, 0] elif CAMERA_FOLLOW_LIGHT == "ForwardView": position = (0, AU / 10, -AU) show_trail = False light_size_scale = 1e2 + light_init_position = [AU / 12, 0, 0] else: position = (0, 2 * AU, -11 * AU) show_trail = True light_size_scale = 5e3 + light_init_position = [AU / 3, 0, 0] # 创建太阳系天体(忽略质量,引力无效,初速度全部为0) -bodies = create_bodies() +bodies = create_solar_system_bodies(ignore_mass=True, init_velocity=[0, 0, 0]) # 创建一个以光速前进的天体(模拟一个光子,质量为0才能达到光速) -light_body = create_light(size_scale=light_size_scale) +light_body = create_light(light_size_scale, light_init_position) bodies.append(light_body) -- GitLab