Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Python_超人
宇宙模拟器
提交
0d1da2e1
宇宙模拟器
项目概览
Python_超人
/
宇宙模拟器
通知
19
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
宇宙模拟器
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
0d1da2e1
编写于
4月 22, 2023
作者:
三月三net
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Python超人-宇宙模拟器
上级
c7aa8388
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
135 addition
and
2 deletion
+135
-2
sim_scenes/science/comets_jupiter.py
sim_scenes/science/comets_jupiter.py
+105
-0
simulators/ursina/entities/entity_utils.py
simulators/ursina/entities/entity_utils.py
+29
-2
simulators/ursina_simulator.py
simulators/ursina_simulator.py
+1
-0
未找到文件。
sim_scenes/science/
jupiter_rocks
.py
→
sim_scenes/science/
comets_jupiter
.py
浏览文件 @
0d1da2e1
...
@@ -11,8 +11,38 @@ from objs import RockSnow, Rock, create_rock
...
@@ -11,8 +11,38 @@ from objs import RockSnow, Rock, create_rock
from
common.consts
import
SECONDS_PER_HOUR
,
SECONDS_PER_HALF_DAY
,
SECONDS_PER_DAY
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
from
sim_scenes.func
import
ursina_run
,
camera_look_at
,
two_bodies_colliding
from
simulators.ursina.entities.body_timer
import
TimeData
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
from
simulators.ursina.ursina_event
import
UrsinaEvent
def
create_comet
(
index
,
gravity_only_for
):
"""
随机生成石头
@param index: 索引号
@param gravity_only_for: 指定一个天体,石头的引力只对该天体有效
@return:
"""
# 随机生成石头的位置和初始速度信息
pos
=
[
-
r
*
random
.
randint
(
120
,
200
)
/
100
,
-
r
*
random
.
randint
(
120
,
200
)
/
1000
,
-
r
*
random
.
randint
(
100
,
300
)
/
100
]
# 随机速度
vel
=
[
0
,
-
random
.
randint
(
90
,
200
)
/
30
,
0
]
# 石头随机大小
size_scale
=
random
.
randint
(
600
,
1200
)
# 随机创建石头
comet
=
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
],
)
# 给石头一个随机旋转的方向和值
comet
.
rotation
=
[
0
,
0
,
0
]
comet
.
rotation
[
random
.
randint
(
0
,
2
)]
=
random
.
randint
(
90
,
200
)
/
100
return
comet
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
"""
"""
彗木相撞模拟
彗木相撞模拟
...
@@ -24,53 +54,47 @@ if __name__ == '__main__':
...
@@ -24,53 +54,47 @@ if __name__ == '__main__':
size_scale
=
1
,
texture
=
"jupiter_hd.jpg"
)
size_scale
=
1
,
texture
=
"jupiter_hd.jpg"
)
bodies
=
[
jupiter
]
bodies
=
[
jupiter
]
rock
s
=
[]
comet
s
=
[]
r
=
jupiter
.
raduis
r
=
jupiter
.
raduis
for
i
in
range
(
30
):
for
i
in
range
(
30
):
# 随机生成岩石位置和初始速度信息
# 随机生成石头
pos
=
[
-
r
*
random
.
randint
(
120
,
200
)
/
100
,
comet
=
create_comet
(
i
,
gravity_only_for
=
jupiter
)
-
r
*
random
.
randint
(
120
,
200
)
/
1000
,
bodies
.
append
(
comet
)
-
r
*
random
.
randint
(
100
,
300
)
/
100
]
comets
.
append
(
comet
)
# 随机速度
vel
=
[
0
,
-
random
.
randint
(
90
,
200
)
/
30
,
0
]
size_scale
=
random
.
randint
(
600
,
1200
)
rock
=
create_rock
(
no
=
i
%
8
+
1
,
name
=
f
'岩石
{
i
+
1
}
'
,
mass
=
4.4e10
,
size_scale
=
size_scale
,
color
=
(
255
,
200
,
0
),
init_position
=
pos
,
init_velocity
=
vel
,
gravity_only_for
=
[
jupiter
],
)
# 岩石随机旋转量
rock
.
rotation
=
[
0
,
0
,
0
]
rock
.
rotation
[
random
.
randint
(
0
,
2
)]
=
random
.
randint
(
90
,
200
)
/
100
bodies
.
append
(
rock
)
rocks
.
append
(
rock
)
def
on_reset
():
def
on_reset
():
for
rock
in
rocks
:
# 重置后恢复所有石头的状态(石头可见、引力有效)
rock
.
set_visible
(
True
)
for
comet
in
comets
:
rock
.
ignore_mass
=
False
comet
.
set_visible
(
True
)
comet
.
ignore_mass
=
False
def
on_ready
():
def
on_ready
():
# 运行前触发
# 创建太阳光
create_directional_light
(
position
=
(
200
,
0
,
-
300
),
target
=
jupiter
)
# 摄像机看向木星
camera_look_at
(
jupiter
,
rotation_z
=
0
)
camera_look_at
(
jupiter
,
rotation_z
=
0
)
def
on_timer_changed
(
time_data
:
TimeData
):
def
on_timer_changed
(
time_data
:
TimeData
):
for
rock
in
rocks
:
# 运行中,每时每刻都会触发
if
rock
.
visibled
:
for
comet
in
comets
:
rock
.
planet
.
rotation
+=
rock
.
rotation
if
comet
.
visibled
:
# 循环判断每个抛出物与木星是否相碰撞
# 如果是否可见,则旋转石头
if
two_bodies_colliding
(
rock
,
jupiter
):
comet
.
planet
.
rotation
+=
comet
.
rotation
# 岩石爆炸
# 循环判断每个石头与木星是否相碰撞,如果相碰撞就爆炸
rock
.
explode
(
jupiter
)
if
two_bodies_colliding
(
comet
,
jupiter
):
# 将石头隐藏、设置引力无效后,展示爆炸效果
comet
.
explode
(
jupiter
)
UrsinaEvent
.
on_reset_subscription
(
on_reset
)
UrsinaEvent
.
on_ready_subscription
(
on_ready
)
UrsinaEvent
.
on_timer_changed_subscription
(
on_timer_changed
)
# 订阅事件
UrsinaEvent
.
on_reset_subscription
(
on_reset
)
# 重置了会触发 on_reset
UrsinaEvent
.
on_ready_subscription
(
on_ready
)
# 运行前会触发 on_ready
UrsinaEvent
.
on_timer_changed_subscription
(
on_timer_changed
)
# 运行中,每时每刻都会触发 on_timer_changed
# 使用 ursina 查看的运行效果
# 使用 ursina 查看的运行效果
# 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹
# 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹
...
...
simulators/ursina/entities/entity_utils.py
浏览文件 @
0d1da2e1
...
@@ -9,7 +9,7 @@
...
@@ -9,7 +9,7 @@
# pip install -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com ursina
# 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
,
\
from
ursina
import
Ursina
,
window
,
Entity
,
Mesh
,
SmoothFollow
,
Texture
,
clamp
,
time
,
\
camera
,
color
,
mouse
,
Vec2
,
Vec3
,
Vec4
,
Text
,
\
camera
,
color
,
mouse
,
Vec2
,
Vec3
,
Vec4
,
Text
,
\
load_texture
,
held_keys
,
destroy
,
PointLight
,
distance
load_texture
,
held_keys
,
destroy
,
PointLight
,
DirectionalLight
,
distance
from
simulators.ursina.entities.body_trail
import
BodyTrail
,
BodyTrailLine
from
simulators.ursina.entities.body_trail
import
BodyTrail
,
BodyTrailLine
from
simulators.ursina.ursina_config
import
UrsinaConfig
from
simulators.ursina.ursina_config
import
UrsinaConfig
...
@@ -37,7 +37,7 @@ def create_name_text(parent):
...
@@ -37,7 +37,7 @@ def create_name_text(parent):
if
text_color
is
None
:
if
text_color
is
None
:
text_color
=
get_inverse_color
(
b_color
)
text_color
=
get_inverse_color
(
b_color
)
else
:
else
:
text_color
=
(
text_color
[
0
]
/
255
,
text_color
[
1
]
/
255
,
text_color
[
2
]
/
255
)
text_color
=
(
text_color
[
0
]
/
255
,
text_color
[
1
]
/
255
,
text_color
[
2
]
/
255
)
name_text
.
set_light_off
()
name_text
.
set_light_off
()
name_text
.
color
=
color
.
rgba
(
text_color
[
0
],
text_color
[
1
],
text_color
[
2
],
1
)
name_text
.
color
=
color
.
rgba
(
text_color
[
0
],
text_color
[
1
],
text_color
[
2
],
1
)
parent
.
name_text
=
name_text
parent
.
name_text
=
name_text
...
@@ -159,6 +159,33 @@ def create_trail_sphere(parent, pos):
...
@@ -159,6 +159,33 @@ def create_trail_sphere(parent, pos):
# return value, direction
# return value, direction
def
create_directional_light
(
position
,
target
=
None
,
shadows
=
False
,
light_color
=
None
):
"""
创建平行光(DirectionalLight)
@param position: 光源位置
@param target: 光源指向目标
@param shadows: 是否支持阴影
@param light_color: 光的颜色
@return:
"""
from
ursina.shaders
import
lit_with_shadows_shader
if
shadows
:
Entity
.
default_shader
=
lit_with_shadows_shader
if
light_color
is
None
:
light_color
=
color
.
white
else
:
light_color
=
color
.
rgba
(
light_color
[
0
]
/
255
,
light_color
[
1
]
/
255
,
light_color
[
2
]
/
255
,
1
)
light
=
DirectionalLight
(
position
=
position
,
intensity
=
10
,
range
=
10
,
color
=
light_color
)
if
target
is
not
None
:
if
hasattr
(
target
,
"planet"
):
if
hasattr
(
target
.
planet
,
"main_entity"
):
light
.
look_at
(
target
.
planet
.
main_entity
)
else
:
light
.
look_at
(
target
.
planet
)
else
:
light
.
look_at
(
target
)
def
create_trail_line
(
parent
,
pos
):
def
create_trail_line
(
parent
,
pos
):
"""
"""
在天体当前的位置创建一个拖尾球体
在天体当前的位置创建一个拖尾球体
...
...
simulators/ursina_simulator.py
浏览文件 @
0d1da2e1
...
@@ -105,6 +105,7 @@ class UrsinaSimulator(Simulator):
...
@@ -105,6 +105,7 @@ class UrsinaSimulator(Simulator):
position
=
body
.
planet
.
position
,
position
=
body
.
planet
.
position
,
scale
=
scale
,
fps
=
6
,
scale
=
scale
,
fps
=
6
,
loop
=
False
,
autoplay
=
True
)
loop
=
False
,
autoplay
=
True
)
explode_ani
.
set_light_off
()
if
target
is
not
None
:
if
target
is
not
None
:
if
hasattr
(
target
,
"planet"
):
if
hasattr
(
target
,
"planet"
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录