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

太阳系三体模拟器

上级 e9e2c9d3
......@@ -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)
# 环状星群
......
......@@ -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):
......
......@@ -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
......
......@@ -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):
......
......@@ -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)
......
......@@ -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
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册