提交 30b8ddd2 编写于 作者: 三月三net's avatar 三月三net

Python超人-宇宙模拟器

上级 29783e3a
# -*- coding:utf-8 -*-
# title :彗木相撞模拟
# description :天象奇观:彗木相撞模拟
# author :Python超人
# date :2023-11-28
# link :https://gitcode.net/pythoncr/
# python_version :3.8
# ==============================================================================
from bodies import Jupiter, Body
from objs import RockSnow, Rock, create_rock
from common.consts import SECONDS_PER_HOUR, SECONDS_PER_HALF_DAY, SECONDS_PER_DAY
from sim_scenes.func import ursina_run, camera_look_at, two_bodies_colliding, create_sphere_sky
from sim_scenes.universe_sim_scenes import UniverseSimScenes
from simulators.ursina.entities.body_timer import TimeData
from simulators.ursina.entities.entity_utils import create_directional_light
from simulators.ursina.ursina_event import UrsinaEvent
import random
def create_comet(index, radius, gravity_only_for):
"""
随机生成石头(随机位置、随机初始速度、随机大小、随机旋转)
@param index: 索引号
@param radius: 木星的半径,保证生成的石头在木星半径外
@param gravity_only_for: 指定一个天体,石头只对该天体引力有效
@return:
"""
# 随机生成石头的位置和初始速度信息
pos = [-radius * random.randint(120, 200) / 100,
-radius * random.randint(120, 200) / 1000,
-radius * random.randint(100, 300) / 100]
# 随机速度
vel = [0, -random.randint(90, 200) / 30, -random.randint(0, 50)]
# 石头随机大小
size_scale = random.randint(600, 1200)
# 随机创建石头
rock = create_rock(
no=index % 8 + 1, name=f'岩石{index + 1}', mass=4.4e10,
size_scale=size_scale, color=(255, 200, 0),
init_position=pos, init_velocity=vel, gravity_only_for=[gravity_only_for],
)
# 给石头一个随机旋转的方向和值
rock.rotation = [0, 0, 0]
rock.rotation[random.randint(0, 2)] = random.randint(90, 200) / 100
return rock
if __name__ == '__main__':
"""
彗木相撞模拟
"""
# 木星在中心位置
jupiter = Jupiter(init_position=[0, 0, 0], init_velocity=[0, 0, 0],
size_scale=1, texture="jupiter_hd.jpg")
bodies = [jupiter]
comets = []
for i in range(50):
# 随机生成石头
comet = create_comet(i, jupiter.radius, gravity_only_for=jupiter)
bodies.append(comet)
comets.append(comet)
def on_timer_changed(time_data: TimeData):
# 运行中,每时每刻都会触发
for comet in comets:
if comet.visibled:
# 如果是否可见,则旋转石头
comet.planet.rotation += comet.rotation
# 循环判断每个石头与木星是否相碰撞,如果相碰撞就爆炸
if two_bodies_colliding(comet, jupiter):
# 将石头隐藏、设置引力无效后,展示爆炸效果
comet.explode(jupiter)
def on_reset():
"""
当按键盘的 “O” 键重置后,恢复所有石头的状态(石头可见、引力有效),这样就可以反复观看
@return:
"""
for comet in comets:
comet.set_visible(True)
comet.ignore_mass = False
def on_ready():
# 运行前触发
create_sphere_sky(scale=2000)
# 为了较好的立体效果,可以增加太阳光线,光线指向木星(target=jupiter)
create_directional_light(position=(200, 0, -300), light_num=3, target=jupiter)
# 摄像机看向木星
camera_look_at(jupiter, rotation_z=0)
# 订阅事件后,上面3个函数功能才会起作用
# 按键盘的 “O” 重置键会触发 on_reset
UrsinaEvent.on_reset_subscription(on_reset)
# 运行前会触发 on_ready
UrsinaEvent.on_ready_subscription(on_ready)
# 运行中,每时每刻都会触发 on_timer_changed
UrsinaEvent.on_timer_changed_subscription(on_timer_changed)
UniverseSimScenes.set_window_size((1920, 1079), False)
# 使用 ursina 查看的运行效果
# 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹
# position = 左-右+、上+下-、前+后-
ursina_run(bodies, SECONDS_PER_HOUR / 10,
position=(0, 0, -300000),
show_exit_button=False,
show_camera_info=False,
show_control_info=False,
show_grid=False,
timer_enabled=True,
cosmic_bg='',
# show_timer=True,
view_closely=0.001)
# -*- coding:utf-8 -*-
# title :地球3D效果
# description :地球3D效果
# author :Python超人
# date :2023-11-28
# link :https://gitcode.net/pythoncr/
# python_version :3.8
# ==============================================================================
from bodies import Earth
from common.consts import SECONDS_PER_HOUR
from sim_scenes.func import ursina_run, create_sphere_sky
from sim_scenes.universe_sim_scenes import UniverseSimScenes
from simulators.ursina.ursina_event import UrsinaEvent
if __name__ == '__main__':
"""
地球3D效果(透视3D地球)
"""
# 创建带有云层的地球
earth = Earth(
# texture="earth_hd.jpg",
texture="earth_hd_trans.png",
rotate_angle=-23.44,
init_position=[0, 0, 0],
init_velocity=[0, 0, 0],
size_scale=1)
# 创建云层(texture纹理图使用了透明云层的图片,云层的 size_scale 要稍微比地球大一点)
clouds = Earth(name="地球云层", texture="transparent_clouds.png",
rotate_angle=-23.44,
size_scale=1.001, parent=earth)
bodies = [earth, clouds]
# TODO: 开启3D摄像机
from simulators.ursina.entities.camera3d import Camera3d
# 3D摄像机初始化(两眼到鼻梁的距离为1000公里效果)
Camera3d.init(eye_distance=1000)
# 录制:
# sim_video_3d_cap.bat featured earth_3d earth_3d.mp4
def on_ready():
# 运行前触发
create_sphere_sky(scale=2000)
def on_timer_changed(time_data):
print(earth.planet.rotation_y)
# 地球转了一圈(360)就停止,370是留些余量
if abs(earth.planet.rotation_y) > 365:
exit(0)
UniverseSimScenes.set_window_size((1920, 1079), False)
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,
SECONDS_PER_HOUR,
position=(1.45 * earth.radius, 0, -30000),
cosmic_bg="", # 无背景(黑色)
show_grid=False, # 不显示网格
show_camera_info=False, # 不显示摄像机信息
show_control_info=False, # 不显示控制提示信息
timer_enabled=True,
view_closely=0.001) # 近距离观看 view_closely=True或0.001
......@@ -42,6 +42,8 @@ def get_args():
if args.start_index > 0:
args.save_name = args.save_name + ("_%s" % args.start_index)
print("save_name:", args.save_name)
if args.save_name == '':
args.save_name = 'video.mp4'
return args
......@@ -295,7 +297,7 @@ def make_3d_video():
if img is None:
return None
_3d_card = img[4:20, 3:20, ]
_3d_card_p = _3d_card[10, 10, ]
_3d_card_p = _3d_card[10, 10,]
index = int(_3d_card_p[1]) + int(_3d_card_p[0])
# if index in r_frames.keys() and index in l_frames.keys():
......@@ -366,7 +368,7 @@ def make_3d_video():
clear_frame_temp_files()
# crop('video.mp4')
print("视频保存完成")
print(args.save_name)
print(os.path.abspath(args.save_name))
if __name__ == '__main__':
......
......@@ -34,6 +34,8 @@ def get_args():
print("fps:", args.fps)
print("save_name:", args.save_name)
print("wait:", args.wait)
if args.save_name == '':
args.save_name = 'video.mp4'
return args
......@@ -183,4 +185,4 @@ if __name__ == '__main__':
cv2.destroyAllWindows()
# crop('video.mp4')
print("视频保存完成时间:", get_current_time())
print(args.save_name)
print(os.path.abspath(args.save_name))
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册