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

Python超人-宇宙模拟器

上级 62ca2e93
......@@ -74,9 +74,9 @@ class FixedStar(Body):
# save_file = os.path.join(temp_dir, "%s_%s.png" % (texture_name, "_".join([str(i) for i in list(self.color)])))
save_file = os.path.join(temp_dir, "%s_%s.png" % (texture_name, str(self.__class__.__name__).lower()))
if os.path.exists(save_file):
return save_file
if self.__class__ is not FixedStar:
if os.path.exists(save_file):
return save_file
fixed_star_img = os.path.join(texture_path, texture)
gen_fixed_star_texture(self.color,
......
# -*- coding:utf-8 -*-
# title :组成文本的星球群(自定义星球)
# description :组成文本的星球群(自定义星球)
# author :Python超人
# date :2023-03-26
# link :https://gitcode.net/pythoncr/
# python_version :3.8
# ==============================================================================
from bodies import FixedStar, ColorBody, Earth, Sun
from common.consts import SECONDS_PER_HOUR, SECONDS_PER_DAY, SECONDS_PER_WEEK
from sim_scenes.func import ursina_run, create_sphere_sky
from sim_scenes.funny.utils.body_utils import gen_bodies_from_image
from simulators.ursina.ursina_event import UrsinaEvent
D = 6000 # 基本距离单位:km(随意赋值)
def create_earth(name, texture, scale):
earth = Earth(name=name, texture=texture, rotate_angle=-23.44,
mass=2e32, size_scale=5 * scale,
init_position=[-450 * D, 100 * D, 6000 * D], # [ 左-右+, 上+下-, 远+近- ]
init_velocity=[0, 0, 0],
ignore_mass=True)
return earth
def show_text_bodies():
"""
显示图片文本的星球群
"""
# 观看摄像机的位置
# camera_pos = (左-右+, 上+下-, 前+后-)
camera_pos = (D, D, -3000 * D)
# 根据 pixel_image 指定图片生成有色星球(注意:图片的像素不要太多)
bodies = gen_bodies_from_image(
pixel_image="./images/ilovechina.png",
texture="color_moon.png",
params={"camera_pos": camera_pos}, light_disable=False)
# 放一个恒星作为背景
# TODO: ignore_mass=True
# 注意:这里的算法是基于牛顿的万有引力(质量为0不受引力的影响在天体物理学中是不严谨)
sun = FixedStar(init_position=[camera_pos[0], camera_pos[1], camera_pos[2] * 30],
texture="fixed_star.png", color=(255, 255, 225), size_scale=20)
# glows = (glow_num:10, glow_scale:1.03 glow_alpha:0.1~1 glow_color_min_val)
sun.glows = (10, 1.05, 0.1)
bodies.append(sun)
earth = create_earth(name="背景地球", texture="earth-huge.jpg", scale=1)
earth_cn = create_earth(name="背景地球", texture="earth-huge-cn-flag.png", scale=1.01)
earth_clouds = create_earth(name="地球云层", texture="transparent_clouds.png", scale=1.005)
bodies.append(earth)
bodies.append(earth_cn)
bodies.append(earth_clouds)
def on_ready():
# from simulators.ursina.entities.entity_utils import create_directional_light
# # 运行前触发
# # 为了较好的立体效果,可以增加太阳光线,光线指向木星(target=jupiter)
# create_directional_light(position=(200, 0, -300), light_num=3, target=earth)
from ursina import camera
# camera.fov = 40
sky = create_sphere_sky(scale=800000)
sky.rotation_y = 180
# sky.rotation_x = 20
# sky.rotation_z = -65
UrsinaEvent.on_ready_subscription(on_ready)
# 使用 ursina 查看的运行效果
# 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹
# position = camera_pos = (左-右+, 上+下-, 前+后-)
ursina_run(bodies, SECONDS_PER_HOUR,
cosmic_bg='',
show_grid=False,
position=camera_pos, view_closely=True)
if __name__ == '__main__':
show_text_bodies()
......@@ -29,7 +29,7 @@ def get_scaled_body_pos(camera_pos, body_pos, scale_factor):
return new_pos
def gen_bodies_from_image(pixel_image, params, texture="color_body.png"):
def gen_bodies_from_image(pixel_image, params, texture="color_body.png", light_disable=True):
"""
根据像素图片以及参数,自动生成星球,注意图片像素不能太多,否则会导致电脑运行太慢
@param pixel_image:
......@@ -63,7 +63,7 @@ def gen_bodies_from_image(pixel_image, params, texture="color_body.png"):
interval_factor = 20 # 星球间距因子
body_template = 'ColorBody(name="%s", mass=mass, color=(%d, %d, %d), size_scale=%.4f, ' \
'init_position=get_position([-%g * D, %g * D, 0], %.4f), ' \
f'init_velocity=[0, 0, 0], ignore_mass=True, texture="{texture}").set_light_disable(True)'
f'init_velocity=[0, 0, 0], ignore_mass=True, texture="{texture}").set_light_disable({light_disable})'
bodies_str = "["
# 以图片像素为坐标,对角线的距离
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册