From 28e024d56acb7ab157eb262f22c62512fd3a272f Mon Sep 17 00:00:00 2001 From: march3 Date: Fri, 27 Oct 2023 14:26: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 --- .../solar_system/halley_comet_sim_02.py | 83 +++++++++++++++++-- simulators/ursina/entities/world_grid.py | 14 +++- 2 files changed, 87 insertions(+), 10 deletions(-) diff --git a/sim_scenes/solar_system/halley_comet_sim_02.py b/sim_scenes/solar_system/halley_comet_sim_02.py index a39b257..0b02a2c 100644 --- a/sim_scenes/solar_system/halley_comet_sim_02.py +++ b/sim_scenes/solar_system/halley_comet_sim_02.py @@ -7,7 +7,7 @@ # python_version :3.9 # ============================================================================== import time - +import math from ursina import camera, application from bodies import Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto @@ -17,6 +17,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 simulators.ursina.entities.world_grid import WorldGrid from simulators.ursina.ursina_config import UrsinaConfig from simulators.ursina.ursina_event import UrsinaEvent from simulators.ursina.ursina_mesh import create_orbit_line @@ -44,9 +45,9 @@ class HalleyCometSim: self.pluto = Pluto(size_scale=1e5, show_trail=False) self.bodies = [ self.sun, # 太阳 - self.mercury, # 水星 - self.venus, # 金星 - self.earth, # 地球 + # self.mercury, # 水星 + # self.venus, # 金星 + # self.earth, # 地球 self.mars, # 火星 self.jupiter, # 木星 self.saturn, # 土星 @@ -82,6 +83,39 @@ class HalleyCometSim: self.build_solar_system() self.build_halley_comet() + def calculate_rotation_angles(self, point1, point2): + dx = point2.x - point1.x + dy = point2.y - point1.y + dz = point2.z - point1.z + + roll = math.atan2(dy, dz) + pitch = math.atan2(dx, math.sqrt(dy ** 2 + dz ** 2)) + yaw = math.atan2(math.sin(roll), math.cos(roll)) + + return roll, pitch, yaw + + def calculate_angles(self, point1, point2): + import numpy as np + # 计算向量AB + AB = point1 - point2 + + # 计算向量AB与x轴、y轴和z轴之间的夹角 + angle_x = np.arctan2(AB.y, AB.x) * 180 / np.pi + angle_y = np.arctan2(AB.z, np.sqrt(AB.x ** 2 + AB.y ** 2)) * 180 / np.pi + angle_z = np.arctan2(np.sqrt(AB.x ** 2 + AB.y ** 2), AB.z) * 180 / np.pi + + return angle_x, angle_y, angle_z + + def calculate_angles(self, point1, point2): + import numpy as np + # 计算向量AB + AB = point2 - point1 + # 计算向量AB与x轴、y轴和z轴之间的夹角 + angle_x = np.degrees(np.arctan2(AB.y, AB.x)) + angle_y = np.degrees(np.arctan2(AB.z, np.sqrt(AB.x ** 2 + AB.y ** 2))) + angle_z = np.degrees(np.arctan2(np.sqrt(AB.x ** 2 + AB.y ** 2), AB.z)) + return angle_x, angle_y, angle_z + def on_ready(self): """ 事件绑定后,模拟器运行前会触发 @@ -95,13 +129,49 @@ class HalleyCometSim: # camera.clip_plane_near = 0.1 camera.clip_plane_far = 1000000 create_sphere_sky(scale=200000) + + WorldGrid().draw_axises(10) + application.time_scale = 5 self.orbit_lines = [] for body in self.bodies[1:]: if isinstance(body, HalleComet): continue print("create_orbit_line", body) - orbit_line = create_orbit_line(self.sun, body,rotation_x=70) + """ + # 在 ursina 中,我以 sun.position 为中心, body.position 的所在位置(x,y,z) 画一个圆环(orbit_line)。 + + 到 sun 中心点(x,y,z) 为半径,返回一个圆形轨道 orbit_line +orbit_line = create_orbit_line(sun, body) +# 帮我解决下面的问题,怎么修改下面的值,让 orbit_line 轨道线的倾斜和 body 匹配 +orbit_line.rotation_x = ? +orbit_line.rotation_y = ? +orbit_line.rotation_z = ? + +body.position - sun.position + + """ + + orbit_line = create_orbit_line(self.sun, body) + + angle_x, angle_y, angle_z = self.calculate_angles(self.sun.planet.position,body.planet.position) + # # 获取body相对于self.sun的位置向量 + # relative_position = body.planet.position - self.sun.planet.position + # + # rotation_x = -math.degrees( + # math.atan2(relative_position.y, math.sqrt(relative_position.x ** 2 + relative_position.z ** 2))) + # rotation_y = math.degrees(math.atan2(relative_position.x, relative_position.z)) + # # 计算旋转角度 + # orbit_line.rotation_x = rotation_x + 90 + # orbit_line.rotation_z = 0 + # orbit_line.rotation_y = rotation_y + 120 + # # + # angle = math.atan2(relative_position.y, relative_position.x) + orbit_line.rotation_x = angle_x + 90 + orbit_line.rotation_y = angle_y + 180 + orbit_line.rotation_z = angle_z + + orbit_line.body = body self.orbit_lines.append(orbit_line) @@ -155,4 +225,5 @@ if __name__ == '__main__': show_camera_info=False, show_control_info=False, show_timer=True, - show_grid=False) + show_grid=False + ) diff --git a/simulators/ursina/entities/world_grid.py b/simulators/ursina/entities/world_grid.py index 8938cb7..e2312f3 100644 --- a/simulators/ursina/entities/world_grid.py +++ b/simulators/ursina/entities/world_grid.py @@ -16,15 +16,21 @@ class WorldGrid(Singleton, Entity): 创建一个宇宙网格对象 """ - def draw_axises(self): + def draw_axises(self, scale_factor=1): """ 画坐标轴 @return: """ - arrow_x, line_x, text_x = create_arrow_line((0, 0, 0), (10, 0, 0), label="X", color=color.red) - arrow_y, line_y, text_y = create_arrow_line((0, 0, 0), (0, 10, 0), label="Y", color=color.green) - arrow_z, line_z, text_z = create_arrow_line((0, 0, 0), (0, 0, 10), label="Z", color=color.yellow) + len_scale = 0.5 * scale_factor + arrow_scale = 1 * scale_factor + text_scale = 50 * scale_factor + arrow_x, line_x, text_x = create_arrow_line((0, 0, 0), (10, 0, 0), label="X", len_scale=len_scale, + arrow_scale=arrow_scale, text_scale=text_scale, color=color.red) + arrow_y, line_y, text_y = create_arrow_line((0, 0, 0), (0, 10, 0), label="Y", len_scale=len_scale, + arrow_scale=arrow_scale, text_scale=text_scale, color=color.green) + arrow_z, line_z, text_z = create_arrow_line((0, 0, 0), (0, 0, 10), label="Z", len_scale=len_scale, + arrow_scale=arrow_scale, text_scale=text_scale, color=color.yellow) def __init__(self, position=None, scale=None): super().__init__() -- GitLab