From 09817f55657af011e6b257cc3b3181249eb09f33 Mon Sep 17 00:00:00 2001 From: march3 Date: Thu, 16 Mar 2023 13:44:40 +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 --- bodies/asteroids.py | 7 ++++-- bodies/body.py | 3 ++- scenes/solar_system_2.py | 5 ++-- simulators/simulator.py | 6 ++--- simulators/views/body_view.py | 3 ++- simulators/views/ursina_view.py | 42 ++++++++++++++++++++++++--------- 6 files changed, 46 insertions(+), 20 deletions(-) diff --git a/bodies/asteroids.py b/bodies/asteroids.py index a5be5d9..6410df1 100644 --- a/bodies/asteroids.py +++ b/bodies/asteroids.py @@ -29,7 +29,9 @@ class Asteroids(Body): def __init__(self, name="Asteroids", mass=1.9891e30, init_position=[0, 0, 0], init_velocity=[0, 0, 0], - texture="asteroids.png", size_scale=1.0, distance_scale=1.0,rotation_speed=0.6130): + texture="asteroids.png", size_scale=1.0, + distance_scale=1.0, rotation_speed=0.6130, + parent=None): params = { "name": name, "mass": mass, @@ -40,7 +42,8 @@ class Asteroids(Body): "texture": texture, "size_scale": size_scale, "distance_scale": distance_scale, - "rotation_speed": rotation_speed + "rotation_speed": rotation_speed, + "parent": parent } super().__init__(**params) # 环状星群 diff --git a/bodies/body.py b/bodies/body.py index 51cc5bb..27ee938 100644 --- a/bodies/body.py +++ b/bodies/body.py @@ -21,7 +21,7 @@ class Body(metaclass=ABCMeta): def __init__(self, name, mass, init_position, init_velocity, density=5e3, color=(125 / 255, 125 / 255, 125 / 255), texture=None, size_scale=1.0, distance_scale=1.0, - rotation_speed=None): + rotation_speed=None, parent=None): """ 天体类 :param name: 天体名称 @@ -70,6 +70,7 @@ class Body(metaclass=ABCMeta): # 是否显示 self.appeared = True + self.parent = parent @property def has_rings(self): diff --git a/scenes/solar_system_2.py b/scenes/solar_system_2.py index cfaebeb..1a835d5 100644 --- a/scenes/solar_system_2.py +++ b/scenes/solar_system_2.py @@ -22,12 +22,13 @@ if __name__ == '__main__': # 2、为了达到最佳的显示效果,对每个行星天体的距离进行了缩放 # region 构建太阳系 + sun = Sun(size_scale=0.8e2) bodies = [ - Sun(size_scale=0.8e2), # 太阳放大 80 倍 + sun, # 太阳放大 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 倍 - Asteroids(size_scale=3e2), # 小行星模拟 + Asteroids(size_scale=3e2, parent=sun), # 小行星模拟 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 diff --git a/simulators/simulator.py b/simulators/simulator.py index e29f326..bc879ae 100644 --- a/simulators/simulator.py +++ b/simulators/simulator.py @@ -32,7 +32,7 @@ class Simulator(metaclass=ABCMeta): :return: """ for body in self.bodies_sys.bodies: - view = viewer_type(body) + view = viewer_type(body, self.bodies_sys) self.body_views.append(view) def evolve(self, dt: int): @@ -64,8 +64,8 @@ class Simulator(metaclass=ABCMeta): view.update() - self.bodies_sys.bodies = list(filter(lambda b:b.appeared, self.bodies_sys.bodies)) - self.body_views = list(filter(lambda b:b.appeared, self.body_views)) + self.bodies_sys.bodies = list(filter(lambda b: b.appeared, self.bodies_sys.bodies)) + self.body_views = list(filter(lambda b: b.appeared, self.body_views)) @abstractmethod def run(self, dt: int, **kwargs): diff --git a/simulators/views/body_view.py b/simulators/views/body_view.py index 0140e21..3082c57 100644 --- a/simulators/views/body_view.py +++ b/simulators/views/body_view.py @@ -18,8 +18,9 @@ class BodyView(metaclass=ABCMeta): 天体视图(天体效果展示用) """ - def __init__(self, body: Body): + def __init__(self, body: Body, bodies_system): self.body = body + self.bodies_system = bodies_system self.sphere = None if self.body.texture is None or self.body.texture == '': self.color = tuple(np.array(body.color) / 255) diff --git a/simulators/views/ursina_view.py b/simulators/views/ursina_view.py index dcaa4e2..c481970 100644 --- a/simulators/views/ursina_view.py +++ b/simulators/views/ursina_view.py @@ -153,7 +153,7 @@ class Planet(Entity): try: pos = self.position except Exception as e: - self.destroy_me() + self.destroy_all() return trails_keys = self.trails.keys() # 如果有拖尾 @@ -193,10 +193,12 @@ class Planet(Entity): def turn(self): pos = self.body_view.position * UrsinaConfig.SCALE_FACTOR - - self.x = -pos[1] - self.y = pos[2] - self.z = pos[0] + if self.body_view.body.parent is None: + self.x = -pos[1] + self.y = pos[2] + self.z = pos[0] + else: + self.follow_parent() dt = 0 if hasattr(self.body_view.body, "dt"): @@ -217,7 +219,7 @@ class Planet(Entity): # 天体旋转 self.rotation_y -= self.rotspeed except Exception as e: - self.destroy_me() + self.destroy_all() return # 如果有行星环 @@ -231,6 +233,18 @@ class Planet(Entity): if len(self.body_view.body.his_position()) > 1: self.create_trails() + + def follow_parent(self): + if not hasattr(self.body_view, "bodies_system"): + return + sys = self.body_view.bodies_system + for b in sys.bodies: + if self.body_view.body.parent == b: + pos = b.position * UrsinaConfig.SCALE_FACTOR + self.x = -pos[1] + self.y = pos[2] + self.z = pos[0] + def create_rings(self): """ 创建行星环(使用土星贴图) @@ -252,11 +266,17 @@ class Planet(Entity): # 设置行星环不受灯光影响,否则看不清行星环 self.ring.set_light_off() - def destroy_me(self): + def destroy_all(self): + # 从天体系统中移除自己(TODO:暂时还不能移除) + # self.body_view.bodies_system.bodies.remove(self.body_view.body) + + # 删除拖尾 for entity, pos in self.trails.items(): destroy(entity) + # 如果有行星环,则删除行星环 if hasattr(self, "ring"): destroy(self.ring) + # 最后删除自己 destroy(self) @@ -265,8 +285,8 @@ class UrsinaView(BodyView): ursina天体视图(天体效果展示用) """ - def __init__(self, body: Body): - BodyView.__init__(self, body) + def __init__(self, body: Body, bodies_system): + BodyView.__init__(self, body, bodies_system) self.velocity = body.velocity self.planet = Planet(self) @@ -281,9 +301,9 @@ class UrsinaView(BodyView): """ self.planet.turn() - def appear(self): pass def disappear(self): - self.planet.destroy_me() + self.planet.destroy_all() + self.appeared = False -- GitLab