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

太阳系三体模拟器

上级 7076f48a
...@@ -75,7 +75,8 @@ def ursina_run(bodies, ...@@ -75,7 +75,8 @@ def ursina_run(bodies,
def callback_update(): def callback_update():
for ursina_view in simulator.ursina_views: for ursina_view in simulator.ursina_views:
simulator.check_and_evolve() simulator.check_and_evolve()
ursina_view.update() if ursina_view.appeared:
ursina_view.update()
# print('....') # print('....')
import sys import sys
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
# python_version :3.8 # python_version :3.8
# ============================================================================== # ==============================================================================
from bodies import Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto, Moon from bodies import Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto, Moon
from common.consts import SECONDS_PER_WEEK, SECONDS_PER_DAY from common.consts import SECONDS_PER_WEEK, SECONDS_PER_DAY, SECONDS_PER_YEAR
from scenes.func import mayavi_run, ursina_run from scenes.func import mayavi_run, ursina_run
if __name__ == '__main__': if __name__ == '__main__':
...@@ -36,4 +36,4 @@ if __name__ == '__main__': ...@@ -36,4 +36,4 @@ if __name__ == '__main__':
# mayavi_run(bodies, SECONDS_PER_WEEK, view_azimuth=-45) # mayavi_run(bodies, SECONDS_PER_WEEK, view_azimuth=-45)
# 使用 ursina 查看的运行效果 # 使用 ursina 查看的运行效果
ursina_run(bodies, SECONDS_PER_DAY, position=(0, 0, 0)) ursina_run(bodies, SECONDS_PER_YEAR, position=(0, 0, 0))
\ No newline at end of file \ No newline at end of file
# -*- coding:utf-8 -*-
# title :ursina UI组件
# description :ursina UI组件
# author :Python超人
# date :2023-02-11
# link :https://gitcode.net/pythoncr/
# python_version :3.8
# ==============================================================================
from ursina import Ursina, window, Entity, Grid, Mesh, camera, Text, application, color, mouse, Vec2, Vec3, \
load_texture, held_keys, Button
from ursina.prefabs.first_person_controller import FirstPersonController
from simulators.ursina.ursina_config import UrsinaConfig
from simulators.ursina.ursina_event import UrsinaEvent
from ursina import WindowPanel, InputField, Button, Slider, ButtonGroup
class UiSlider(Slider):
def __init__(self, text, min=0.01, max=3, default=1):
super().__init__(text=text,
height=Text.size,
y=-.6,
step=.01,
min=min,
max=max,
default=default,
color=color.rgba(0.0, 0.0, 0.0, 0.5))
self.label.scale = 1
self.height = Text.size/1.2
# self.text_entity
# self.update()
class SwithButton(ButtonGroup):
def __int__(self, options, default):
super().__init__(options, min_selection=1, y=0, default=default,
selected_color=color.green, ignore_paused=True,
color=color.rgba(0.0, 0.0, 0.0, 0.5))
self.label.scale = 0.8
\ No newline at end of file
...@@ -9,51 +9,32 @@ ...@@ -9,51 +9,32 @@
from ursina import Ursina, window, Entity, Grid, Mesh, camera, Text, application, color, mouse, Vec2, Vec3, \ from ursina import Ursina, window, Entity, Grid, Mesh, camera, Text, application, color, mouse, Vec2, Vec3, \
load_texture, held_keys, Button load_texture, held_keys, Button
from ursina.prefabs.first_person_controller import FirstPersonController from ursina.prefabs.first_person_controller import FirstPersonController
from simulators.ursina.ui_component import UiSlider, SwithButton
from simulators.ursina.ursina_config import UrsinaConfig from simulators.ursina.ursina_config import UrsinaConfig
from simulators.ursina.ursina_event import UrsinaEvent from simulators.ursina.ursina_event import UrsinaEvent
from ursina import WindowPanel, InputField, Button, Slider, ButtonGroup from ursina import WindowPanel, InputField, Button, Slider, ButtonGroup
class UrsinaUI: class UrsinaUI:
def __init__(self):
Text.default_font = 'simsun.ttc'
# self.pause_handler = Entity(ignore_paused=True) def ui_component_init(self):
# 加载中文字体文件 Text.default_font = 'simsun.ttc'
# text_time_scale = "1"
# self.text_time_scale_info = None
# self.pause_handler.input = self.pause_handler_input
# self.show_text_time_scale_info()
# key_info_str = "退出[按2次ESC] 方位控制[鼠标QWEASD] 开始暂停[空格] 控制倍率[Tab - +]"
# key_info = Text(text=key_info_str, position=(-0.8, 0.5), origin=(-1, 1), background=True)
# # self.show_button()
self.slider_body_spin_factor = Slider(text="自转速度", y=-.6, step=.01, min=0.01, max=3, default=1,
color=color.rgba(0.0, 0.0, 0.0, 0.5))
self.slider_run_speed_factor = Slider(text="运行速度", y=-.1, step=.01, min=0.01, max=500, default=1,
color=color.rgba(0.0, 0.0, 0.0, 0.5))
application.time_scale = 0.5 application.time_scale = 0.5
self.slider_control_speed_factor = Slider(text="控制速度", y=-.1, step=.01, min=0.01, max=30, default=0.5, self.slider_body_spin_factor = UiSlider(text='自转速度', min=0.01, max=30, default=1)
color=color.rgba(0.0, 0.0, 0.0, 0.5)) self.slider_run_speed_factor = UiSlider(text="运行速度", min=0.01, max=800, default=1)
self.slider_control_speed_factor = UiSlider(text="控制速度", min=0.01, max=30, default=application.time_scale)
self.slider_trail_length = UiSlider(text="拖尾长度", min=30, max=500, default=UrsinaConfig.trail_length)
self.slider_body_spin_factor.on_value_changed = self.on_slider_body_spin_changed self.slider_body_spin_factor.on_value_changed = self.on_slider_body_spin_changed
self.slider_run_speed_factor.on_value_changed = self.on_slider_run_speed_changed self.slider_run_speed_factor.on_value_changed = self.on_slider_run_speed_changed
self.slider_control_speed_factor.on_value_changed = self.on_slider_control_speed_changed self.slider_control_speed_factor.on_value_changed = self.on_slider_control_speed_changed
self.slider_trail_length = Slider(text="拖尾长度", y=-.1, step=1, min=30, max=500, default=UrsinaConfig.trail_length,
color=color.rgba(0.0, 0.0, 0.0, 0.5))
self.slider_trail_length.on_value_changed = self.on_slider_trail_length_changed self.slider_trail_length.on_value_changed = self.on_slider_trail_length_changed
self.on_off_switch = SwithButton(('||', '○'), default='○')
self.on_off_switch = ButtonGroup(('||', '○'), min_selection=1, y=0, default='○',
selected_color=color.green, ignore_paused=True,
color=color.rgba(0.0, 0.0, 0.0, 0.5))
self.on_off_switch.selected_color = color.red self.on_off_switch.selected_color = color.red
self.on_off_trail = ButtonGroup((' ', '...'), min_selection=1, y=0, default=' ', self.on_off_trail = SwithButton((' ', '...'), default=' ')
selected_color=color.green, ignore_paused=True,
color=color.rgba(0.0, 0.0, 0.0, 0.5))
self.on_off_trail.on_value_changed = self.on_off_trail_changed self.on_off_trail.on_value_changed = self.on_off_trail_changed
self.point_button = Button(text='寻找', origin=(0, 0), y=2, self.point_button = Button(text='寻找', origin=(0, 0), y=2,
...@@ -61,6 +42,7 @@ class UrsinaUI: ...@@ -61,6 +42,7 @@ class UrsinaUI:
self.reset_button = Button(text='重置', origin=(0, 0), y=2, self.reset_button = Button(text='重置', origin=(0, 0), y=2,
on_click=self.on_reset_button_click, color=color.rgba(0.0, 0.0, 0.0, 0.5)) on_click=self.on_reset_button_click, color=color.rgba(0.0, 0.0, 0.0, 0.5))
self.on_off_switch.on_value_changed = self.on_off_switch_changed self.on_off_switch.on_value_changed = self.on_off_switch_changed
wp = WindowPanel( wp = WindowPanel(
title='', title='',
content=( content=(
...@@ -79,9 +61,28 @@ class UrsinaUI: ...@@ -79,9 +61,28 @@ class UrsinaUI:
), ignore_paused=True, color=color.rgba(0.0, 0.0, 0.0, 0.5) ), ignore_paused=True, color=color.rgba(0.0, 0.0, 0.0, 0.5)
) )
wp.y = 0.5 # wp.panel.scale_y / 2 * wp.scale_y # center the window panel wp.y = 0.5 # wp.panel.scale_y / 2 * wp.scale_y # center the window panel
wp.x = -wp.scale_x wp.x = 0.6 # wp.scale_x + 0.1
# wp.x = 0#wp.panel.scale_x / 2 * wp.scale_x
self.wp = wp self.wp = wp
def __init__(self):
self.ui_component_init()
# self.pause_handler = Entity(ignore_paused=True)
# 加载中文字体文件
# text_time_scale = "1"
# self.text_time_scale_info = None
# self.pause_handler.input = self.pause_handler_input
# self.show_text_time_scale_info()
# key_info_str = "退出[按2次ESC] 方位控制[鼠标QWEASD] 开始暂停[空格] 控制倍率[Tab - +]"
# key_info = Text(text=key_info_str, position=(-0.8, 0.5), origin=(-1, 1), background=True)
# # self.show_button()
# slider_text = Text(text='自转速度', scale=1, position=(-0.6, 0.3))
# slider = Slider(scale=0.5, position=(-0.6, 0), min=0, max=10, step=1, text=slider_text)
def on_off_trail_changed(self): def on_off_trail_changed(self):
if self.on_off_trail.value == "...": if self.on_off_trail.value == "...":
UrsinaConfig.show_trail = True UrsinaConfig.show_trail = True
......
...@@ -19,7 +19,7 @@ from common.system import System ...@@ -19,7 +19,7 @@ from common.system import System
import time import time
import datetime import datetime
import math import math
from ursina import EditorCamera, PointLight, SpotLight, AmbientLight, DirectionalLight # , SunLight from ursina import EditorCamera, PointLight, SpotLight, AmbientLight, DirectionalLight
from scenes.func import ursina_run from scenes.func import ursina_run
...@@ -143,22 +143,7 @@ class UrsinaSimulator(Simulator): ...@@ -143,22 +143,7 @@ class UrsinaSimulator(Simulator):
return lights return lights
# def create_asteroids(self):
# """
# 小行星
# :return:
# """
# from simulators.views.ursina_mesh import create_torus,create_body_torus
# textureAsteroids = '../textures/asteroids.png'
# body_torus = create_torus(9, 10, 64)
# asteroids = Entity(model=body_torus, texture=textureAsteroids, scale=10, rotation=(90, 0, 0), double_sided=True)
# asteroids.x = 0
# asteroids.y = 0
# asteroids.z = 0
# asteroids.set_light_off()
def run(self, dt, **kwargs): def run(self, dt, **kwargs):
from ursina import EditorCamera, PointLight, SpotLight, AmbientLight, DirectionalLight
# 设定时间间隔为0.01秒 # 设定时间间隔为0.01秒
interval = 0.01 interval = 0.01
self.evolve_dt = dt * interval self.evolve_dt = dt * interval
...@@ -185,66 +170,9 @@ class UrsinaSimulator(Simulator): ...@@ -185,66 +170,9 @@ class UrsinaSimulator(Simulator):
if cosmic_bg is not None and os.path.exists(cosmic_bg): if cosmic_bg is not None and os.path.exists(cosmic_bg):
self.cosmic_background(cosmic_bg) self.cosmic_background(cosmic_bg)
# self.create_asteroids()
ui = UrsinaUI() ui = UrsinaUI()
# mouse.visible = True
EditorCamera(ignore_paused=True) EditorCamera(ignore_paused=True)
# pause_handler = Entity(ignore_paused=True)
# # 加载中文字体文件
# Text.default_font = 'simsun.ttc'
# # text_time_scale = "1"
# text_time_scale_info = None
#
# def show_text_time_scale_info():
# nonlocal text_time_scale_info
# if text_time_scale_info is not None:
# text_time_scale_info.disable()
# text_time_scale = "控制倍率:" + str(application.time_scale).ljust(4, " ")
# text_time_scale_info = Text(text=text_time_scale, position=(-0.8, 0.5), origin=(-1, 1), background=True)
#
# # 按空格键则暂停
# def pause_handler_input(key):
# nonlocal text_time_scale_info
# time_scales = [0.05, 0.1, 0.2, 0.5, 1, 5, 10, 20, 30]
# # print(key)
# if key == 'space':
# application.paused = not application.paused # Pause/unpause the game.
# elif key == 'tab':
# # application.time_scale 属性控制游戏时间流逝的速度。
# # 具体来说,它是一个浮点数,用于调整游戏时间流逝速度的比例,其默认值为 1.0,表示正常速度。
# # 当你将它设置为小于 1.0 的值时,游戏时间会变慢,而设置为大于 1.0 的值时,游戏时间则会变快。
# for idx, time_scale in enumerate(time_scales):
# if float(application.time_scale) == time_scale:
# if idx < len(time_scales) - 1:
# application.time_scale = time_scales[idx + 1]
# break
# else:
# application.time_scale = time_scales[0]
# elif key == '+':
# UrsinaConfig.run_speed_factor *= 2
# elif key == "= up":
# UrsinaConfig.body_spin_factor *= 2
# # if application.time_scale in time_scales:
# # idx = time_scales.index(application.time_scale)
# # if idx < len(time_scales) - 1:
# # application.time_scale = time_scales[idx + 1]
# elif key == '-':
# UrsinaConfig.run_speed_factor *= 0.5
# elif key == "- up":
# UrsinaConfig.body_spin_factor *= 0.5
# # if application.time_scale in time_scales:
# # idx = time_scales.index(application.time_scale)
# # if idx > 0:
# # application.time_scale = time_scales[idx - 1]
#
# show_text_time_scale_info()
#
# pause_handler.input = pause_handler_input
# show_text_time_scale_info()
# key_info_str = "退出[按2次ESC] 方位控制[鼠标QWEASD] 开始暂停[空格] 控制倍率[Tab - +]"
# key_info = Text(text=key_info_str, position=(-0.8, 0.5), origin=(-1, 1), background=True)
self.app.run() self.app.run()
......
...@@ -160,6 +160,7 @@ class Planet(Entity): ...@@ -160,6 +160,7 @@ class Planet(Entity):
try: try:
pos = self.position pos = self.position
except Exception as e: except Exception as e:
print(self.body_view.body)
self.destroy_all() self.destroy_all()
return return
trails_keys = self.trails.keys() trails_keys = self.trails.keys()
...@@ -226,6 +227,7 @@ class Planet(Entity): ...@@ -226,6 +227,7 @@ class Planet(Entity):
# 天体旋转 # 天体旋转
self.rotation_y -= self.rotspeed self.rotation_y -= self.rotspeed
except Exception as e: except Exception as e:
print(self.body_view.body)
self.destroy_all() self.destroy_all()
return return
...@@ -281,6 +283,7 @@ class Planet(Entity): ...@@ -281,6 +283,7 @@ class Planet(Entity):
# 删除拖尾 # 删除拖尾
for entity, pos in self.trails.items(): for entity, pos in self.trails.items():
destroy(entity) destroy(entity)
self.trails.clear()
def destroy_all(self): def destroy_all(self):
# 从天体系统中移除自己(TODO:暂时还不能移除) # 从天体系统中移除自己(TODO:暂时还不能移除)
...@@ -290,6 +293,8 @@ class Planet(Entity): ...@@ -290,6 +293,8 @@ class Planet(Entity):
# 如果有行星环,则删除行星环 # 如果有行星环,则删除行星环
if hasattr(self, "ring"): if hasattr(self, "ring"):
destroy(self.ring) destroy(self.ring)
self.body_view.body.appeared = False
self.body_view.appeared = False
# 最后删除自己 # 最后删除自己
destroy(self) destroy(self)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册