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

Python超人-宇宙模拟器

上级 56f5a2b4
from objs.diamond import Diamond from objs.diamond import Diamond
from objs.football import Football from objs.football import Football
from objs.satellite import Satellite from objs.satellite import Satellite
from objs.space_ship import SpaceShip
因为 它太大了无法显示 source diff 。你可以改为 查看blob
...@@ -25,6 +25,7 @@ class Obj(metaclass=ABCMeta): ...@@ -25,6 +25,7 @@ class Obj(metaclass=ABCMeta):
texture=None, size_scale=1.0, distance_scale=1.0, texture=None, size_scale=1.0, distance_scale=1.0,
parent=None, ignore_mass=False, parent=None, ignore_mass=False,
trail_color=None, show_name=False, trail_color=None, show_name=False,
rotation=None,
gravity_only_for=[], model=None): gravity_only_for=[], model=None):
""" """
对象类 对象类
...@@ -49,6 +50,7 @@ class Obj(metaclass=ABCMeta): ...@@ -49,6 +50,7 @@ class Obj(metaclass=ABCMeta):
self.__his_reserved_num = 200 self.__his_reserved_num = 200
self.gravity_only_for = gravity_only_for self.gravity_only_for = gravity_only_for
self.model = self.find_model(model) self.model = self.find_model(model)
self.rotation = rotation
if name is None: if name is None:
name = getattr(self.__class__, '__name__') name = getattr(self.__class__, '__name__')
......
# -*- coding:utf-8 -*-
# title :地球
# description :地球
# author :Python超人
# date :2023-02-11
# link :https://gitcode.net/pythoncr/
# python_version :3.8
# ==============================================================================
from objs.obj import Obj
class SpaceShip(Obj):
"""
太空飞船
"""
def __init__(self, name="太空飞船", mass=5.97237e24,
init_position=[0, 0, 0],
init_velocity=[0, 0, 0],
texture="space_ship.png", size_scale=1.0, distance_scale=1.0,
ignore_mass=False, density=1e3, color=(7, 0, 162),
trail_color=None, show_name=False,
model="space_ship.obj", rotation=(0, 0, 0),
parent=None, gravity_only_for=[]):
params = {
"name": name,
"mass": mass,
"init_position": init_position,
"init_velocity": init_velocity,
"density": density,
"color": color,
"texture": texture,
"size_scale": size_scale,
"distance_scale": distance_scale,
"ignore_mass": ignore_mass,
"trail_color": trail_color,
"show_name": show_name,
"parent": parent,
"rotation": rotation,
"gravity_only_for": gravity_only_for,
"model": model
}
super().__init__(**params)
if __name__ == '__main__':
spaceship = SpaceShip()
print(spaceship)
...@@ -225,6 +225,20 @@ def create_light_body(size_scale, init_position, speed=LIGHT_SPEED): ...@@ -225,6 +225,20 @@ def create_light_body(size_scale, init_position, speed=LIGHT_SPEED):
init_velocity=[0, 0, speed]).set_light_disable(True) init_velocity=[0, 0, speed]).set_light_disable(True)
def create_light_ship(size_scale, init_position, speed=LIGHT_SPEED):
"""
用天体模拟一个光速飞船
@param size_scale: 光速飞船的大小
@param init_position: 光速飞船的初始位置
@param speed: 光速飞船的速度->1光速=299792.458 千米/秒(km/秒)
@return:
"""
from objs.space_ship import SpaceShip
return SpaceShip(name='光速飞船', mass=0, size_scale=size_scale, color=(255, 110, 0),
init_position=init_position,
init_velocity=[0, 0, speed]).set_light_disable(True)
def create_text_panel(width=0.35, height=.5): def create_text_panel(width=0.35, height=.5):
# 创建一个 Panel 组件 # 创建一个 Panel 组件
from ursina import Text, Panel, color, camera, Vec3 from ursina import Text, Panel, color, camera, Vec3
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
# link :https://gitcode.net/pythoncr/ # link :https://gitcode.net/pythoncr/
# python_version :3.8 # python_version :3.8
# ============================================================================== # ==============================================================================
from sim_scenes.func import ursina_run, create_solar_system_bodies, create_light_body from sim_scenes.func import ursina_run, create_solar_system_bodies, create_light_ship
from common.consts import LIGHT_SPEED from common.consts import LIGHT_SPEED
from sim_scenes.solar_system.speed_of_light_init import SpeedOfLightInit from sim_scenes.solar_system.speed_of_light_init import SpeedOfLightInit
...@@ -25,12 +25,12 @@ bodies = create_solar_system_bodies(ignore_mass=True, init_velocity=[0, 0, 0]) ...@@ -25,12 +25,12 @@ bodies = create_solar_system_bodies(ignore_mass=True, init_velocity=[0, 0, 0])
# 从 init 对象中获取 光体的大小(light_size_scale),光体的位置(light_init_position) # 从 init 对象中获取 光体的大小(light_size_scale),光体的位置(light_init_position)
# 创建一个以光速前进的天体(模拟一个光子) speed=1光速=299792.458千米/秒,注意:质量为0才能达到光速,虽然如此,但也可以试试超光速 # 创建一个以光速前进的天体(模拟一个光子) speed=1光速=299792.458千米/秒,注意:质量为0才能达到光速,虽然如此,但也可以试试超光速
light_body = create_light_body(init.light_size_scale, init.light_init_position, speed=LIGHT_SPEED * 1) light_ship = create_light_ship(init.light_size_scale, init.light_init_position, speed=LIGHT_SPEED * 1)
# 增加光速天体到天体集合 # 增加光速天体到天体集合
bodies.append(light_body) bodies.append(light_ship)
# 运行前指定bodies、light_body并订阅事件 # 运行前指定bodies、light_body并订阅事件
init.light_body = light_body init.light_ship = light_ship
init.bodies = bodies init.bodies = bodies
init.event_subscription() init.event_subscription()
......
...@@ -33,35 +33,42 @@ class SpeedOfLightInit: ...@@ -33,35 +33,42 @@ class SpeedOfLightInit:
self.arrived_info = "" self.arrived_info = ""
self.__camera_follow_light = camera_follow_light self.__camera_follow_light = camera_follow_light
self.__light_body = None self.__light_ship = None
self.__bodies = None self.__bodies = None
self.view_closely = False self.view_closely = False
if self.__camera_follow_light in ["SideView", "SideViewActualSize"]: if self.__camera_follow_light in ["SideView"]:
# 摄像机位置 = 前-后+、上+下-、左-右+、 # 摄像机位置 = 前-后+、上+下-、左-右+、
self.camera_position = (AU, 0, 0) self.camera_position = (AU / 5, 0, 0)
self.show_trail = True self.show_trail = True
self.light_size_scale = 1e3 self.light_size_scale = 2e6
self.light_init_position = [AU / 3, 0, 0] self.light_init_position = [AU, 0, 0]
elif self.__camera_follow_light in ["SideViewActualSize"]:
# 摄像机位置 = 前-后+、上+下-、左-右+、
# self.camera_position = (0, AU, 0)
self.show_trail = True
self.light_size_scale = 2e6
self.light_init_position = [AU, 0, 0]
elif self.__camera_follow_light == "ForwardView": elif self.__camera_follow_light == "ForwardView":
# 摄像机位置 = 左-右+、上+下-、前+后- # 摄像机位置 = 左-右+、前+后-、上-下+
self.camera_position = (0, AU / 10, -AU) # self.camera_position = (0, AU / 10, -AU)
self.camera_position = (0, -AU / 25, -AU / 50)
self.show_trail = True self.show_trail = True
self.light_size_scale = 1e2 self.light_size_scale = 2e6
self.light_init_position = [AU / 12, 0, 0] self.light_init_position = [AU / 12, 0, 0]
else: else:
# 摄像机位置 = 左-右+、上+下-、前+后- # 摄像机位置 = 左-右+、上+下-、前+后-
self.camera_position = (0, AU, -6 * AU) self.camera_position = (0, AU, -6 * AU)
self.show_trail = True self.show_trail = True
self.light_size_scale = 2e3 self.light_size_scale = 1e7
self.light_init_position = [AU / 3, 0, 0] self.light_init_position = [AU / 3, 0, 0]
@property @property
def light_body(self): def light_ship(self):
return self.__light_body return self.__light_ship
@light_body.setter @light_ship.setter
def light_body(self, value): def light_ship(self, value):
self.__light_body = value self.__light_ship = value
@property @property
def bodies(self): def bodies(self):
...@@ -73,10 +80,10 @@ class SpeedOfLightInit: ...@@ -73,10 +80,10 @@ class SpeedOfLightInit:
if self.__camera_follow_light == "SideViewActualSize": if self.__camera_follow_light == "SideViewActualSize":
# TODO: 将天体的大小不进行缩放 # TODO: 将天体的大小不进行缩放
for body in self.__bodies: for body in self.__bodies:
if body is self.light_body: if body is self.light_ship:
continue continue
body.size_scale = 1 body.size_scale = 1
self.camera_position = [-self.light_init_position[0] / 1.35, 0, 0] self.camera_position = [-self.light_init_position[0] / 1.005, 0, 0]
self.view_closely = True self.view_closely = True
def on_reset(self): def on_reset(self):
...@@ -97,7 +104,7 @@ class SpeedOfLightInit: ...@@ -97,7 +104,7 @@ class SpeedOfLightInit:
订阅事件 订阅事件
@return: @return:
""" """
if self.light_body is None: if self.light_ship is None:
raise Exception("请指定 SpeedOfLightInit.light_body") raise Exception("请指定 SpeedOfLightInit.light_body")
if self.bodies is None: if self.bodies is None:
...@@ -136,14 +143,26 @@ class SpeedOfLightInit: ...@@ -136,14 +143,26 @@ class SpeedOfLightInit:
self.text_panel = create_text_panel() self.text_panel = create_text_panel()
self.text_panel.text = self.arrived_info.replace("${distance}", "0 AU") self.text_panel.text = self.arrived_info.replace("${distance}", "0 AU")
if self.__camera_follow_light in ["SideView", "SideViewActualSize"]: self.light_ship.planet.rotation_x = 90
camera.parent = self.light_body.planet
if self.__camera_follow_light in ["SideView"]:
camera.rotation_z = -90
camera.rotation_y = -85
elif self.__camera_follow_light in ["ForwardView"]:
camera.rotation_x = -75
elif self.__camera_follow_light in ["SideViewActualSize"]:
self.light_ship.planet.rotation_x = 0
# camera.rotation_z = -90
camera.rotation_y = -85 camera.rotation_y = -85
if self.__camera_follow_light in ["SideView", "SideViewActualSize"]:
camera.parent = self.light_ship.planet
elif self.__camera_follow_light == "ForwardView": elif self.__camera_follow_light == "ForwardView":
# self.light_body.planet.enabled = False # self.light_body.planet.enabled = False
camera.parent = self.light_body.planet camera.parent = self.light_ship.planet
self.light_body.planet.input = self.light_body_input self.light_ship.planet.input = self.light_body_input
camera.rotation_y = -15 # camera.rotation_y = -15
if hasattr(camera, "sky"): if hasattr(camera, "sky"):
# 摄像机跟随地球后,需要对深空背景进行调整,否则看到的是黑色背景 # 摄像机跟随地球后,需要对深空背景进行调整,否则看到的是黑色背景
camera.sky.scale = 800 camera.sky.scale = 800
...@@ -151,7 +170,7 @@ class SpeedOfLightInit: ...@@ -151,7 +170,7 @@ class SpeedOfLightInit:
camera.clip_plane_far = 1000000 camera.clip_plane_far = 1000000
# 取消订阅(防止 光体 的大小进行变化影响摄像机的视角) # 取消订阅(防止 光体 的大小进行变化影响摄像机的视角)
UrsinaEvent.on_body_size_changed_unsubscription(self.light_body.planet.change_body_scale) UrsinaEvent.on_body_size_changed_unsubscription(self.light_ship.planet.change_body_scale)
# def on_body_size(self): # def on_body_size(self):
# self.light_body.planet.scale = self.light_body.planet_scale # self.light_body.planet.scale = self.light_body.planet_scale
...@@ -201,7 +220,7 @@ class SpeedOfLightInit: ...@@ -201,7 +220,7 @@ class SpeedOfLightInit:
run_speed_maps = smooth_speed_transition(run_speed_maps) run_speed_maps = smooth_speed_transition(run_speed_maps)
self.run_speed_maps = run_speed_maps self.run_speed_maps = run_speed_maps
light_distance = self.light_body.position[2] light_distance = self.light_ship.position[2]
current_idx = 0 current_idx = 0
for i, m in enumerate(self.run_speed_maps): for i, m in enumerate(self.run_speed_maps):
if i == 0: if i == 0:
...@@ -246,19 +265,19 @@ class SpeedOfLightInit: ...@@ -246,19 +265,19 @@ class SpeedOfLightInit:
self.auto_run_speed() self.auto_run_speed()
for body in self.bodies: for body in self.bodies:
if body is self.light_body or isinstance(body, Sun) \ if body is self.light_ship or isinstance(body, Sun) \
or body in self.arrived_bodies or isinstance(body, Asteroids): or body in self.arrived_bodies or isinstance(body, Asteroids):
# 对于光速天体、太阳、小行星群、“已到达天体列表”中的天体无需计算 # 对于光速天体、太阳、小行星群、“已到达天体列表”中的天体无需计算
continue continue
# 计算判断,如果光速天体距离到达了某个天体,就记录到“已到达天体列表”中 # 计算判断,如果光速天体距离到达了某个天体,就记录到“已到达天体列表”中
if self.light_body.position[2] >= body.position[2]: if self.light_ship.position[2] >= body.position[2]:
self.arrived_bodies.append(body) self.arrived_bodies.append(body)
if self.text_panel is not None: if self.text_panel is not None:
self.arrived_info += f"[{time_data.time_text}]\t到达\t[{body.name}]\n\n" self.arrived_info += f"[{time_data.time_text}]\t到达\t[{body.name}]\n\n"
# distance = round(self.light_body.position[2] / AU, 4) # distance = round(self.light_body.position[2] / AU, 4)
# # print("浮点数保留两位小数,宽5位,不足补0:%05.5f " % 2.222) # # print("浮点数保留两位小数,宽5位,不足补0:%05.5f " % 2.222)
# self.text_panel.text = self.arrived_info.replace("${distance}", "%.4f AU" % distance) # self.text_panel.text = self.arrived_info.replace("${distance}", "%.4f AU" % distance)
print(f"[{time_data.time_text}] 到达 [{body.name}] {round(self.light_body.position[2] / AU, 4)} AU") print(f"[{time_data.time_text}] 到达 [{body.name}] {round(self.light_ship.position[2] / AU, 4)} AU")
return return
if not hasattr(self, "last_time"): if not hasattr(self, "last_time"):
...@@ -269,5 +288,5 @@ class SpeedOfLightInit: ...@@ -269,5 +288,5 @@ class SpeedOfLightInit:
# self.text_panel.text = self.arrived_info.replace("${distance}", "%.4f AU" % distance) # self.text_panel.text = self.arrived_info.replace("${distance}", "%.4f AU" % distance)
self.last_time = datetime.datetime.now() self.last_time = datetime.datetime.now()
distance = round(self.light_body.position[2] / AU, 4) distance = round(self.light_ship.position[2] / AU, 4)
self.text_panel.text = self.arrived_info.replace("${distance}", "%.4f AU" % distance) self.text_panel.text = self.arrived_info.replace("${distance}", "%.4f AU" % distance)
\ No newline at end of file
...@@ -74,7 +74,11 @@ class Planet(Entity): ...@@ -74,7 +74,11 @@ class Planet(Entity):
model = self.body.model model = self.body.model
else: else:
model = self.body.model model = self.body.model
rotation = (0, 0, 0) if hasattr(self.body, "rotation"):
if self.body.rotation is None:
rotation = (0, 0, 0)
else:
rotation = tuple(self.body.rotation)
else: else:
model = create_sphere(0.5, subdivisions) model = create_sphere(0.5, subdivisions)
rotation = (0, 0, 0) rotation = (0, 0, 0)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册