From 4a9336908011b3d31d6116a7f6c93b198e22f068 Mon Sep 17 00:00:00 2001 From: march3 Date: Fri, 17 Nov 2023 20:58:44 +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 --- bodies/__init__.py | 2 + bodies/universe_body.py | 77 ++++++++++++++ sim_lab/wormhole_sim.py | 221 ++++++++++++++-------------------------- 3 files changed, 157 insertions(+), 143 deletions(-) create mode 100644 bodies/universe_body.py diff --git a/bodies/__init__.py b/bodies/__init__.py index 7ed7644..dafd149 100644 --- a/bodies/__init__.py +++ b/bodies/__init__.py @@ -43,3 +43,5 @@ from bodies.fixed_stars.procyon import Procyon # 南河三 # from bodies.color_body import ColorBody + +from bodies.universe_body import UniverseBody diff --git a/bodies/universe_body.py b/bodies/universe_body.py new file mode 100644 index 0000000..a6de2fc --- /dev/null +++ b/bodies/universe_body.py @@ -0,0 +1,77 @@ +# -*- coding:utf-8 -*- +# title :宇宙体 +# description :模拟一个宇宙体 +# author :Python超人 +# date :2023-11-17 +# link :https://gitcode.net/pythoncr/ +# python_version :3.9 +# ============================================================================== + +from bodies import Body + + +class UniverseBody(Body): + def __init__(self, name="宇宙体", mass=1.9891e35, + init_position=[0, 0, 0], + init_velocity=[0, 0, 0], + color=(0, 0, 0), + texture=None, + density=1.408e20, + size_scale=1.0, distance_scale=1.0, + rotation_speed=0, ignore_mass=False, trail_color=None, show_name=False): + params = { + "name": name, + "mass": mass, + "init_position": init_position, + "init_velocity": init_velocity, + "density": density, + "color": color, + "texture": texture, + "size_scale": size_scale * 10000, + "distance_scale": distance_scale, + "rotation_speed": rotation_speed, + "ignore_mass": ignore_mass, + "trail_color": trail_color, + "show_name": show_name + } + super().__init__(**params) + # self.set_resolution(50) + self.set_ignore_gravity(True) + self.set_light_disable(False) + + +class OursUniverse(UniverseBody): + def __init__(self, name="我们的宇宙", + size_scale=1.0, + init_position=[0, 0, 0], + init_velocity=[0, 0, 0]): + params = { + "name": name, + "mass": 1.9891e35, + "init_position": init_position, + "init_velocity": init_velocity, + "density": 1.408e20, + # "color": color, + "texture": 'bg_pan.jpg', + "size_scale": size_scale, + # "distance_scale": distance_scale, + + # "ignore_mass": ignore_mass, + # "trail_color": trail_color, + # "show_name": show_name + } + super().__init__(**params) + + +def create_universe_body(name, texture, size_scale=1.0, init_position=[0, 0, 0], init_velocity=[0, 0, 0]): + params = { + "name": name, + "mass": 1.9891e35, + "init_position": init_position, + "init_velocity": init_velocity, + "density": 1.408e20, + # "color": color, + "texture": texture, + "size_scale": size_scale, + } + return UniverseBody(**params) diff --git a/sim_lab/wormhole_sim.py b/sim_lab/wormhole_sim.py index 5f3eedf..3c293c1 100644 --- a/sim_lab/wormhole_sim.py +++ b/sim_lab/wormhole_sim.py @@ -7,6 +7,7 @@ # python_version :3.9 # ============================================================================== from bodies import Sun, Earth +from bodies.universe_body import create_universe_body from common.consts import SECONDS_PER_DAY, SECONDS_PER_WEEK, SECONDS_PER_MONTH, SECONDS_PER_YEAR, AU from objs import Obj, SpaceShip, StarWarsSpeeder, ScifiGunship, SciFiBomber, CoreValagaClas from sim_scenes.func import mayavi_run, mpl_run, ursina_run, camera_look_at, two_bodies_colliding @@ -15,114 +16,37 @@ from ursina import camera, application, lerp from bodies import Body from simulators.ursina.ursina_event import UrsinaEvent -ours_cosmic_bg = "bg_pan.jpg" -worms_cosmic_bg = "bg_pan_red.jpg" - - -class UniverseBody(Body): - def __init__(self, name="虫洞", mass=1.9891e35, - init_position=[0, 0, 0], - init_velocity=[0, 0, 0], - color=(0, 0, 0), - texture=None, - density=1.408e20, - size_scale=1.0, distance_scale=1.0, - rotation_speed=0, ignore_mass=False, trail_color=None, show_name=False): - params = { - "name": name, - "mass": mass, - "init_position": init_position, - "init_velocity": init_velocity, - "density": density, - "color": color, - "texture": texture, - "size_scale": size_scale * 10000, - "distance_scale": distance_scale, - "rotation_speed": rotation_speed, - "ignore_mass": ignore_mass, - "trail_color": trail_color, - "show_name": show_name - } - super().__init__(**params) - - -class WormHole(UniverseBody): - def __init__(self, name="虫洞", mass=1.9891e35, texture=worms_cosmic_bg, size_scale=1.0, - init_position=[0, 0, 0], init_velocity=[0, 0, 0]): - params = { - "name": name, - "mass": mass, - "init_position": init_position, - "init_velocity": init_velocity, - "density": 1.408e20, - # "color": color, - "texture": texture, - "size_scale": size_scale, - # "distance_scale": distance_scale, - # "rotation_speed": rotation_speed, - # "ignore_mass": ignore_mass, - # "trail_color": trail_color, - # "show_name": show_name - } - super().__init__(**params) - - -class OursUniverse(UniverseBody): - def __init__(self, name="我们的宇宙", texture=ours_cosmic_bg, size_scale=1.0, - init_position=[0, 0, 0], init_velocity=[0, 0, 0]): - params = { - "name": name, - "mass": 1.9891e35, - "init_position": init_position, - "init_velocity": init_velocity, - "density": 1.408e20, - # "color": color, - "texture": texture, - "size_scale": size_scale, - # "distance_scale": distance_scale, - - # "ignore_mass": ignore_mass, - # "trail_color": trail_color, - # "show_name": show_name - } - super().__init__(**params) +ours_cosmic_bg = "cosmic_pan_1.jpg" +worms_cosmic_bg = "cosmic_pan_4.jpg" -if __name__ == '__main__': - """ - - """ - # 代码案例如下: +class WormholeSim: SIZE_SCALE = 1 # 生成的太阳放大 50 倍 RUN_DIAMETER = 1.392e6 velocity_rate = 22 - camera_target = CoreValagaClas(name="摄像机目标", mass=1e30, color=(111, 140, 255), - # init_position=[0, 0, 0], - init_position=[0, 0, AU / 10], - init_velocity=[0, 0, 0], - size_scale=SIZE_SCALE * 1e1) # .set_ignore_gravity(True) - ours_universe = OursUniverse( - # size_scale=SIZE_SCALE * 1e3, - size_scale=SIZE_SCALE, - init_position=[0, 0, -AU/120], - init_velocity=[0, 0, 0], - ).set_ignore_gravity(True) - worm_hole = WormHole( - # init_position=[RUN_DIAMETER, RUN_DIAMETER, 0], - init_position=[0, 0, AU/120], - init_velocity=[0, 0, 0], - size_scale=SIZE_SCALE).set_ignore_gravity(True) - bodies = [ - camera_target, - ours_universe, - worm_hole - ] - - # camera_position = [0, 0, 0] - - - def on_ready(): + def __init__(self): + self.camera_target = CoreValagaClas(name="摄像机目标", mass=1e30, color=(111, 140, 255), + # init_position=[0, 0, 0], + init_position=[0, 0, AU / 10], + init_velocity=[0, 0, 0], + size_scale=WormholeSim.SIZE_SCALE * 1e1) # .set_ignore_gravity(True) + self.universes = [] + + self.ours_universe = create_universe_body("我们的宇宙", "cosmic_pan_1.jpg", + size_scale=WormholeSim.SIZE_SCALE, + init_position=[0, 0, -AU / 120], + init_velocity=[0, 0, 0], + ) + self.universes.append(self.ours_universe) + self.worm_hole = create_universe_body("虫洞1", "cosmic_pan_4.jpg", + init_position=[0, 0, AU / 120], + init_velocity=[0, 0, 0], + size_scale=WormholeSim.SIZE_SCALE) + self.universes.append(self.worm_hole) + self.bodies = [self.camera_target] + self.universes + + def on_ready(self): """ 事件绑定后,模拟器运行前会触发 @return: @@ -135,29 +59,30 @@ if __name__ == '__main__': # camera.scale = 1000 # camera.parent = camera_target.planet # camera.rotation_x = 90 + camera.fov = 120 - worm_hole.planet.rotation_y = 30 - ours_universe.planet.rotation_y = 130 + self.worm_hole.planet.rotation_y = 30 + self.ours_universe.planet.rotation_y = 130 - camera_look_at(camera_target) + camera_look_at(self.camera_target) camera.collider = "sphere" - worm_hole.planet.collider = "sphere" - ours_universe.planet.init_scale = SIZE_SCALE * 1000 + self.worm_hole.planet.collider = "sphere" + self.ours_universe.planet.init_scale = self.SIZE_SCALE * 1000 + self.camera_target.current_universe = self.ours_universe + self.camera_target.targe_universe = self.worm_hole # camera_position = camera.position - - def on_timer_changed(time_data): - global camera_position + def on_timer_changed(self, time_data): # return # pass # camera_look_at(camera_target) # camera.rotation_x -= 2 - camera_target.planet.look_at(worm_hole.planet) - if camera.intersects(worm_hole.planet).hit: + self.camera_target.planet.look_at(self.camera_target.targe_universe.planet) + if camera.intersects(self.camera_target.targe_universe.planet).hit: # worm_hole.planet.init_scale = SIZE_SCALE * 1000 - ours_universe.planet.init_scale = SIZE_SCALE - if worm_hole.planet.init_scale < SIZE_SCALE*1000: - worm_hole.planet.init_scale *= 1.1 + self.camera_target.current_universe.planet.init_scale = self.SIZE_SCALE + if self.camera_target.targe_universe.planet.init_scale < self.SIZE_SCALE * 1000: + self.camera_target.targe_universe.planet.init_scale *= 1.1 # if ours_universe.planet.init_scale > SIZE_SCALE: # ours_universe.planet.init_scale /= 1.1 if not hasattr(camera, "init_rotation_y"): @@ -165,11 +90,11 @@ if __name__ == '__main__': camera.rotation_y_v = 0.01 if abs(camera.init_rotation_y - camera.rotation_y) < 10: - camera.rotation_y_v += 0.005 + camera.rotation_y_v += 0.5 elif abs(camera.init_rotation_y - camera.rotation_y) < 90: - camera.rotation_y_v += 0.01 + camera.rotation_y_v += 0.3 elif abs(camera.init_rotation_y - camera.rotation_y) < 120: - camera.rotation_y_v -= 0.01 + camera.rotation_y_v -= 0.1 elif abs(camera.init_rotation_y - camera.rotation_y) < 180: camera.rotation_y_v -= 0.01 if camera.rotation_y_v < 0.1: @@ -179,33 +104,43 @@ if __name__ == '__main__': camera.rotation_y += camera.rotation_y_v # camera_look_at(ours_universe) # else: - # if two_bodies_colliding(camera_target, worm_hole): - # camera_target.stop_and_ignore_gravity() - # print(1,camera_target.planet.position) - # camera_target.planet.position = lerp(camera_target.planet.position, worm_hole.planet.position, 1e-2) - # print(2,camera_target.planet.position) - # camera.position = (camera.position[0],camera.position[1],camera.position[2]) # - camera.position = lerp(camera.position, camera_target.planet.position, 0.3e-2) + # if two_bodies_colliding(camera_target, worm_hole): + # camera_target.stop_and_ignore_gravity() + # print(1,camera_target.planet.position) + # camera_target.planet.position = lerp(camera_target.planet.position, worm_hole.planet.position, 1e-2) + # print(2,camera_target.planet.position) + # camera.position = (camera.position[0],camera.position[1],camera.position[2]) # + camera.position = lerp(camera.position, self.camera_target.planet.position, 0.2e-2) # camera_position = [camera_position[0], camera_position[1], camera_position[2] - 0.01] # camera.position = camera_position + def build_events(self): + # 订阅事件后,上面2个函数功能才会起作用 + # 运行中,每时每刻都会触发 on_timer_changed + UrsinaEvent.on_timer_changed_subscription(wormhole_sim.on_timer_changed) + # 运行前会触发 on_ready + UrsinaEvent.on_ready_subscription(wormhole_sim.on_ready) + + def run(self): + self.build_events() + # 使用 ursina 查看的运行效果 + # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 + # position = 左-右+、上+下-、前+后- + ursina_run(self.bodies, SECONDS_PER_DAY / 24, + # position=(0, AU / 1200, -1.003 * AU), + # position=[-AU/100, 0, 0], + position=[0, 0, -AU / 10], + cosmic_bg='', + # gravity_works=False, + timer_enabled=True, + show_grid=False, + show_trail=False) - # 订阅事件后,上面2个函数功能才会起作用 - # 运行中,每时每刻都会触发 on_timer_changed - UrsinaEvent.on_timer_changed_subscription(on_timer_changed) - # 运行前会触发 on_ready - UrsinaEvent.on_ready_subscription(on_ready) - - # 使用 ursina 查看的运行效果 - # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 - # position = 左-右+、上+下-、前+后- - ursina_run(bodies, SECONDS_PER_DAY / 24, - # position=(0, AU / 1200, -1.003 * AU), - # position=[-AU/100, 0, 0], - position=[0, 0, -AU/10], - cosmic_bg='', - # gravity_works=False, - timer_enabled=True, - show_grid=False, - show_trail=False) + +if __name__ == '__main__': + """ + + """ + wormhole_sim = WormholeSim() + wormhole_sim.run() -- GitLab