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

Python超人-宇宙模拟器

上级 28e024d5
...@@ -33,21 +33,22 @@ class HalleyCometSim: ...@@ -33,21 +33,22 @@ class HalleyCometSim:
def build_solar_system(self): def build_solar_system(self):
# region 构建太阳系 # region 构建太阳系
self.sun = Sun(size_scale=0.8e2, show_trail=False) show_trail = True
self.mercury = Mercury(size_scale=5e3, show_trail=False) self.sun = Sun(size_scale=0.8e2, show_trail=show_trail)
self.venus = Venus(size_scale=5e3, show_trail=False) self.mercury = Mercury(size_scale=5e3, show_trail=show_trail)
self.earth = Earth(size_scale=5e3, rotate_angle=0, show_trail=False) self.venus = Venus(size_scale=5e3, show_trail=show_trail)
self.mars = Mars(size_scale=5e3, show_trail=False) self.earth = Earth(size_scale=5e3, rotate_angle=0, show_trail=show_trail)
self.jupiter = Jupiter(size_scale=2.5e3, show_trail=False) self.mars = Mars(size_scale=5e3, show_trail=show_trail)
self.saturn = Saturn(size_scale=2.5e3, show_trail=False) self.jupiter = Jupiter(size_scale=2.5e3, show_trail=show_trail)
self.uranus = Uranus(size_scale=6e3, show_trail=False) self.saturn = Saturn(size_scale=2.5e3, show_trail=show_trail)
self.neptune = Neptune(size_scale=6e3, show_trail=False) self.uranus = Uranus(size_scale=6e3, show_trail=show_trail)
self.pluto = Pluto(size_scale=1e5, show_trail=False) self.neptune = Neptune(size_scale=6e3, show_trail=show_trail)
self.pluto = Pluto(size_scale=1e5, show_trail=show_trail)
self.bodies = [ self.bodies = [
self.sun, # 太阳 self.sun, # 太阳
# self.mercury, # 水星 self.mercury, # 水星
# self.venus, # 金星 self.venus, # 金星
# self.earth, # 地球 self.earth, # 地球
self.mars, # 火星 self.mars, # 火星
self.jupiter, # 木星 self.jupiter, # 木星
self.saturn, # 土星 self.saturn, # 土星
...@@ -55,6 +56,11 @@ class HalleyCometSim: ...@@ -55,6 +56,11 @@ class HalleyCometSim:
self.neptune, # 海王星 self.neptune, # 海王星
# self.pluto, # 冥王星 # self.pluto, # 冥王星
] ]
self.bodies = [
self.sun, # 太阳
self.mars, # 火星
self.neptune, # 海王星
]
# endregion # endregion
init_bodies_reality_pos_vels(self.bodies) init_bodies_reality_pos_vels(self.bodies)
...@@ -81,7 +87,7 @@ class HalleyCometSim: ...@@ -81,7 +87,7 @@ class HalleyCometSim:
def build(self): def build(self):
self.build_solar_system() self.build_solar_system()
self.build_halley_comet() # self.build_halley_comet()
def calculate_rotation_angles(self, point1, point2): def calculate_rotation_angles(self, point1, point2):
dx = point2.x - point1.x dx = point2.x - point1.x
...@@ -107,13 +113,13 @@ class HalleyCometSim: ...@@ -107,13 +113,13 @@ class HalleyCometSim:
return angle_x, angle_y, angle_z return angle_x, angle_y, angle_z
def calculate_angles(self, point1, point2): def calculate_angles(self, point1, point2):
import numpy as np
# 计算向量AB # 计算向量AB
AB = point2 - point1 AB = point2 - point1
# 计算向量AB与x轴、y轴和z轴之间的夹角 # 计算向量AB与x轴、y轴和z轴之间的夹角
angle_x = np.degrees(np.arctan2(AB.y, AB.x)) angle_x = math.degrees(math.atan2(AB.z, AB.y))
angle_y = np.degrees(np.arctan2(AB.z, np.sqrt(AB.x ** 2 + AB.y ** 2))) angle_y = math.degrees(math.atan2(AB.z, AB.x))
angle_z = np.degrees(np.arctan2(np.sqrt(AB.x ** 2 + AB.y ** 2), AB.z)) angle_z = math.degrees(math.atan2(AB.y, AB.x))
return angle_x, angle_y, angle_z return angle_x, angle_y, angle_z
def on_ready(self): def on_ready(self):
...@@ -125,6 +131,7 @@ class HalleyCometSim: ...@@ -125,6 +131,7 @@ class HalleyCometSim:
from ursina import scene from ursina import scene
UrsinaConfig.trail_type = "line" UrsinaConfig.trail_type = "line"
UrsinaConfig.trail_length = 91 UrsinaConfig.trail_length = 91
UrsinaConfig.trail_length = 1000
UrsinaConfig.trail_thickness_factor = 3 UrsinaConfig.trail_thickness_factor = 3
# camera.clip_plane_near = 0.1 # camera.clip_plane_near = 0.1
camera.clip_plane_far = 1000000 camera.clip_plane_far = 1000000
...@@ -167,9 +174,9 @@ body.position - sun.position ...@@ -167,9 +174,9 @@ body.position - sun.position
# orbit_line.rotation_y = rotation_y + 120 # orbit_line.rotation_y = rotation_y + 120
# # # #
# angle = math.atan2(relative_position.y, relative_position.x) # angle = math.atan2(relative_position.y, relative_position.x)
orbit_line.rotation_x = angle_x + 90 orbit_line.rotation_x = angle_x
orbit_line.rotation_y = angle_y + 180 orbit_line.rotation_y = angle_y # - 50
orbit_line.rotation_z = angle_z orbit_line.rotation_z = -angle_z
orbit_line.body = body orbit_line.body = body
...@@ -183,14 +190,15 @@ body.position - sun.position ...@@ -183,14 +190,15 @@ body.position - sun.position
@param time_data: @param time_data:
@return: @return:
""" """
# 哈雷彗星飞行的翻转效果 if hasattr(self, "self.halley_comet"):
if self.halley_comet.planet.enabled: # 哈雷彗星飞行的翻转效果
self.halley_comet.planet.rotation_x += 0.1 if self.halley_comet.planet.enabled:
self.halley_comet.planet.rotation_y += 1 self.halley_comet.planet.rotation_x += 0.1
# 摄像机始终看向哈雷彗星 self.halley_comet.planet.rotation_y += 1
# camera_look_at(self.halley_comet) # 摄像机始终看向哈雷彗星
d = calculate_distance(self.halley_comet.position, self.sun.position) # camera_look_at(self.halley_comet)
self.text_panel.text = "哈雷彗星距离太阳:%.3f AU" % (d / AU) d = calculate_distance(self.halley_comet.position, self.sun.position)
self.text_panel.text = "哈雷彗星距离太阳:%.3f AU" % (d / AU)
for i, orbit_line in enumerate(self.orbit_lines): for i, orbit_line in enumerate(self.orbit_lines):
if i < 4: if i < 4:
...@@ -207,6 +215,9 @@ if __name__ == '__main__': ...@@ -207,6 +215,9 @@ if __name__ == '__main__':
""" """
sim = HalleyCometSim() sim = HalleyCometSim()
sim.build() sim.build()
# 订阅事件后,上面2个函数功能才会起作用 # 订阅事件后,上面2个函数功能才会起作用
# 运行中,每时每刻都会触发 on_timer_changed # 运行中,每时每刻都会触发 on_timer_changed
UrsinaEvent.on_timer_changed_subscription(sim.on_timer_changed) UrsinaEvent.on_timer_changed_subscription(sim.on_timer_changed)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册