From 5bc3009e0ed3c3ccf57ad198530f418dce3ad1f6 Mon Sep 17 00:00:00 2001 From: march3 Date: Fri, 3 Nov 2023 12:35:27 +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/solarsystem_planets_size.py | 72 ++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 7 deletions(-) diff --git a/sim_lab/solarsystem_planets_size.py b/sim_lab/solarsystem_planets_size.py index 4ce8933..3de8914 100644 --- a/sim_lab/solarsystem_planets_size.py +++ b/sim_lab/solarsystem_planets_size.py @@ -6,9 +6,11 @@ # link :https://gitcode.net/pythoncr/ # python_version :3.8 # ============================================================================== +import threading + from bodies import Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto, Moon from common.consts import SECONDS_PER_HOUR, SECONDS_PER_DAY, AU -from sim_scenes.func import ursina_run +from sim_scenes.func import ursina_run, create_sphere_sky from simulators.ursina.entities.body_timer import TimeData, AppTimeUtil from simulators.ursina.entities.entity_utils import create_directional_light from simulators.ursina.ursina_event import UrsinaEvent @@ -27,7 +29,7 @@ bodies = [ Venus(name="金星", size_scale=FACTOR), earth2, Mars(name="火星", size_scale=FACTOR), - Jupiter(name="木星", size_scale=FACTOR), + Jupiter(name="木星", texture='jupiter_hd.jpg', size_scale=FACTOR), Saturn(name="土星", size_scale=FACTOR).show_rings(False), Uranus(name="天王星", size_scale=FACTOR), Neptune(name="海王星", size_scale=FACTOR), @@ -59,8 +61,12 @@ if __name__ == '__main__': create_directional_light(position=(E_M_DISTANCE / 2, E_M_DISTANCE * 20, -E_M_DISTANCE * 100), light_num=3, target=earth1) - application.time_scale = 0.01 - + application.time_scale = 0.001 + sky = create_sphere_sky(scale=2000) + sky.alpha = 0.5 + # sky.rotation_y = -90 + sky.rotation_x = 200 + # sky.rotation_z = 200 key_points = [(E_M_DISTANCE / 2, 0, -E_M_DISTANCE), (0, 0, -E_M_DISTANCE / 4), @@ -83,6 +89,29 @@ if __name__ == '__main__': interval = 3 + # def move_planet(planet_body, target_pos): + # planet_body.planet.origin_update = planet_body.planet.update + # + # def new_update(): + # import numpy as np + # current_pos = planet_body.init_position + # c_pos = current_pos * UrsinaConfig.SCALE_FACTOR + # t_pos = np.array(target_pos) * UrsinaConfig.SCALE_FACTOR + # # for _ in range(10): + # # current_pos = planet_body.init_position + # # new_pos = ursina.lerp(Vec3(current_pos[0], current_pos[1], current_pos[2]), + # # Vec3(target_pos[0], target_pos[1], target_pos[2]), 1e9) + # # planet_body.init_position = [new_pos[0], new_pos[1], new_pos[2]] + # n_pos = ursina.lerp(Vec3(c_pos[0], c_pos[1], c_pos[2]), + # Vec3(t_pos[0], t_pos[1], t_pos[2]), 3e-2) + # planet_body.planet.origin_update() + # new_pos = np.array([n_pos[0], n_pos[1], n_pos[2]]) / UrsinaConfig.SCALE_FACTOR + # planet_body.init_position = new_pos + # + # planet_body.planet.update = new_update + # # return warp + + def on_timer_changed(time_data: TimeData): # camera_time = app_time_util.get_param("camera_time", 2) @@ -97,7 +126,34 @@ if __name__ == '__main__': last_time = app_time_util.get_param("last_time", interval + 3) # 间隔3秒出现水星 # 判断是否第一个到达指定的时间 if app_time_util.is_first_arrival(last_time, time_data): - bodies[body_index].init_position = plant_positions[body_index - 2] + # target_position = plant_positions[body_index - 2] + + def move_planet(planet_body, target_pos): + import numpy as np + + def warp(): + # current_pos = planet_body.init_position + t_pos = np.array(target_pos) * UrsinaConfig.SCALE_FACTOR + for _ in range(200): + current_pos = planet_body.init_position + c_pos = current_pos * UrsinaConfig.SCALE_FACTOR + n_pos = ursina.lerp(Vec3(c_pos[0], c_pos[1], c_pos[2]), + Vec3(t_pos[0], t_pos[1], t_pos[2]), 6e-2) + new_pos = np.array([n_pos[0], n_pos[1], n_pos[2]]) / UrsinaConfig.SCALE_FACTOR + planet_body.init_position = new_pos + time.sleep(0.0001) + + planet_body.init_position = target_pos + + + return warp + + f = move_planet(bodies[body_index], plant_positions[body_index - 2]) + # f() + t = threading.Thread(target=f) + t.start() + # time.sleep(0.1) + app_time_util.inc_param("body_index", 1) app_time_util.inc_param("last_time", interval) # 间隔3秒出现下一个行星 @@ -123,7 +179,9 @@ if __name__ == '__main__': # 使用 ursina 查看的运行效果 # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 # position = 左-右+、上+下-、前+后- - ursina_run(bodies, SECONDS_PER_HOUR, + ursina_run(bodies, SECONDS_PER_HOUR * 2, position=key_points[0], - view_closely=True, + cosmic_bg='', + show_grid=False, + # view_closely=True, timer_enabled=True) -- GitLab