From 116598480071e400d5f6ba9481b7e6fb65f55cd4 Mon Sep 17 00:00:00 2001 From: march3 Date: Sun, 2 Apr 2023 12:35:14 +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 --- sim_scenes/solar_system/earth_moon6.py | 35 ++++++++ simulators/ursina/entities/body_trail.py | 93 +--------------------- simulators/ursina/entities/entity_utils.py | 57 ------------- simulators/ursina/entities/planet.py | 2 +- simulators/ursina/ursina_mesh.py | 2 +- simulators/ursina_simulator.py | 6 +- 6 files changed, 43 insertions(+), 152 deletions(-) create mode 100644 sim_scenes/solar_system/earth_moon6.py diff --git a/sim_scenes/solar_system/earth_moon6.py b/sim_scenes/solar_system/earth_moon6.py new file mode 100644 index 0000000..666fce2 --- /dev/null +++ b/sim_scenes/solar_system/earth_moon6.py @@ -0,0 +1,35 @@ +# -*- coding:utf-8 -*- +# title :地月场景模拟 +# description :地月场景模拟 +# author :Python超人 +# date :2023-02-11 +# link :https://gitcode.net/pythoncr/ +# python_version :3.8 +# ============================================================================== +from bodies import Sun, Earth, Moon +from common.consts import SECONDS_PER_HOUR, SECONDS_PER_HALF_DAY, SECONDS_PER_DAY, SECONDS_PER_WEEK, SECONDS_PER_MONTH +from sim_scenes.func import mayavi_run, ursina_run +from bodies.body import AU + +if __name__ == '__main__': + """ + 地球、6个月球 + """ + # 地球的Y方向初始速度 + EARTH_INIT_VELOCITY = 0 + # 地球在中心位置 + e = Earth(init_position=[0, 0, 0], texture="earth_hd.jpg", init_velocity=[0, 0, 0]) + bodies = [ + e, + Moon(init_position=[e.diameter, 0, 0], init_velocity=[0, 0, 0]), # 月球在地球前面 + Moon(init_position=[-e.diameter, 0, 0], init_velocity=[0, 0, 0]), # 月球在地球后面 + Moon(init_position=[0, e.diameter, 0], init_velocity=[0, 0, 0]), # 月球在地球左面 + Moon(init_position=[0, -e.diameter, 0], init_velocity=[0, 0, 0]), # 月球在地球右面 + Moon(init_position=[0, 0, e.diameter], init_velocity=[0, 0, 0]), # 月球在地球上面 + Moon(init_position=[0, 0, -e.diameter], init_velocity=[0, 0, 0]), # 月球在地球下面 + ] + + # 使用 ursina 查看的运行效果 + # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 + # position = 左-右+、上+下-、前+后- + ursina_run(bodies, 60, position=(0, 0, 0), show_trail=True, view_closely=0.001) diff --git a/simulators/ursina/entities/body_trail.py b/simulators/ursina/entities/body_trail.py index ae803b5..fe8fe90 100644 --- a/simulators/ursina/entities/body_trail.py +++ b/simulators/ursina/entities/body_trail.py @@ -9,7 +9,6 @@ # pip install -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com ursina from ursina import Entity, Mesh, Text, color, destroy, Vec3 -from simulators.ursina.ursina_config import UrsinaConfig from simulators.ursina.ursina_mesh import create_arrow_line @@ -23,12 +22,6 @@ class BodyTrail(Entity): ) def input(self, key): - # if self.hovered: - # if key == 'left mouse down': - # print(f"{self} was clicked!") - # self.text.enabled = True - # elif key == 'left mouse up': - # self.text.enabled = False if self.hovered: if key == 'left mouse down': # print(key, self) @@ -41,95 +34,11 @@ class BodyTrail(Entity): for c in self.children: destroy(c) return - # entity_infos = {"velocity": [vel_info, vel_direction, vel_position], - # "acceleration": [acc_info, acc_direction, acc_position]} + vel_info, vel_direction, vel_position = self.entity_infos["velocity"] acc_info, acc_direction, acc_position = self.entity_infos["acceleration"] - # - # verts_acc = [(0, 0, 0), tuple(acc_direction)] - # verts_vel = [(0, 0, 0), tuple(vel_direction)] - - # acc_arrow = Arrow(parent=self,from_pos=Vec3((0, 0, 0)), to_pos=Vec3(tuple(acc_direction)),color=color.yellow, alpha=0.5) - # acc_arrow.set_light_off() v_arrow, v_line, v_text = create_arrow_line((0, 0, 0), tuple(vel_direction), parent=self, label=vel_info, color=color.red, alpha=0.8, arrow_scale=0.5) a_arrow, a_line, a_text = create_arrow_line((0, 0, 0), tuple(acc_direction), parent=self, label=acc_info, color=color.yellow, alpha=0.8, arrow_scale=0.5) - - # acc_line = Entity(parent=self, model=Mesh(vertices=verts_acc, mode='line', thickness=3), - # color=color.yellow, alpha=0.5) - # acc_line.set_light_off() - # - # # vel_arrow = Arrow(parent=self,from_pos=Vec3((0, 0, 0)), to_pos=Vec3(tuple(vel_direction)),color=color.red, alpha=0.5) - # # vel_arrow.set_light_off() - # - # vel_line = Entity(parent=self, model=Mesh(vertices=verts_vel, mode='line', thickness=3), - # color=color.red, alpha=0.5) - # vel_line.set_light_off() - # - # vel_text = Text(vel_info, scale=50, billboard=True, parent=self, - # font=UrsinaConfig.CN_FONT, background=False, color=color.red, - # position=vel_position, alpha=0.5) - # vel_text.set_light_off() - # - # acc_text = Text(acc_info, scale=50, billboard=True, parent=self, - # font=UrsinaConfig.CN_FONT, background=False, color=color.yellow, - # position=acc_position, alpha=0.5) - # acc_text.set_light_off() - - -class Arrow(Entity): - def __init__(self, parent, from_pos=(0, 0, 0), to_pos=(1, 0, 0), **kwargs): - from_pos = to_pos / 2 - super().__init__(parent=parent, model='arrow', position=from_pos, **kwargs) - # self.x = -pos[1] - # self.y = pos[2] - # self.z = pos[0] - to_pos = 1000 * to_pos - # to_pos = -to_pos[2],-to_pos[1],to_pos[0] - # to_pos = -to_pos[2],-to_pos[0],to_pos[1] - # to_pos = to_pos[0],to_pos[2],to_pos[1] - # to_pos = -to_pos[0],to_pos[1],to_pos[2] - # to_pos = to_pos[1], -to_pos[0], -to_pos[2] - # to_pos = to_pos[1], -to_pos[0], -to_pos[2] - self.rotation = (0, 0, 0) - self.look_at(to_pos) - print(self.rotation) - # self.model = Mesh(vertices=[ - # from_pos, - # from_pos + (to_pos - from_pos) * 0.9, - # from_pos + (to_pos - from_pos) * 0.9 + Vec3(0, 0.1, 0), - # to_pos, - # from_pos + (to_pos - from_pos) * 0.9 - Vec3(0, 0.1, 0), - # from_pos + (to_pos - from_pos) * 0.9 - # ], mode='triangle', thickness=thickness) - # - # self.color = color - # self.look_at(to_pos) - # self.scale_z = (to_pos - from_pos).length() - -# class Arrow(Entity): -# def __init__(self,parent, from_pos, to_pos, **kwargs): -# super().__init__(parent=parent, model='arrow', **kwargs) -# self.position = from_pos -# self.look_at(to_pos) - - -# import numpy as np -# def draw_arrow(parent, from_pos, to_pos, color, alpha): -# # 计算方向向量和长度 -# p1 = np.array(from_pos) -# p2 = np.array(to_pos) -# direction = p2 - p1 -# length = np.linalg.norm(direction) -# -# # 创建箭头实体 -# arrow = Entity(parent=parent, model="arrow", scale=(1, 1, length),color=color,alpha=alpha) -# -# # 设置箭头位置和方向 -# arrow_position = p1 + direction/2 -# arrow.position = arrow_position -# arrow.look_at(p2) -# -# return arrow diff --git a/simulators/ursina/entities/entity_utils.py b/simulators/ursina/entities/entity_utils.py index c4fb683..5d7b577 100644 --- a/simulators/ursina/entities/entity_utils.py +++ b/simulators/ursina/entities/entity_utils.py @@ -255,9 +255,6 @@ def create_trail_info(body, trail): acc_direction = acceleration[1] acc_direction = np.array(acc_direction) * 2 - verts_acc = [(0, 0, 0), tuple(acc_direction)] - verts_vel = [(0, 0, 0), tuple(vel_direction)] - vel_position = vel_direction vel_position = (vel_position[0], vel_position[1], vel_position[2]) @@ -267,57 +264,3 @@ def create_trail_info(body, trail): trail.entity_infos = {"velocity": [vel_info, vel_direction, vel_position], "acceleration": [acc_info, acc_direction, acc_position]} - -def create_trail_text_xxx(body, trail): - velocity = merge_vectors(body.velocity) - acceleration = merge_vectors(body.acceleration) - vel_info = "%.2fkm/s" % (velocity[0]) - acc_m = acceleration[0] * 1000 - - if acc_m >= 0.01: - acc_info = "%.2fm/s²" % (acc_m) - else: - acc_info = "%.2fmm/s²" % (acc_m * 1000) - - vel_direction = velocity[1] - vel_direction = np.array(vel_direction) * 5 - - acc_direction = acceleration[1] - acc_direction = np.array(acc_direction) * 2 - # acc_direction = np.array(acc_direction)*UrsinaConfig.SCALE_FACTOR - # vertsyz = [tuple(body.position),tuple(acc_direction)] # [(0, 0, 0), (0, 10, 0), (0, 0, 0), (0, 0, 10)] - verts_acc = [(0, 0, 0), tuple(acc_direction)] - acc_line = Entity(parent=trail, model=Mesh(vertices=verts_acc, mode='line', thickness=3), - color=color.yellow, alpha=0.5) - acc_line.set_light_off() - - verts_vel = [(0, 0, 0), tuple(vel_direction)] - vel_line = Entity(parent=trail, model=Mesh(vertices=verts_vel, mode='line', thickness=3), - color=color.red, alpha=0.5) - vel_line.set_light_off() - - vel_position = vel_direction - vel_position = (vel_position[0], vel_position[1], vel_position[2]) - - vel_text = Text(vel_info, scale=50, billboard=True, parent=trail, - font=UrsinaConfig.CN_FONT, background=False, color=color.red, - position=vel_position, alpha=0.5) - vel_text.set_light_off() - acc_position = acc_direction - acc_position = (acc_position[0], acc_position[1], acc_position[2]) - - acc_text = Text(acc_info, scale=50, billboard=True, parent=trail, - font=UrsinaConfig.CN_FONT, background=False, color=color.yellow, - position=acc_position, alpha=0.5) - acc_text.set_light_off() - # self.name_text.background.color = color.rgba(b_color[0], b_color[1], b_color[2], 0.3) - # # self.name_text.scale = self.scale - # inverse_color = get_inverse_color(b_color) - # self.name_text.color = color.rgba(inverse_color[0], inverse_color[1], inverse_color[2], 1) - # acc_line.enabled = False - # vel_line.enabled = False - # vel_text.enabled = False - # acc_text.enabled = False - - trail.entity_infos = [acc_line, vel_line, vel_text, acc_text] - return acc_line, vel_line, vel_text, acc_text diff --git a/simulators/ursina/entities/planet.py b/simulators/ursina/entities/planet.py index 185bdee..1a38e7e 100644 --- a/simulators/ursina/entities/planet.py +++ b/simulators/ursina/entities/planet.py @@ -62,7 +62,7 @@ class Planet(Entity): subdivisions = self.body.resolution model = create_sphere(0.5, subdivisions) - rotation = (0, self.body.rotate_angle,0) + rotation = (0, 0, 0) UrsinaEvent.on_reset_subscription(self.on_reset) diff --git a/simulators/ursina/ursina_mesh.py b/simulators/ursina/ursina_mesh.py index 41c1010..1e595c3 100644 --- a/simulators/ursina/ursina_mesh.py +++ b/simulators/ursina/ursina_mesh.py @@ -307,7 +307,7 @@ if __name__ == '__main__': # arrow = Entity(model=create_arrow(), color=color.yellow) # arrow.set_light_off() - arrow, line = create_arrow_line((0, 0, 0), (10, 0, 0)) + arrow, line, text = create_arrow_line((0, 0, 0), (10, 0, 0)) EditorCamera() app.run() diff --git a/simulators/ursina_simulator.py b/simulators/ursina_simulator.py index 9fd1fd3..28394f5 100644 --- a/simulators/ursina_simulator.py +++ b/simulators/ursina_simulator.py @@ -37,7 +37,11 @@ class WorldGrid(Entity): """ def draw_axises(self): - # 坐标轴 + """ + 画坐标轴 + @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) -- GitLab