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

Python超人-宇宙模拟器

上级 836d4ba9
...@@ -66,7 +66,10 @@ if __name__ == '__main__': ...@@ -66,7 +66,10 @@ if __name__ == '__main__':
pass pass
# 订阅事件后,上面2个函数功能才会起作用
# 运行中,每时每刻都会触发 on_timer_changed
UrsinaEvent.on_timer_changed_subscription(on_timer_changed) UrsinaEvent.on_timer_changed_subscription(on_timer_changed)
# 运行前会触发 on_ready
UrsinaEvent.on_ready_subscription(on_ready) UrsinaEvent.on_ready_subscription(on_ready)
# 使用 ursina 查看的运行效果 # 使用 ursina 查看的运行效果
......
...@@ -51,7 +51,10 @@ if __name__ == '__main__': ...@@ -51,7 +51,10 @@ if __name__ == '__main__':
info["satellite"].planet.look_at(earth.planet) info["satellite"].planet.look_at(earth.planet)
# 订阅事件后,上面2个函数功能才会起作用
# 运行前会触发 on_ready
UrsinaEvent.on_ready_subscription(on_ready) UrsinaEvent.on_ready_subscription(on_ready)
# 运行中,每时每刻都会触发 on_timer_changed
UrsinaEvent.on_timer_changed_subscription(on_timer_changed) UrsinaEvent.on_timer_changed_subscription(on_timer_changed)
# 使用 ursina 查看的运行效果 # 使用 ursina 查看的运行效果
......
...@@ -44,7 +44,10 @@ if __name__ == '__main__': ...@@ -44,7 +44,10 @@ if __name__ == '__main__':
create_line(from_pos=jupiter.planet.position, to_pos=dancer.planet.main_entity.position, color=color.white) create_line(from_pos=jupiter.planet.position, to_pos=dancer.planet.main_entity.position, color=color.white)
# 订阅事件后,上面2个函数功能才会起作用
# 运行前会触发 on_ready
UrsinaEvent.on_ready_subscription(on_ready) UrsinaEvent.on_ready_subscription(on_ready)
# 运行中,每时每刻都会触发 on_timer_changed
UrsinaEvent.on_timer_changed_subscription(on_timer_changed) UrsinaEvent.on_timer_changed_subscription(on_timer_changed)
# 使用 ursina 查看的运行效果 # 使用 ursina 查看的运行效果
......
...@@ -96,7 +96,10 @@ if __name__ == '__main__': ...@@ -96,7 +96,10 @@ if __name__ == '__main__':
# print(camera.rotation_y) # print(camera.rotation_y)
# 订阅事件后,上面2个函数功能才会起作用
# 运行前会触发 on_ready
UrsinaEvent.on_ready_subscription(on_ready) UrsinaEvent.on_ready_subscription(on_ready)
# 运行中,每时每刻都会触发 on_timer_changed
UrsinaEvent.on_timer_changed_subscription(on_timer_changed) UrsinaEvent.on_timer_changed_subscription(on_timer_changed)
# 使用 ursina 查看的运行效果 # 使用 ursina 查看的运行效果
......
...@@ -16,18 +16,19 @@ from simulators.ursina.ursina_event import UrsinaEvent ...@@ -16,18 +16,19 @@ from simulators.ursina.ursina_event import UrsinaEvent
def create_ejected_object(velocity, raduis, trail_color, gravity_only_for, angle=10): def create_ejected_object(velocity, raduis, trail_color, gravity_only_for, angle=10):
""" """
创建一个被抛的物体 创建一个抛出物体
@param velocity: 抛出去的速度 @param velocity: 抛出去的速度
@param raduis: 物体地球中心的半径 @param raduis: 地球中心的半径
@param trail_color: 轨迹颜色 @param trail_color: 轨迹颜色
@param gravity_only_for: 指定一个天体,被抛的物体的引力只对该天体有效
@param angle: 抛出去的角度(地平线夹角,默认为10) @param angle: 抛出去的角度(地平线夹角,默认为10)
@return: @return:
""" """
# 根据速度、角度获取矢量速度(vx、vy) -> vx² + vy² = velocity² # 根据速度、角度获取矢量速度(vx、vy) -> vx² + vy² = velocity²
vx, vy = get_vector2d_velocity(velocity, angle=angle) vx, vy = get_vector2d_velocity(velocity, angle=angle)
football = Football(name=f'物体速度:{velocity}', mass=500, size_scale=1e3, trail_color=trail_color, football = Football(name=f'物体速度:{velocity}', mass=500, size_scale=1e3,
init_position=[0, raduis, 0], trail_color=trail_color, init_position=[0, raduis + 500, 0],
init_velocity=[vx, vy, 0], gravity_only_for=[gravity_only_for]) # 仅适用于地球的重力,物体之间重力不要受到影响 init_velocity=[vx, vy, 0], gravity_only_for=[gravity_only_for])
return football return football
...@@ -38,43 +39,47 @@ if __name__ == '__main__': ...@@ -38,43 +39,47 @@ if __name__ == '__main__':
# 地球在中心位置 # 地球在中心位置
earth = Earth(init_position=[0, 0, 0], init_velocity=[0, 0, 0], earth = Earth(init_position=[0, 0, 0], init_velocity=[0, 0, 0],
size_scale=1, rotation_speed=0, texture="earth_hd.jpg") size_scale=1, rotation_speed=0, texture="earth_hd.jpg")
raduis = earth.raduis + 300 raduis = earth.raduis
# TODO: 4个不同的抛出速度 7.5km/s、8.5km/s、10km/s、11.2km/s(第二宇宙速度) # TODO: 创建4个不同的抛出速度的物体,速度分别为:7.5km/s、8.5km/s、10km/s、11.2km/s(第二宇宙速度)
# 粉色:velocity = 7.5,飞不出地球太远,就落地 # 粉色拖尾:velocity = 7.5,飞不出地球太远,就落地(仅适用于地球的重力,物体之间重力不要受到影响)
obj0 = create_ejected_object(velocity=7.5, raduis=raduis, trail_color=(255, 0, 255), gravity_only_for=earth) obj0 = create_ejected_object(velocity=7.5, raduis=raduis, trail_color=(255, 0, 255), gravity_only_for=earth)
# 红色:velocity = 8.5,飞不出地球太远,就落地 # 红色拖尾:velocity = 8.5,飞不出地球太远,就落地
obj1 = create_ejected_object(velocity=8.5, raduis=raduis, trail_color=(255, 0, 0), gravity_only_for=earth) obj1 = create_ejected_object(velocity=8.5, raduis=raduis, trail_color=(255, 0, 0), gravity_only_for=earth)
# 绿色:velocity = 10,能飞出地球很远,但还是无法摆脱地球引力 # 绿色拖尾:velocity = 10,能飞出地球很远,但还是无法摆脱地球引力
obj2 = create_ejected_object(velocity=10, raduis=raduis, trail_color=(0, 255, 0), gravity_only_for=earth) obj2 = create_ejected_object(velocity=10, raduis=raduis, trail_color=(0, 255, 0), gravity_only_for=earth)
# 蓝色:velocity = 11.2,脱离地球引力直接飞出。速度11.2千米/秒为脱离地球引力的速度叫第二宇宙速度 # 蓝色拖尾:velocity = 11.2,脱离地球引力直接飞出。速度11.2千米/秒为脱离地球引力的速度叫第二宇宙速度
obj3 = create_ejected_object(velocity=11.2, raduis=raduis, trail_color=(0, 0, 255), gravity_only_for=earth) obj3 = create_ejected_object(velocity=11.2, raduis=raduis, trail_color=(0, 0, 255), gravity_only_for=earth)
bodies = [earth, obj0, obj1, obj2, obj3] bodies = [earth, obj0, obj1, obj2, obj3]
def on_timer_changed(time_data: TimeData): def on_reset():
""" """
定时触发,在10分钟后,进行碰撞判断,如果抛出物与地球相碰撞了,则抛出物体静止不动 当按键盘的 “O” 键重置后,恢复所有抛出物的状态(引力有效),这样就可以反复观看
@param time_data:
@return: @return:
""" """
if time_data.total_minutes > 10: for obj in [obj0, obj1, obj2, obj3]:
# 抛出物体10分钟后,再进行碰撞判断 obj.ignore_mass = False
for obj in [obj0, obj1, obj2, obj3]:
# 循环判断每个抛出物与地球是否相碰撞
if two_bodies_colliding(obj, earth):
# 如果抛出物与地球相碰撞了,则静止不动(抛出物停止并忽略引力)
obj.stop_and_ignore_gravity()
def on_reset(): def on_timer_changed(time_data: TimeData):
"""
在运行中,每时每刻都会触发,对抛出物与地球的碰撞进行判断,如果碰到地球,则停止运动
@param time_data:
@return:
"""
for obj in [obj0, obj1, obj2, obj3]: for obj in [obj0, obj1, obj2, obj3]:
obj.ignore_mass = False # 循环判断每个抛出物与地球是否相碰撞
if two_bodies_colliding(obj, earth):
# 如果抛出物与地球相碰撞了,则静止不动(抛出物停止并忽略引力)
obj.stop_and_ignore_gravity()
# 订阅计时器事件(定时触发) # 订阅事件后,上面2个函数功能才会起作用
UrsinaEvent.on_timer_changed_subscription(on_timer_changed) # 按键盘的 “O” 重置键会触发 on_reset
UrsinaEvent.on_reset_subscription(on_reset) UrsinaEvent.on_reset_subscription(on_reset)
# 运行中,每时每刻都会触发 on_timer_changed
UrsinaEvent.on_timer_changed_subscription(on_timer_changed)
# 使用 ursina 查看的运行效果 # 使用 ursina 查看的运行效果
# 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹
......
...@@ -94,7 +94,7 @@ class SpeedOfLightInit: ...@@ -94,7 +94,7 @@ class SpeedOfLightInit:
def on_reset(self): def on_reset(self):
""" """
点击了重置按钮触发 当按键盘的 “O” 键重置后会触发
@return: @return:
""" """
if hasattr(self, "run_speed_maps"): if hasattr(self, "run_speed_maps"):
...@@ -117,9 +117,12 @@ class SpeedOfLightInit: ...@@ -117,9 +117,12 @@ class SpeedOfLightInit:
raise Exception("请指定 SpeedOfLightInit.bodies") raise Exception("请指定 SpeedOfLightInit.bodies")
# 订阅重新开始事件 # 订阅重新开始事件
# 按键盘的 “O” 重置键会触发 on_reset
UrsinaEvent.on_reset_subscription(self.on_reset) UrsinaEvent.on_reset_subscription(self.on_reset)
# 运行前会触发 on_ready
UrsinaEvent.on_ready_subscription(self.on_ready) UrsinaEvent.on_ready_subscription(self.on_ready)
# 订阅计时器事件(记录已到达天体列表) # 订阅计时器事件(记录已到达天体列表)
# 运行中,每时每刻都会触发 on_timer_changed
UrsinaEvent.on_timer_changed_subscription(self.on_timer_changed) UrsinaEvent.on_timer_changed_subscription(self.on_timer_changed)
def light_body_input(self, key): def light_body_input(self, key):
......
...@@ -37,6 +37,8 @@ if __name__ == '__main__': ...@@ -37,6 +37,8 @@ if __name__ == '__main__':
camera_look_at(earth) camera_look_at(earth)
# 订阅事件后,上面的函数功能才会起作用
# 运行前会触发 on_ready
UrsinaEvent.on_ready_subscription(on_ready) UrsinaEvent.on_ready_subscription(on_ready)
# 使用 ursina 查看的运行效果 # 使用 ursina 查看的运行效果
......
...@@ -31,6 +31,8 @@ if __name__ == '__main__': ...@@ -31,6 +31,8 @@ if __name__ == '__main__':
pass pass
# 订阅事件后,上面的函数功能才会起作用
# 运行前会触发 on_ready
UrsinaEvent.on_ready_subscription(on_ready) UrsinaEvent.on_ready_subscription(on_ready)
# 使用 ursina 查看的运行效果 # 使用 ursina 查看的运行效果
......
...@@ -65,7 +65,10 @@ if __name__ == '__main__': ...@@ -65,7 +65,10 @@ if __name__ == '__main__':
def on_reset(): def on_reset():
# 重置后恢复所有石头的状态(石头可见、引力有效) """
当按键盘的 “O” 键重置后,恢复所有石头的状态(石头可见、引力有效),这样就可以反复观看
@return:
"""
for comet in comets: for comet in comets:
comet.set_visible(True) comet.set_visible(True)
comet.ignore_mass = False comet.ignore_mass = False
...@@ -91,10 +94,13 @@ if __name__ == '__main__': ...@@ -91,10 +94,13 @@ if __name__ == '__main__':
comet.explode(jupiter) comet.explode(jupiter)
# 订阅事件 # 订阅事件后,上面3个函数功能才会起作用
UrsinaEvent.on_reset_subscription(on_reset) # 重置了会触发 on_reset # 按键盘的 “O” 重置键会触发 on_reset
UrsinaEvent.on_ready_subscription(on_ready) # 运行前会触发 on_ready UrsinaEvent.on_reset_subscription(on_reset)
UrsinaEvent.on_timer_changed_subscription(on_timer_changed) # 运行中,每时每刻都会触发 on_timer_changed # 运行前会触发 on_ready
UrsinaEvent.on_ready_subscription(on_ready)
# 运行中,每时每刻都会触发 on_timer_changed
UrsinaEvent.on_timer_changed_subscription(on_timer_changed)
# 使用 ursina 查看的运行效果 # 使用 ursina 查看的运行效果
# 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹
......
...@@ -46,7 +46,8 @@ if __name__ == '__main__': ...@@ -46,7 +46,8 @@ if __name__ == '__main__':
camera_look_at(moon, rotation_z=0) camera_look_at(moon, rotation_z=0)
# 订阅计时器事件(定时触发) # 订阅事件后,上面的函数功能才会起作用
# 运行中,每时每刻都会触发 on_timer_changed
UrsinaEvent.on_timer_changed_subscription(on_timer_changed) UrsinaEvent.on_timer_changed_subscription(on_timer_changed)
# 使用 ursina 查看的运行效果 # 使用 ursina 查看的运行效果
......
...@@ -57,7 +57,10 @@ if __name__ == '__main__': ...@@ -57,7 +57,10 @@ if __name__ == '__main__':
earth.look_at(sun, rotation_z=0) earth.look_at(sun, rotation_z=0)
# 订阅事件后,上面2个函数功能才会起作用
# 运行前会触发 on_ready
UrsinaEvent.on_ready_subscription(on_ready) UrsinaEvent.on_ready_subscription(on_ready)
# 运行中,每时每刻都会触发 on_timer_changed
UrsinaEvent.on_timer_changed_subscription(on_timer_changed) UrsinaEvent.on_timer_changed_subscription(on_timer_changed)
# 使用 ursina 查看的运行效果 # 使用 ursina 查看的运行效果
......
...@@ -66,6 +66,7 @@ class BodyTimer(Singleton): ...@@ -66,6 +66,7 @@ class BodyTimer(Singleton):
def __init__(self): def __init__(self):
if not hasattr(self, "inited"): if not hasattr(self, "inited"):
self.velocity_inc = 1e-30 # 理论上数值越小越好,可以使用 1e-300 ,但建议使用:1e-30 # 0.00001 self.velocity_inc = 1e-30 # 理论上数值越小越好,可以使用 1e-300 ,但建议使用:1e-30 # 0.00001
# 按键盘的 “O” 重置键会触发 on_reset
UrsinaEvent.on_reset_subscription(self.reset) UrsinaEvent.on_reset_subscription(self.reset)
UrsinaEvent.on_pause_subscription(self.pause) UrsinaEvent.on_pause_subscription(self.pause)
UrsinaEvent.on_start_subscription(self.start) UrsinaEvent.on_start_subscription(self.start)
......
...@@ -83,8 +83,9 @@ class Planet(Entity): ...@@ -83,8 +83,9 @@ class Planet(Entity):
else: else:
model = create_sphere(0.5, subdivisions) model = create_sphere(0.5, subdivisions)
rotation = (0, 0, 0) rotation = (0, 0, 0)
# 按键盘的 “O” 重置键会触发 on_reset
UrsinaEvent.on_reset_subscription(self.on_reset) UrsinaEvent.on_reset_subscription(self.on_reset)
# 当对天体大小进行缩放的时候会触发 change_body_scale
UrsinaEvent.on_body_size_changed_subscription(self.change_body_scale) UrsinaEvent.on_body_size_changed_subscription(self.change_body_scale)
super().__init__( super().__init__(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册