From 5bdd5f35deba9cdb17a466a162bedcff78007545 Mon Sep 17 00:00:00 2001 From: march3 <13505732@qq.com> Date: Thu, 2 Mar 2023 21:40:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=AA=E9=98=B3=E7=B3=BB=E4=B8=89=E4=BD=93?= =?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 --- scenes/func.py | 32 ++++++++++++++++++++++++++++-- simulators/ursina_simulator.py | 35 ++++++++++++++++++--------------- simulators/views/ursina_view.py | 2 +- 3 files changed, 50 insertions(+), 19 deletions(-) diff --git a/scenes/func.py b/scenes/func.py index da90d36..1727787 100644 --- a/scenes/func.py +++ b/scenes/func.py @@ -7,7 +7,7 @@ # python_version :3.8 # ============================================================================== import matplotlib.pyplot as plt -from common.consts import SECONDS_PER_WEEK +from common.consts import SECONDS_PER_WEEK, SECONDS_PER_DAY, SECONDS_PER_HALF_DAY from common.system import System @@ -52,6 +52,33 @@ def mayavi_run(bodies, dt=SECONDS_PER_WEEK, mlab.show() +update = None + + +def ursina_run(bodies, dt=SECONDS_PER_HALF_DAY, position=(4000000, 800000000, 4000000)): + """ + + :param bodies: + :param dt: + :return: + """ + global update + + from simulators.ursina_simulator import UrsinaSimulator, UrsinaPlayer + body_sys = System(bodies) + simulator = UrsinaSimulator(body_sys) + + player = UrsinaPlayer(position, simulator.ursina_views) + + def callback_update(): + for ursina_view in simulator.ursina_views: + simulator.check_and_evolve() + ursina_view.update() + + update = callback_update + simulator.run(dt) + + def mpl_run(bodies, dt=SECONDS_PER_WEEK, gif_file_name=None, gif_max_frame=200): """ @@ -93,4 +120,5 @@ if __name__ == '__main__': Sun(size_scale=1.2e2), # 太阳放大 120 倍 Earth(size_scale=4e3, distance_scale=1), # 地球放大 4000 倍,距离保持不变 ] - mpl_run(bodies, SECONDS_PER_WEEK) + # mpl_run(bodies, SECONDS_PER_WEEK) + ursina_run(bodies, SECONDS_PER_WEEK) diff --git a/simulators/ursina_simulator.py b/simulators/ursina_simulator.py index 22a2024..0a6b9e5 100644 --- a/simulators/ursina_simulator.py +++ b/simulators/ursina_simulator.py @@ -124,7 +124,10 @@ class UrsinaSimulator(Simulator): """检查时间间隔是否已过""" now = datetime.datetime.now() elapsed_time = now - self.last_time - return elapsed_time >= self.interval + value = elapsed_time >= self.interval + if value: + self.last_time = now + return value def check_and_evolve(self): if self.check_elapsed_time(): @@ -133,8 +136,8 @@ class UrsinaSimulator(Simulator): def run(self, dt, **kwargs): self.evolve_dt = dt # 设定时间间隔为1秒 - self.interval = datetime.timedelta(seconds=1) - self.last_time = datetime.datetime.now() + self.interval = datetime.timedelta(seconds=0.01) + self.last_time = datetime.datetime.now() - datetime.timedelta(seconds=2) # EditorCamera() self.app.run() @@ -167,18 +170,18 @@ if __name__ == '__main__': # Earth(name='地球', mass=5.97237e24, init_position=[0, -349597870.700, 0], init_velocity=[15.50, 0, 0], # size_scale=4e3, texture="earth.png", distance_scale=1), # 地球放大 4000 倍,距离保持不变 # ] - # bodies = [ - # Sun(size_scale=0.8e2), # 太阳放大 80 倍 - # Mercury(size_scale=4e3, distance_scale=1.3), # 水星放大 4000 倍,距离放大 1.3 倍 - # Venus(size_scale=4e3, distance_scale=1.3), # 金星放大 4000 倍,距离放大 1.3 倍 - # Earth(size_scale=4e3, distance_scale=1.3), # 地球放大 4000 倍,距离放大 1.3 倍 - # Mars(size_scale=4e3, distance_scale=1.3), # 火星放大 4000 倍,距离放大 1.3 倍 - # Jupiter(size_scale=0.68e3, distance_scale=0.65), # 木星放大 680 倍,距离缩小到真实距离的 0.65 - # Saturn(size_scale=0.68e3, distance_scale=0.52), # 土星放大 680 倍,距离缩小到真实距离的 0.52 - # Uranus(size_scale=0.8e3, distance_scale=0.36), # 天王星放大 800 倍,距离缩小到真实距离的 0.36 - # Neptune(size_scale=1e3, distance_scale=0.27), # 海王星放大 1000 倍,距离缩小到真实距离的 0.27 - # Pluto(size_scale=10e3, distance_scale=0.23), # 冥王星放大 10000 倍,距离缩小到真实距离的 0.23(从太阳系的行星中排除) - # ] + bodies = [ + Sun(size_scale=0.8e2), # 太阳放大 80 倍 + Mercury(size_scale=4e3, distance_scale=1.3), # 水星放大 4000 倍,距离放大 1.3 倍 + Venus(size_scale=4e3, distance_scale=1.3), # 金星放大 4000 倍,距离放大 1.3 倍 + Earth(size_scale=4e3, distance_scale=1.3), # 地球放大 4000 倍,距离放大 1.3 倍 + Mars(size_scale=4e3, distance_scale=1.3), # 火星放大 4000 倍,距离放大 1.3 倍 + Jupiter(size_scale=0.68e3, distance_scale=0.65), # 木星放大 680 倍,距离缩小到真实距离的 0.65 + Saturn(size_scale=0.68e3, distance_scale=0.52), # 土星放大 680 倍,距离缩小到真实距离的 0.52 + Uranus(size_scale=0.8e3, distance_scale=0.36), # 天王星放大 800 倍,距离缩小到真实距离的 0.36 + Neptune(size_scale=1e3, distance_scale=0.27), # 海王星放大 1000 倍,距离缩小到真实距离的 0.27 + Pluto(size_scale=10e3, distance_scale=0.23), # 冥王星放大 10000 倍,距离缩小到真实距离的 0.23(从太阳系的行星中排除) + ] # bodies = [ # Saturn(size_scale=0.68e3, init_position=[0, 0, 0], # init_velocity=[0, 0, 0], distance_scale=0.52), # 土星放大 680 倍,距离缩小到真实距离的 0.52 @@ -199,4 +202,4 @@ if __name__ == '__main__': # player._update() - simulator.run(SECONDS_PER_HALF_DAY) + simulator.run(SECONDS_PER_DAY) diff --git a/simulators/views/ursina_view.py b/simulators/views/ursina_view.py index 971374b..fca4bf3 100644 --- a/simulators/views/ursina_view.py +++ b/simulators/views/ursina_view.py @@ -248,7 +248,7 @@ class Planet(Entity): self.y = pos[2] self.z = pos[0] - # self.rotation_y -= self.rotspeed + self.rotation_y -= self.rotspeed def input(self, key): if key == "enter": -- GitLab