提交 2c8eb08f 编写于 作者: 三月三net's avatar 三月三net

Python超人-宇宙模拟器

上级 ce911879
......@@ -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 = {
......
......@@ -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)
......@@ -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²
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册