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

Python超人-宇宙模拟器

上级 9d2b1121
# -*- coding:utf-8 -*-
# title :三体一向箔场景模拟
# description :三体一向箔场景模拟
# title :一向箔场景模拟
# description :一向箔场景模拟
# author :Python超人
# date :2023-10-27
# link :https://gitcode.net/pythoncr/
......@@ -23,7 +23,7 @@ from simulators.func import ext_fun_for_method
class OneWayFoilSim:
"""
三体一向箔场景模拟
一向箔场景模拟
"""
def __init__(self):
......@@ -50,37 +50,37 @@ class OneWayFoilSim:
]
# endregion
self.two_way_foil_dt_factor = 1
self.sun.two_way_foil_dt = 0.003 * self.two_way_foil_dt_factor
self.saturn.two_way_foil_dt = 0.005 * self.two_way_foil_dt_factor
self.uranus.two_way_foil_dt = 0.002 * self.two_way_foil_dt_factor
self.one_way_foil_dt_factor = 1
self.sun.one_way_foil_dt = 0.003 * self.one_way_foil_dt_factor
self.saturn.one_way_foil_dt = 0.005 * self.one_way_foil_dt_factor
self.uranus.one_way_foil_dt = 0.002 * self.one_way_foil_dt_factor
init_bodies_pos_vels(self.bodies)
def build_two_way_foil(self):
def build_one_way_foil(self):
"""
创建一向箔(一个原始的方形一向箔,一个不断扩展的圆形一向箔)
@return:
"""
# 原始的方形一向箔
self.two_way_foil = QuadObj(texture='two_way_foil.png',
self.one_way_foil = QuadObj(texture='two_way_foil.png',
# size_scale=4e7,
size_scale=1e7,
init_velocity=[0, -50, 60],
init_position=[0, 5 * AU, -6 * AU]) \
.set_light_disable(True).set_ignore_gravity(True)
# 不断扩展的圆形一向箔
self.two_way_foil_circle = CircleObj(texture="two_way_foil_circle.png",
size_scale=self.two_way_foil.size_scale * 2,
# 不断扩展的一向箔
self.one_way_foil_line = CircleObj(texture="two_way_foil_circle.png",
size_scale=self.one_way_foil.size_scale * 2,
) \
.set_light_disable(True).set_ignore_gravity(True)
self.bodies.append(self.two_way_foil)
self.bodies.append(self.two_way_foil_circle)
self.bodies.append(self.one_way_foil)
self.bodies.append(self.one_way_foil_line)
def build(self):
self.build_solar_system()
self.build_two_way_foil()
self.build_one_way_foil()
def on_ready(self):
"""
......@@ -93,29 +93,27 @@ class OneWayFoilSim:
create_sphere_sky(scale=200000)
application.time_scale = 5
# 圆形一向箔初始化(一开始不显示)
self.two_way_foil_circle.planet.rotation_x = 90
self.two_way_foil_circle.planet.enabled = False
self.one_way_foil_line.planet.rotation_x = 90
self.one_way_foil_line.planet.enabled = False
def change_two_way_foil():
# 长方形膜状物,长八点五厘米,宽五点二厘米,比一张信用卡略大一些,极薄,看不出任何厚度。
# 封装状态下晶莹剔透、无色透明。待机模式下由于封装力场逐渐蒸发,会发出白光,使其表面呈纯白色,看上去就是一张纸条
self.two_way_foil.planet.scale_x = 8.5
self.two_way_foil.planet.scale_y = 0.1
def change_one_way_foil():
self.one_way_foil.planet.scale_x = 8.5
self.one_way_foil.planet.scale_y = 0.1
ext_fun_for_method(self.two_way_foil.planet, after_run_fun=change_two_way_foil)
ext_fun_for_method(self.one_way_foil.planet, after_run_fun=change_one_way_foil)
def change_two_way_foil_circle():
if hasattr(self.two_way_foil_circle.planet, "start_scale"):
def change_one_way_foil_line():
if hasattr(self.one_way_foil_line.planet, "start_scale"):
# 长方形膜状物,长八点五厘米,宽五点二厘米,比一张信用卡略大一些,极薄,看不出任何厚度。
# 封装状态下晶莹剔透、无色透明。待机模式下由于封装力场逐渐蒸发,会发出白光,使其表面呈纯白色,看上去就是一张纸条
self.two_way_foil_circle.planet.start_scale += 0.3
self.two_way_foil_circle.planet.scale_x = self.two_way_foil_circle.planet.start_scale
self.two_way_foil_circle.planet.scale_y = 1
self.two_way_foil_circle.planet.scale_z = 1
self.one_way_foil_line.planet.start_scale += 0.3
self.one_way_foil_line.planet.scale_x = self.one_way_foil_line.planet.start_scale
self.one_way_foil_line.planet.scale_y = 3
self.one_way_foil_line.planet.scale_z = 3
# 'box'/'sphere'/'capsule'/'mesh'
self.two_way_foil_circle.planet.collider = "sphere"
ext_fun_for_method(self.two_way_foil_circle.planet, after_run_fun=change_two_way_foil_circle)
self.one_way_foil_line.planet.collider = "sphere"
ext_fun_for_method(self.one_way_foil_line.planet, after_run_fun=change_one_way_foil_line)
def gen_pixcel_image(self, planet):
"""
......@@ -142,7 +140,7 @@ class OneWayFoilSim:
def flatten_animation(self, body):
"""
天体维化的动画
天体一维维化的动画
@param body: 天体
@return:
"""
......@@ -155,11 +153,11 @@ class OneWayFoilSim:
# 对Y轴进行压平动画,如果压平大小不足 1/50,则继续压缩,直到压缩到 1/50 就不压缩了
# (如果觉得 1/50 的厚度压的不够,还可以继续压缩,基本上就够了)
# if planet.scale_y_v > planet.init_scale_y / 50:
if planet.scale_y_v > 0.5:
if planet.scale_y_v > 1:
planet.scale_y_v /= 1.01
else:
self.gen_pixcel_image(planet)
planet.scale_x_v += 0.05
planet.scale_x_v += 0.1
# 灯光关闭,不然压到2纬就会是黑色
planet.set_light_off(True)
......@@ -169,9 +167,9 @@ class OneWayFoilSim:
return warp
body.is_2d = True
# 压平时,转速将为以前的 1/200
body.planet.rotation_speed /= 200
body.is_1d = True
# 压平时,转速将为以前的 1/150
body.planet.rotation_speed /= 150
# 记录原始的厚度大小
body.planet.scale_y_v = body.planet.scale_y
body.planet.init_scale_y = body.planet.scale_y
......@@ -185,7 +183,7 @@ class OneWayFoilSim:
def get_target(self):
for body in self.bodies:
if hasattr(body, "is_2d"):
if hasattr(body, "is_1d"):
continue
return body
return None
......@@ -197,12 +195,12 @@ class OneWayFoilSim:
"""
# 如果一向箔和太阳碰撞
if two_bodies_colliding(self.two_way_foil, self.sun):
if two_bodies_colliding(self.one_way_foil, self.sun):
# 隐藏原始一向箔,保持在原地,不在飞行
# self.two_way_foil.planet.enabled = False
self.two_way_foil.init_velocity = [0, 0, 0]
# self.one_way_foil.planet.enabled = False
self.one_way_foil.init_velocity = [0, 0, 0]
def two_way_foil_update():
def one_way_foil_update():
target = self.get_target()
if target is self.sun:
......@@ -210,26 +208,26 @@ class OneWayFoilSim:
self.sun.hide_children()
# 让实体A朝向实体B
# self.two_way_foil.planet.look_at(target)
# self.one_way_foil.planet.look_at(target)
if target is not None:
# 让实体A向着实体B移动
dt = target.two_way_foil_dt if hasattr(target, "two_way_foil_dt") \
else 0.003 * self.two_way_foil_dt_factor
dt = target.one_way_foil_dt if hasattr(target, "one_way_foil_dt") \
else 0.003 * self.one_way_foil_dt_factor
# current_time = time.time()
# if current_time - self.two_way_foil.last_time >= 0.1:
# if current_time - self.one_way_foil.last_time >= 0.1:
# dt = dt * 2
# self.two_way_foil.last_time = current_time
# target.two_way_foil_dt = dt
self.two_way_foil.planet.position = \
lerp(self.two_way_foil.planet.position, target.planet.position, dt)
# self.one_way_foil.last_time = current_time
# target.one_way_foil_dt = dt
self.one_way_foil.planet.position = \
lerp(self.one_way_foil.planet.position, target.planet.position, dt)
else:
self.two_way_foil.planet.enabled = False
self.one_way_foil.planet.enabled = False
self.two_way_foil.planet.update = two_way_foil_update
self.one_way_foil.planet.update = one_way_foil_update
# # 圆形一向箔显示并设置透明度为0.8
self.two_way_foil_circle.planet.alpha = 0.8
self.two_way_foil_circle.planet.enabled = True
self.two_way_foil_circle.planet.start_scale = 1
self.one_way_foil_line.planet.alpha = 0.8
self.one_way_foil_line.planet.enabled = True
self.one_way_foil_line.planet.start_scale = 1
# 当前阶段为 stage_02:一向箔压平天体的阶段
self.current_stage = self.stage_02
......@@ -239,12 +237,12 @@ class OneWayFoilSim:
一向箔压平天体(二维化)的阶段
@return:
"""
self.two_way_foil.planet.enabled = False
self.one_way_foil.planet.enabled = False
# self.sun.two_dim.planet.init_scale += 0.05
# 圆形一向箔不断扩展变大
self.two_way_foil.planet.init_scale += 8
self.one_way_foil.planet.init_scale += 8
# 调整天体二维化的时间,一般需要延时,保证扩展和二维化同步的真实效果
two_way_delay_times = [0.5, # 太阳
one_way_delay_times = [0.5, # 太阳
0.5, 0.8, 1.0, # 水星 金星 地球
1.2, 2.0, 3.0, # 火星 木星 土星
4.0, 4.5, 5.5] # 天王星 海王星 冥王星
......@@ -252,20 +250,20 @@ class OneWayFoilSim:
if isinstance(b, Obj):
# 一向箔不处理
continue
if hasattr(b, "two_way_time"):
if hasattr(b, "one_way_time"):
# 一向箔和天体碰撞的时间不为空,则说明已经碰撞
if b.two_way_time is not None:
if b.one_way_time is not None:
# 如果碰撞后的延时时间到,则进行压平天体处理(二维化)
if time.time() - b.two_way_time > two_way_delay_times[idx]:
if time.time() - b.one_way_time > one_way_delay_times[idx]:
self.flatten_animation(b)
# 一向箔和天体碰撞的时间设置为空,就是说明二维化结束
b.two_way_time = None
elif two_bodies_colliding(self.two_way_foil_circle, b):
b.one_way_time = None
elif two_bodies_colliding(self.one_way_foil_line, b):
# 一向箔和天体碰撞,但暂时先不二维化,记下时间,延时二维化
b.two_way_time = time.time()
b.one_way_time = time.time()
# 圆形一向箔不断旋转的效果
self.two_way_foil_circle.planet.rotation_z += 4
self.one_way_foil_line.planet.rotation_z += 4
def on_timer_changed(self, time_data):
"""
......@@ -273,21 +271,21 @@ class OneWayFoilSim:
@param time_data:
@return:
"""
# 原始方形一向箔飞行的翻转效果
if self.two_way_foil.planet.enabled:
self.two_way_foil.planet.rotation_x += 0.1
self.two_way_foil.planet.rotation_y += 1
# 原始一向箔飞行的翻转效果
if self.one_way_foil.planet.enabled:
self.one_way_foil.planet.rotation_x += 0.1
self.one_way_foil.planet.rotation_y += 1
# # 摄像机始终看向一向箔
# camera_look_at(self.two_way_foil)
# camera_look_at(self.one_way_foil)
target = self.get_target()
if target is not None:
if target is self.sun:
# 摄像机始终看向一向箔
camera_look_at(self.two_way_foil)
camera_look_at(self.one_way_foil)
else:
# 摄像机始终看向一向箔
camera_look_at(self.two_way_foil, rotation_z=0)
camera_look_at(self.one_way_foil, rotation_z=0)
camera.position = \
lerp(camera.position, target.planet.position, 0.001)
......@@ -296,7 +294,7 @@ class OneWayFoilSim:
if __name__ == '__main__':
"""
三体一向箔场景模拟
一向箔场景模拟
"""
sim = OneWayFoilSim()
sim.build()
......@@ -313,7 +311,7 @@ if __name__ == '__main__':
# position=(0, 2 * AU, -11 * AU),
position=(0, 5 * AU, -5 * AU),
cosmic_bg='',
bg_music='sounds/no_glory.mp3',
# bg_music='sounds/no_glory.mp3',
show_camera_info=False,
show_control_info=False,
timer_enabled=True,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册