From 2c8eb08f7cc01efc93e81a90d8a4729347683fe4 Mon Sep 17 00:00:00 2001 From: march3 Date: Sun, 16 Apr 2023 19:36:32 +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 --- objs/satellite.py | 2 +- sim_scenes/earth/earth_satellites.py | 17 +++++++--- simulators/ursina/entities/entity_utils.py | 36 +++++++++++----------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/objs/satellite.py b/objs/satellite.py index 311e758..f20dc39 100644 --- a/objs/satellite.py +++ b/objs/satellite.py @@ -19,7 +19,7 @@ class Satellite(Obj): init_velocity=[0, 0, 0], texture="satelite.png", size_scale=1.0, distance_scale=1.0, ignore_mass=False, density=1e3, color=(7, 0, 162), - trail_color=None, show_name=False, + trail_color=(255, 255, 255), show_name=False, model="satelite.obj", parent=None, gravity_only_for=[]): params = { diff --git a/sim_scenes/earth/earth_satellites.py b/sim_scenes/earth/earth_satellites.py index 2e7dc9a..943ac5e 100644 --- a/sim_scenes/earth/earth_satellites.py +++ b/sim_scenes/earth/earth_satellites.py @@ -103,12 +103,12 @@ if __name__ == '__main__': # 北斗卫星高度为2.13-2.15万千米。GPS卫星平均轨道高度2.02万千米。 bodies = [earth] satellite_infos = [ - {"position": [0, 0, 10000], "velocity": [-6.3, 0, 0]}, + {"position": [0, 0, 10002], "velocity": [6.3, 0, 0]}, {"position": [0, 0, -12000], "velocity": [5.75, 0, 0]}, {"position": [0, 8000, 0], "velocity": [7.05, 0, 0]}, - {"position": [0, -12000, 0], "velocity": [-5.75, 0, 0]}, - {"position": [0, 0, 8000], "velocity": [0, 7.05, 0]}, - {"position": [0, 0, -10000], "velocity": [0, -6.3, 0]}, + {"position": [0, -12002, 0], "velocity": [5.75, 0, 0]}, + {"position": [0, 0, 8002], "velocity": [0, 7.05, 0]}, + {"position": [0, 0, -10000], "velocity": [0, 6.3, 0]}, ] for i, info in enumerate(satellite_infos): # altitude = random.randint(4000, 10000) @@ -117,6 +117,7 @@ if __name__ == '__main__': satellite = Satellite(name=f'卫星{i + 1}', mass=4.4e10, size_scale=2e2, color=(255, 200, 0), init_position=info["position"], init_velocity=info["velocity"]) + info["satellite"] = satellite bodies.append(satellite) @@ -126,13 +127,19 @@ if __name__ == '__main__': UrsinaConfig.trail_type = "line" pass + def on_timer_changed(time_data: TimeData): + for info in satellite_infos: + info["satellite"].planet.look_at(earth.planet) + UrsinaEvent.on_ready_subscription(on_ready) + UrsinaEvent.on_timer_changed_subscription(on_timer_changed) # 使用 ursina 查看的运行效果 # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 # position = 左-右+、上+下-、前+后- ursina_run(bodies, SECONDS_PER_HOUR / 2, - position=(30000, 5000, -30000), + position=(30000, 10000, -20000), show_trail=True, + show_timer=True, view_closely=0.001) diff --git a/simulators/ursina/entities/entity_utils.py b/simulators/ursina/entities/entity_utils.py index 40b6baf..0f603d7 100644 --- a/simulators/ursina/entities/entity_utils.py +++ b/simulators/ursina/entities/entity_utils.py @@ -9,7 +9,7 @@ # pip install -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com ursina from ursina import Ursina, window, Entity, Mesh, SmoothFollow, Texture, clamp, time, \ camera, color, mouse, Vec2, Vec3, Vec4, Text, \ - load_texture, held_keys, destroy, PointLight + load_texture, held_keys, destroy, PointLight, distance from simulators.ursina.entities.body_trail import BodyTrail, BodyTrailLine from simulators.ursina.ursina_config import UrsinaConfig @@ -143,14 +143,14 @@ def create_trail_sphere(parent, pos): return trail -def merge_vectors(vectors): - # 计算速度的大小 - x, y, z = vectors[0], vectors[1], vectors[2] - value = math.sqrt(x ** 2 + y ** 2 + z ** 2) - # 计算速度的方向 - direction = (x / value, y / value, z / value) - # 返回速度大小和速度方向 - return value, direction +# def merge_vectors(vectors): +# # 计算速度的大小 +# x, y, z = vectors[0], vectors[1], vectors[2] +# value = math.sqrt(x ** 2 + y ** 2 + z ** 2) +# # 计算速度的方向 +# direction = (x / value, y / value, z / value) +# # 返回速度大小和速度方向 +# return value, direction def create_trail_line(parent, pos): @@ -159,16 +159,16 @@ def create_trail_line(parent, pos): @param pos: @return: """ + trail = None if hasattr(parent, "trail_last_pos"): trail_last_pos = parent.trail_last_pos - value, direction = merge_vectors(pos - trail_last_pos) - trail = BodyTrailLine(color=parent.trail_color, scale=parent.trail_scale, position=trail_last_pos, - direction=Vec3(direction)) - trail.set_light_off() + if distance(pos, trail_last_pos) > 0: + value, direction = get_value_direction_vectors(pos - trail_last_pos) + trail = BodyTrailLine(color=parent.trail_color, scale=parent.trail_scale, position=trail_last_pos, + direction=Vec3(direction)) + trail.set_light_off() parent.last_trail = trail - else: - trail = None parent.trail_last_pos = pos return trail @@ -261,7 +261,7 @@ def create_fixed_star_lights(fixed_star): light = PointLight(parent=fixed_star, intensity=10, range=10, color=color.white) -def merge_vectors(vectors): +def get_value_direction_vectors(vectors): # 计算速度的大小 x, y, z = vectors[0], vectors[1], vectors[2] value = math.sqrt(x ** 2 + y ** 2 + z ** 2) @@ -276,8 +276,8 @@ def merge_vectors(vectors): def create_trail_info(body, trail): - velocity = merge_vectors(body.velocity) - acceleration = merge_vectors(body.acceleration) + velocity = get_value_direction_vectors(body.velocity) + acceleration = get_value_direction_vectors(body.acceleration) vel_value = velocity[0] # km/s acc_value = acceleration[0] # km/s² -- GitLab