From 3558bb8ccd2c1f29c2bd6d4efa481ce38abe1d5a Mon Sep 17 00:00:00 2001 From: march3 Date: Mon, 6 Nov 2023 21:33:48 +0800 Subject: [PATCH] =?UTF-8?q?Python=E8=B6=85=E4=BA=BA-=E5=AE=87=E5=AE=99?= =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bodies/earth.py | 2 + bodies/jupiter.py | 2 + bodies/mars.py | 2 + bodies/mercury.py | 2 + bodies/neptune.py | 2 + bodies/saturn.py | 2 + bodies/uranus.py | 2 + bodies/venus.py | 2 + sim_scenes/solar_system/halley_comet_lib.py | 48 +++++++++++++++++++ sim_scenes/solar_system/halley_comet_sim.py | 36 ++------------ .../solar_system/halley_comet_sim_02.py | 35 ++++---------- .../solar_system/halley_comet_sim_03.py | 11 +---- 12 files changed, 79 insertions(+), 67 deletions(-) create mode 100644 sim_scenes/solar_system/halley_comet_lib.py diff --git a/bodies/earth.py b/bodies/earth.py index e1ea7dd..de8f420 100644 --- a/bodies/earth.py +++ b/bodies/earth.py @@ -51,6 +51,8 @@ class Earth(Body): "parent": parent } super().__init__(**params) + # 公转周期(天) + self.orbital_days = 365.24219 if __name__ == '__main__': diff --git a/bodies/jupiter.py b/bodies/jupiter.py index 50b04d9..ebf972d 100644 --- a/bodies/jupiter.py +++ b/bodies/jupiter.py @@ -45,6 +45,8 @@ class Jupiter(Body): "show_name": show_name } super().__init__(**params) + # 公转周期(天) + self.orbital_days = 11.862 * 365.24219 if __name__ == '__main__': diff --git a/bodies/mars.py b/bodies/mars.py index 2ef052e..2e3d420 100644 --- a/bodies/mars.py +++ b/bodies/mars.py @@ -45,6 +45,8 @@ class Mars(Body): "show_name": show_name } super().__init__(**params) + # 公转周期(天) + self.orbital_days = 686.971 if __name__ == '__main__': diff --git a/bodies/mercury.py b/bodies/mercury.py index f7e12e5..45dbcbc 100644 --- a/bodies/mercury.py +++ b/bodies/mercury.py @@ -46,6 +46,8 @@ class Mercury(Body): "show_name": show_name } super().__init__(**params) + # + self.orbital_days = 87.9691 if __name__ == '__main__': diff --git a/bodies/neptune.py b/bodies/neptune.py index 0407fad..9e35cdf 100644 --- a/bodies/neptune.py +++ b/bodies/neptune.py @@ -46,6 +46,8 @@ class Neptune(Body): "show_name": show_name } super().__init__(**params) + # 公转周期(天) + self.orbital_days = 164.8 * 365.24219 if __name__ == '__main__': diff --git a/bodies/saturn.py b/bodies/saturn.py index 81ac4e5..c53bb71 100644 --- a/bodies/saturn.py +++ b/bodies/saturn.py @@ -47,6 +47,8 @@ class Saturn(Body): } self.__has_rings = True super().__init__(**params) + # 公转周期(天) + self.orbital_days = 29.4571 * 365.24219 def show_rings(self, value): self.__has_rings = value diff --git a/bodies/uranus.py b/bodies/uranus.py index 103fac2..6c387c3 100644 --- a/bodies/uranus.py +++ b/bodies/uranus.py @@ -46,6 +46,8 @@ class Uranus(Body): "show_name": show_name } super().__init__(**params) + # 公转周期(天) + self.orbital_days = 84.0205 * 365.24219 if __name__ == '__main__': diff --git a/bodies/venus.py b/bodies/venus.py index 64f0d77..ca8f726 100644 --- a/bodies/venus.py +++ b/bodies/venus.py @@ -46,6 +46,8 @@ class Venus(Body): "show_name": show_name } super().__init__(**params) + # 公转周期(天) + self.orbital_days = 224.701 if __name__ == '__main__': diff --git a/sim_scenes/solar_system/halley_comet_lib.py b/sim_scenes/solar_system/halley_comet_lib.py new file mode 100644 index 0000000..fc6262b --- /dev/null +++ b/sim_scenes/solar_system/halley_comet_lib.py @@ -0,0 +1,48 @@ +# -*- coding:utf-8 -*- +# title :哈雷彗星场景半真实模拟共用库 +# description : +# author :Python超人 +# date :2023-11-06 +# link :https://gitcode.net/pythoncr/ +# python_version :3.9 +# ============================================================================== +import math +from dataclasses import dataclass +from dataclasses import field + +from common.celestial_data_service import get_reality_orbit_points +from common.consts import AU +from objs import HalleComet +from simulators.ursina.ursina_mesh import create_orbit_by_points + + +@dataclass(order=True) +class HalleyCometParams: + start_time: str = field(default='1983-03-20 00:00:00') + init_velocity: list[float] = field(default_factory=[3.34, 0, 10.718]) + init_position: list[float] = field(default_factory=[0, 0.5 * AU, -10 * AU]) + + +def create_halley_comet(init_velocity, init_position): + """ + 创建哈雷彗星 + @return: + """ + # 哈雷彗星的平均运行速度约为每小时 70,000 英里或每小时 126,000 公里 。(35公里/秒) + # 每76.1年环绕太阳一周的周期彗星 + halley_comet = HalleComet( + # size_scale=4e7, + size_scale=1e8, + init_velocity=init_velocity, + init_position=init_position) \ + .set_light_disable(True) + return halley_comet + + +def create_orbit_line(center_body, body, start_time): + orbital_days = int(math.ceil(body.orbital_days * 1.02)) + points = get_reality_orbit_points(type(body).__name__.lower(), start_time=start_time, days=orbital_days, + segments=100) + # print(points) + orbit_line = create_orbit_by_points(center_body.position, points, line_color=body.trail_color) + return orbit_line diff --git a/sim_scenes/solar_system/halley_comet_sim.py b/sim_scenes/solar_system/halley_comet_sim.py index 338d78f..884538b 100644 --- a/sim_scenes/solar_system/halley_comet_sim.py +++ b/sim_scenes/solar_system/halley_comet_sim.py @@ -20,6 +20,7 @@ from common.func import calculate_distance from objs import HalleComet from sim_scenes.func import create_text_panel from sim_scenes.func import ursina_run, create_sphere_sky +from sim_scenes.solar_system.halley_comet_lib import HalleyCometParams, create_halley_comet, create_orbit_line from simulators.ursina.entities.body_timer import TimeData from simulators.ursina.entities.entity_utils import get_value_direction_vectors from simulators.ursina.ui.control_ui import ControlUI @@ -30,13 +31,6 @@ from dataclasses import dataclass from dataclasses import field -@dataclass(order=True) -class HalleyCometParams: - start_time: str = field(default='1983-03-20 00:00:00') - init_velocity: list[float] = field(default_factory=[3.34, 0, 10.718]) - init_position: list[float] = field(default_factory=[0, 0.5 * AU, -10 * AU]) - - class HalleyCometSim: """ 哈雷彗星场景模拟 @@ -80,14 +74,6 @@ class HalleyCometSim: self.neptune, # 海王星 # self.pluto, # 冥王星 ] - self.mercury.orbital_days = 87.9691 - self.venus.orbital_days = 224.701 - self.earth.orbital_days = 365.24219 - self.mars.orbital_days = 686.971 - self.jupiter.orbital_days = 11.862 * 365.24219 - self.saturn.orbital_days = 29.4571 * 365.24219 - self.uranus.orbital_days = 84.0205 * 365.24219 - self.neptune.orbital_days = 164.8 * 365.24219 # self.bodies = [ # self.sun, # 太阳 @@ -107,27 +93,13 @@ class HalleyCometSim: 创建哈雷彗星 @return: """ - # 哈雷彗星的平均运行速度约为每小时 70,000 英里或每小时 126,000 公里 。(35公里/秒) - # 每76.1年环绕太阳一周的周期彗星 - self.halley_comet = HalleComet( # size_scale=4e7, - size_scale=0.4e8, - init_velocity=self.params.init_velocity, - init_position=self.params.init_position) \ - .set_light_disable(True) - + self.halley_comet = create_halley_comet(self.params.init_velocity, self.params.init_position) self.bodies.append(self.halley_comet) def build(self): self.build_solar_system() self.build_halley_comet() - def create_orbit_line(self, center_body, body): - orbital_days = int(math.ceil(body.orbital_days * 1.02)) - points = get_reality_orbit_points(type(body).__name__.lower(), start_time=self.start_time, days=orbital_days, - segments=100) - # print(points) - orbit_line = create_orbit_by_points(center_body.position, points, line_color=body.trail_color) - return orbit_line def on_ready(self): """ @@ -150,7 +122,7 @@ class HalleyCometSim: for body in self.bodies[1:]: if isinstance(body, HalleComet): continue - orbit_line = self.create_orbit_line(self.sun, body) + orbit_line = create_orbit_line(self.sun, body, self.start_time) self.orbit_lines.append(orbit_line) self.text_panel = create_text_panel() @@ -323,8 +295,6 @@ if __name__ == '__main__': # init_velocity=[-2.841, 4.7, 8.86], # init_position=[0, -5 * AU, -10 * AU] - - sim = HalleyCometSim(params) sim.build() diff --git a/sim_scenes/solar_system/halley_comet_sim_02.py b/sim_scenes/solar_system/halley_comet_sim_02.py index c8b60ff..7882345 100644 --- a/sim_scenes/solar_system/halley_comet_sim_02.py +++ b/sim_scenes/solar_system/halley_comet_sim_02.py @@ -20,6 +20,7 @@ from common.func import calculate_distance from objs import HalleComet from sim_scenes.func import create_text_panel from sim_scenes.func import ursina_run, create_sphere_sky +from sim_scenes.solar_system.halley_comet_lib import create_halley_comet, create_orbit_line from simulators.ursina.ursina_config import UrsinaConfig from simulators.ursina.ursina_event import UrsinaEvent from simulators.ursina.ursina_mesh import create_orbit_by_points @@ -64,14 +65,6 @@ class HalleyCometSim: self.neptune, # 海王星 # self.pluto, # 冥王星 ] - self.mercury.orbital_days = 87.9691 - self.venus.orbital_days = 224.701 - self.earth.orbital_days = 365.24219 - self.mars.orbital_days = 686.971 - self.jupiter.orbital_days = 11.862 * 365.24219 - self.saturn.orbital_days = 29.4571 * 365.24219 - self.uranus.orbital_days = 84.0205 * 365.24219 - self.neptune.orbital_days = 164.8 * 365.24219 # self.bodies = [ # self.sun, # 太阳 @@ -88,28 +81,20 @@ class HalleyCometSim: 创建哈雷彗星 @return: """ - # 哈雷彗星的平均运行速度约为每小时 70,000 英里或每小时 126,000 公里 。(35公里/秒) - # 每76.1年环绕太阳一周的周期彗星 - self.halley_comet = HalleComet( - # size_scale=4e7, - size_scale=1e8, - init_velocity=[-2.836, 4.705, 8.85], - init_position=[0, -5 * AU, -10 * AU]) \ - .set_light_disable(True) - + self.halley_comet = create_halley_comet([-2.836, 4.705, 8.85], [0, -5 * AU, -10 * AU]) self.bodies.append(self.halley_comet) def build(self): self.build_solar_system() self.build_halley_comet() - def create_orbit_line(self, center_body, body): - orbital_days = int(math.ceil(body.orbital_days * 1.02)) - points = get_reality_orbit_points(type(body).__name__.lower(), start_time=self.dt, days=orbital_days, - segments=100) - # print(points) - orbit_line = create_orbit_by_points(center_body.position, points, line_color=body.trail_color) - return orbit_line + # def create_orbit_line(self, center_body, body): + # orbital_days = int(math.ceil(body.orbital_days * 1.02)) + # points = get_reality_orbit_points(type(body).__name__.lower(), start_time=self.dt, days=orbital_days, + # segments=100) + # # print(points) + # orbit_line = create_orbit_by_points(center_body.position, points, line_color=body.trail_color) + # return orbit_line def on_ready(self): """ @@ -134,7 +119,7 @@ class HalleyCometSim: for body in self.bodies[1:]: if isinstance(body, HalleComet): continue - orbit_line = self.create_orbit_line(self.sun, body) + orbit_line = create_orbit_line(self.sun, body, self.dt) self.orbit_lines.append(orbit_line) self.text_panel = create_text_panel() diff --git a/sim_scenes/solar_system/halley_comet_sim_03.py b/sim_scenes/solar_system/halley_comet_sim_03.py index 99e83b3..f3e3543 100644 --- a/sim_scenes/solar_system/halley_comet_sim_03.py +++ b/sim_scenes/solar_system/halley_comet_sim_03.py @@ -18,6 +18,7 @@ from common.func import calculate_distance from objs import HalleComet, Obj from sim_scenes.func import camera_look_at, two_bodies_colliding, create_text_panel from sim_scenes.func import ursina_run, create_sphere_sky +from sim_scenes.solar_system.halley_comet_lib import create_halley_comet from simulators.ursina.ursina_config import UrsinaConfig from simulators.ursina.ursina_event import UrsinaEvent from simulators.ursina.ursina_mesh import create_orbit_line @@ -64,14 +65,7 @@ class HalleyCometSim: 创建哈雷彗星 @return: """ - # 哈雷彗星的平均运行速度约为每小时 70,000 英里或每小时 126,000 公里 。(35公里/秒) - # 每76.1年环绕太阳一周的周期彗星 - self.halley_comet = HalleComet( # size_scale=4e7, - size_scale=1e8, - init_velocity=[-3.34, 3, 10.718], - init_position=[0, -2 * AU, -10 * AU]) \ - .set_light_disable(True) - + self.halley_comet = create_halley_comet([-3.34, 3, 10.718], [0, -2 * AU, -10 * AU]) self.bodies.append(self.halley_comet) def build(self): @@ -84,7 +78,6 @@ class HalleyCometSim: @return: """ # 创建天空 - from ursina import scene UrsinaConfig.trail_type = "line" UrsinaConfig.trail_length = 91 UrsinaConfig.trail_thickness_factor = 3 -- GitLab