提交 09817f55 编写于 作者: 三月三net's avatar 三月三net

太阳系三体模拟器

上级 e9e2c9d3
...@@ -29,7 +29,9 @@ class Asteroids(Body): ...@@ -29,7 +29,9 @@ class Asteroids(Body):
def __init__(self, name="Asteroids", mass=1.9891e30, def __init__(self, name="Asteroids", mass=1.9891e30,
init_position=[0, 0, 0], init_position=[0, 0, 0],
init_velocity=[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 = { params = {
"name": name, "name": name,
"mass": mass, "mass": mass,
...@@ -40,7 +42,8 @@ class Asteroids(Body): ...@@ -40,7 +42,8 @@ class Asteroids(Body):
"texture": texture, "texture": texture,
"size_scale": size_scale, "size_scale": size_scale,
"distance_scale": distance_scale, "distance_scale": distance_scale,
"rotation_speed": rotation_speed "rotation_speed": rotation_speed,
"parent": parent
} }
super().__init__(**params) super().__init__(**params)
# 环状星群 # 环状星群
......
...@@ -21,7 +21,7 @@ class Body(metaclass=ABCMeta): ...@@ -21,7 +21,7 @@ class Body(metaclass=ABCMeta):
def __init__(self, name, mass, init_position, init_velocity, def __init__(self, name, mass, init_position, init_velocity,
density=5e3, color=(125 / 255, 125 / 255, 125 / 255), density=5e3, color=(125 / 255, 125 / 255, 125 / 255),
texture=None, size_scale=1.0, distance_scale=1.0, texture=None, size_scale=1.0, distance_scale=1.0,
rotation_speed=None): rotation_speed=None, parent=None):
""" """
天体类 天体类
:param name: 天体名称 :param name: 天体名称
...@@ -70,6 +70,7 @@ class Body(metaclass=ABCMeta): ...@@ -70,6 +70,7 @@ class Body(metaclass=ABCMeta):
# 是否显示 # 是否显示
self.appeared = True self.appeared = True
self.parent = parent
@property @property
def has_rings(self): def has_rings(self):
......
...@@ -22,12 +22,13 @@ if __name__ == '__main__': ...@@ -22,12 +22,13 @@ if __name__ == '__main__':
# 2、为了达到最佳的显示效果,对每个行星天体的距离进行了缩放 # 2、为了达到最佳的显示效果,对每个行星天体的距离进行了缩放
# region 构建太阳系 # region 构建太阳系
sun = Sun(size_scale=0.8e2)
bodies = [ bodies = [
Sun(size_scale=0.8e2), # 太阳放大 80 倍 sun, # 太阳放大 80 倍
Mercury(size_scale=4e3, distance_scale=1.3), # 水星放大 4000 倍,距离放大 1.3 倍 Mercury(size_scale=4e3, distance_scale=1.3), # 水星放大 4000 倍,距离放大 1.3 倍
Venus(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 倍 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 倍 Mars(size_scale=4e3, distance_scale=1.3), # 火星放大 4000 倍,距离放大 1.3 倍
Jupiter(size_scale=0.68e3, distance_scale=0.65), # 木星放大 680 倍,距离缩小到真实距离的 0.65 Jupiter(size_scale=0.68e3, distance_scale=0.65), # 木星放大 680 倍,距离缩小到真实距离的 0.65
Saturn(size_scale=0.68e3, distance_scale=0.52), # 土星放大 680 倍,距离缩小到真实距离的 0.52 Saturn(size_scale=0.68e3, distance_scale=0.52), # 土星放大 680 倍,距离缩小到真实距离的 0.52
......
...@@ -32,7 +32,7 @@ class Simulator(metaclass=ABCMeta): ...@@ -32,7 +32,7 @@ class Simulator(metaclass=ABCMeta):
:return: :return:
""" """
for body in self.bodies_sys.bodies: for body in self.bodies_sys.bodies:
view = viewer_type(body) view = viewer_type(body, self.bodies_sys)
self.body_views.append(view) self.body_views.append(view)
def evolve(self, dt: int): def evolve(self, dt: int):
...@@ -64,8 +64,8 @@ class Simulator(metaclass=ABCMeta): ...@@ -64,8 +64,8 @@ class Simulator(metaclass=ABCMeta):
view.update() view.update()
self.bodies_sys.bodies = list(filter(lambda b:b.appeared, self.bodies_sys.bodies)) 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.body_views = list(filter(lambda b: b.appeared, self.body_views))
@abstractmethod @abstractmethod
def run(self, dt: int, **kwargs): def run(self, dt: int, **kwargs):
......
...@@ -18,8 +18,9 @@ class BodyView(metaclass=ABCMeta): ...@@ -18,8 +18,9 @@ class BodyView(metaclass=ABCMeta):
天体视图(天体效果展示用) 天体视图(天体效果展示用)
""" """
def __init__(self, body: Body): def __init__(self, body: Body, bodies_system):
self.body = body self.body = body
self.bodies_system = bodies_system
self.sphere = None self.sphere = None
if self.body.texture is None or self.body.texture == '': if self.body.texture is None or self.body.texture == '':
self.color = tuple(np.array(body.color) / 255) self.color = tuple(np.array(body.color) / 255)
......
...@@ -153,7 +153,7 @@ class Planet(Entity): ...@@ -153,7 +153,7 @@ class Planet(Entity):
try: try:
pos = self.position pos = self.position
except Exception as e: except Exception as e:
self.destroy_me() self.destroy_all()
return return
trails_keys = self.trails.keys() trails_keys = self.trails.keys()
# 如果有拖尾 # 如果有拖尾
...@@ -193,10 +193,12 @@ class Planet(Entity): ...@@ -193,10 +193,12 @@ class Planet(Entity):
def turn(self): def turn(self):
pos = self.body_view.position * UrsinaConfig.SCALE_FACTOR pos = self.body_view.position * UrsinaConfig.SCALE_FACTOR
if self.body_view.body.parent is None:
self.x = -pos[1] self.x = -pos[1]
self.y = pos[2] self.y = pos[2]
self.z = pos[0] self.z = pos[0]
else:
self.follow_parent()
dt = 0 dt = 0
if hasattr(self.body_view.body, "dt"): if hasattr(self.body_view.body, "dt"):
...@@ -217,7 +219,7 @@ class Planet(Entity): ...@@ -217,7 +219,7 @@ class Planet(Entity):
# 天体旋转 # 天体旋转
self.rotation_y -= self.rotspeed self.rotation_y -= self.rotspeed
except Exception as e: except Exception as e:
self.destroy_me() self.destroy_all()
return return
# 如果有行星环 # 如果有行星环
...@@ -231,6 +233,18 @@ class Planet(Entity): ...@@ -231,6 +233,18 @@ class Planet(Entity):
if len(self.body_view.body.his_position()) > 1: if len(self.body_view.body.his_position()) > 1:
self.create_trails() 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): def create_rings(self):
""" """
创建行星环(使用土星贴图) 创建行星环(使用土星贴图)
...@@ -252,11 +266,17 @@ class Planet(Entity): ...@@ -252,11 +266,17 @@ class Planet(Entity):
# 设置行星环不受灯光影响,否则看不清行星环 # 设置行星环不受灯光影响,否则看不清行星环
self.ring.set_light_off() 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(): for entity, pos in self.trails.items():
destroy(entity) destroy(entity)
# 如果有行星环,则删除行星环
if hasattr(self, "ring"): if hasattr(self, "ring"):
destroy(self.ring) destroy(self.ring)
# 最后删除自己
destroy(self) destroy(self)
...@@ -265,8 +285,8 @@ class UrsinaView(BodyView): ...@@ -265,8 +285,8 @@ class UrsinaView(BodyView):
ursina天体视图(天体效果展示用) ursina天体视图(天体效果展示用)
""" """
def __init__(self, body: Body): def __init__(self, body: Body, bodies_system):
BodyView.__init__(self, body) BodyView.__init__(self, body, bodies_system)
self.velocity = body.velocity self.velocity = body.velocity
self.planet = Planet(self) self.planet = Planet(self)
...@@ -281,9 +301,9 @@ class UrsinaView(BodyView): ...@@ -281,9 +301,9 @@ class UrsinaView(BodyView):
""" """
self.planet.turn() self.planet.turn()
def appear(self): def appear(self):
pass pass
def disappear(self): def disappear(self):
self.planet.destroy_me() self.planet.destroy_all()
self.appeared = False
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册