From f5d53e79d1c864472ba0440e670e3608f058d0b9 Mon Sep 17 00:00:00 2001 From: march3 Date: Mon, 25 Mar 2024 13:21:51 +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 --- common/celestial_data_service.py | 14 ++++++ .../\345\244\252\351\230\263\347\263\273.py" | 45 ++++++++++++++++- ...41\346\213\237\345\234\272\346\231\257.py" | 49 +++++++++++++++++-- .../\345\244\252\351\230\263\347\263\273.py" | 45 +++++++++++++++++ 4 files changed, 148 insertions(+), 5 deletions(-) create mode 100644 "\345\274\225\345\212\233\344\270\226\347\225\214/\346\250\241\346\213\237\345\256\236\351\252\214\345\256\244/\345\244\252\351\230\263\347\263\273/\345\244\252\351\230\263\347\263\273.py" diff --git a/common/celestial_data_service.py b/common/celestial_data_service.py index a682921..812b845 100644 --- a/common/celestial_data_service.py +++ b/common/celestial_data_service.py @@ -439,6 +439,20 @@ def get_reality_orbit_points(body_name, start_time=None, days=365, segments=100, else: s = int(round(days / segments)) + body_dict = {'地球': 'earth', + '太阳': 'sun', + '月球': 'moon', + '水星': 'mercury', + '金星': 'venus', + '地月': 'earth-moon-barycenter', + '火星': 'mars', + '木星': 'jupiter', + '土星': 'saturn', + '天王星': 'uranus', + '海王星': 'neptune'} + + body_name = body_dict.get(body_name, body_name) + points = [] for d in range(0, days, s): dt = d + start_time diff --git "a/\345\274\225\345\212\233\344\270\226\347\225\214/\344\270\207\347\211\251/\345\244\252\351\230\263\347\263\273.py" "b/\345\274\225\345\212\233\344\270\226\347\225\214/\344\270\207\347\211\251/\345\244\252\351\230\263\347\263\273.py" index 2011d81..50b4912 100644 --- "a/\345\274\225\345\212\233\344\270\226\347\225\214/\344\270\207\347\211\251/\345\244\252\351\230\263\347\263\273.py" +++ "b/\345\274\225\345\212\233\344\270\226\347\225\214/\344\270\207\347\211\251/\345\244\252\351\230\263\347\263\273.py" @@ -1,8 +1,7 @@ -from bodies import Sun, Earth, Moon +from bodies import * from 引力世界 import 造物主 - class 太阳(Sun): def __init__(self, 初始位置=[0, 0, 0], 初始速度=[0, 0, 0], 缩放大小=1): super(太阳, self).__init__(init_velocity=初始速度, init_position=初始位置, size_scale=缩放大小) @@ -18,6 +17,48 @@ class 月球(Moon): super(月球, self).__init__(init_velocity=初始速度, init_position=初始位置, size_scale=缩放大小) +class 水星(Mercury): + def __init__(self, 初始位置=[0, 0, 0], 初始速度=[0, 0, 0], 缩放大小=1): + super(水星, self).__init__(init_velocity=初始速度, init_position=初始位置, size_scale=缩放大小) + + +class 金星(Venus): + def __init__(self, 初始位置=[0, 0, 0], 初始速度=[0, 0, 0], 缩放大小=1): + super(金星, self).__init__(init_velocity=初始速度, init_position=初始位置, size_scale=缩放大小) + + +class 火星(Mars): + def __init__(self, 初始位置=[0, 0, 0], 初始速度=[0, 0, 0], 缩放大小=1): + super(火星, self).__init__(init_velocity=初始速度, init_position=初始位置, size_scale=缩放大小) + + +class 木星(Jupiter): + def __init__(self, 初始位置=[0, 0, 0], 初始速度=[0, 0, 0], 缩放大小=1): + super(木星, self).__init__(init_velocity=初始速度, init_position=初始位置, size_scale=缩放大小) + + +class 土星(Saturn): + def __init__(self, 初始位置=[0, 0, 0], 初始速度=[0, 0, 0], 缩放大小=1): + super(土星, self).__init__(init_velocity=初始速度, init_position=初始位置, size_scale=缩放大小) + + +class 天王星(Uranus): + def __init__(self, 初始位置=[0, 0, 0], 初始速度=[0, 0, 0], 缩放大小=1): + super(天王星, self).__init__(init_velocity=初始速度, init_position=初始位置, size_scale=缩放大小) + + +class 海王星(Neptune): + def __init__(self, 初始位置=[0, 0, 0], 初始速度=[0, 0, 0], 缩放大小=1): + super(海王星, self).__init__(init_velocity=初始速度, init_position=初始位置, size_scale=缩放大小) + + 造物主.登记(['太阳', 'sun'], 太阳) 造物主.登记(['地球', 'earth'], 地球) 造物主.登记(['月球', 'moon'], 月球) +造物主.登记(['水星', 'mercury'], 水星) +造物主.登记(['金星', 'venus'], 金星) +造物主.登记(['火星', 'mars'], 火星) +造物主.登记(['木星', 'jupiter'], 木星) +造物主.登记(['土星', 'saturn'], 土星) +造物主.登记(['天王星', 'uranus'], 天王星) +造物主.登记(['海王星', 'neptune'], 海王星) diff --git "a/\345\274\225\345\212\233\344\270\226\347\225\214/\346\250\241\346\213\237\345\234\272\346\231\257.py" "b/\345\274\225\345\212\233\344\270\226\347\225\214/\346\250\241\346\213\237\345\234\272\346\231\257.py" index e92278d..410be32 100644 --- "a/\345\274\225\345\212\233\344\270\226\347\225\214/\346\250\241\346\213\237\345\234\272\346\231\257.py" +++ "b/\345\274\225\345\212\233\344\270\226\347\225\214/\346\250\241\346\213\237\345\234\272\346\231\257.py" @@ -8,15 +8,18 @@ # ============================================================================== from ursina import Ursina, camera, EditorCamera, Sky, Entity, load_model, color -from common.celestial_data_service import set_solar_system_celestial_position, conv_to_astropy_time +from common.celestial_data_service import set_solar_system_celestial_position, conv_to_astropy_time, \ + get_reality_orbit_points from common.consts import SECONDS_PER_DAY, AU from bodies.body import Body +from bodies import * from sim_scenes.func import create_sphere_sky, ursina_run from sim_scenes.universe_sim_scenes import UniverseSimScenes from simulators.ursina.entities.body_timer import TimeData from simulators.ursina.ui.control_ui import ControlUI from simulators.ursina.ursina_config import UrsinaConfig from simulators.ursina.ursina_event import UrsinaEvent +from simulators.ursina.ursina_mesh import create_orbit_by_points from 引力世界.数据.天体数据 import get_body_pos_vel @@ -49,8 +52,13 @@ class 模拟场景(UniverseSimScenes): def on_ready(self): - from ursina import application + from ursina import application, window # application.time_scale = 0.0001 + + window_size = (int(1920/2), int(1079/2)) + UniverseSimScenes.set_window_size(window_size, False) + window.borderless = False + window.position = (20, 20) # 运行前触发 self.sky = create_sphere_sky(texture="bg_pan2.jpg", scale=500000, rotation_x=20, rotation_y=160, rotation_z=20) @@ -95,6 +103,37 @@ class 太阳系模拟场景(模拟场景): def __init__(self): self.造物() + def create_orbit_line(self,center_body, body, start_time, alpha=0.2): + import math + if not hasattr(body, "orbital_days"): + return None + orbital_days = int(math.ceil(body.orbital_days)) + 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, + alpha=alpha) + return orbit_line + + def create_orbit_lines(self): + """ + 创建太阳系天体的真实轨迹(太阳和哈雷彗星除外) + @return: + """ + self.orbit_lines = [] + for body in self.bodies: + if isinstance(body, Sun): + continue + if isinstance(body, Earth): + alpha = 0.5 + else: + alpha = 0.2 + orbit_line = self.create_orbit_line(self.太阳, body, self.start_time, alpha=alpha) + if orbit_line is not None: + self.orbit_lines.append(orbit_line) + def set_bodies_position(self, time_data: TimeData): """ 设置天体的位置(包含速度和加速度的信息) @@ -104,6 +143,10 @@ class 太阳系模拟场景(模拟场景): t = self.start_time + time_data.total_days set_solar_system_celestial_position(self.bodies, t, True, self.地月距离放大倍数) + def on_ready(self): + super(太阳系模拟场景, self).on_ready() + self.create_orbit_lines() + def show_clock(self, time_data): """ 显示时钟 @@ -142,7 +185,7 @@ class 太阳系模拟场景(模拟场景): # 使用 ursina 查看的运行效果 # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 # position = 左-右+、上+下-、前+后- - self.地月距离放大倍数 = 50 + self.地月距离放大倍数 = 10 if isinstance(运行时间, str): self.start_time = conv_to_astropy_time(运行时间) else: diff --git "a/\345\274\225\345\212\233\344\270\226\347\225\214/\346\250\241\346\213\237\345\256\236\351\252\214\345\256\244/\345\244\252\351\230\263\347\263\273/\345\244\252\351\230\263\347\263\273.py" "b/\345\274\225\345\212\233\344\270\226\347\225\214/\346\250\241\346\213\237\345\256\236\351\252\214\345\256\244/\345\244\252\351\230\263\347\263\273/\345\244\252\351\230\263\347\263\273.py" new file mode 100644 index 0000000..5944d0d --- /dev/null +++ "b/\345\274\225\345\212\233\344\270\226\347\225\214/\346\250\241\346\213\237\345\256\236\351\252\214\345\256\244/\345\244\252\351\230\263\347\263\273/\345\244\252\351\230\263\347\263\273.py" @@ -0,0 +1,45 @@ +# -*- coding:utf-8 -*- +# title :太阳地球月球模拟 +# description :太阳地球月球模拟 +# date :2024-03-20 +# python_version :3.9 +# ============================================================================== + +from 引力世界 import 造物主 +from 引力世界.模拟场景 import 太阳系模拟场景 + + +class 太阳系模拟(太阳系模拟场景): + def 造物(self): + 太阳坐标, 太阳速度 = self.获取坐标和速度('太阳') + 地球坐标, 地球速度 = self.获取坐标和速度('地球') + 月球坐标, 月球速度 = self.获取坐标和速度('月球') + + 水星坐标, 水星速度 = self.获取坐标和速度('水星') + 金星坐标, 金星速度 = self.获取坐标和速度('金星') + 火星坐标, 火星速度 = self.获取坐标和速度('火星') + 木星坐标, 木星速度 = self.获取坐标和速度('木星') + 土星坐标, 土星速度 = self.获取坐标和速度('土星') + 天王星坐标, 天王星速度 = self.获取坐标和速度('天王星') + 海王星坐标, 海王星速度 = self.获取坐标和速度('海王星') + + + self.太阳 = 造物主.造物(名称='太阳', 缩放大小=1e1) + self.地球 = 造物主.造物(名称='地球', 缩放大小=5.5e2) + self.月球 = 造物主.造物(名称='月球', 缩放大小=5e2) + + self.水星 = 造物主.造物(名称='水星', 缩放大小=1e3) + self.金星 = 造物主.造物(名称='金星', 缩放大小=5e2) + self.火星 = 造物主.造物(名称='火星', 缩放大小=5e2) + self.木星 = 造物主.造物(名称='木星', 缩放大小=5e2) + self.土星 = 造物主.造物(名称='土星', 缩放大小=5e2) + self.天王星 = 造物主.造物(名称='天王星', 缩放大小=5e2) + self.海王星 = 造物主.造物(名称='海王星', 缩放大小=5e2) + + + # print(self.太阳) + + +场景 = 太阳系模拟() +# 场景.摄像机看向(场景.地球) +场景.运行(运行时间='1982-09-24 00:00:00') -- GitLab