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

Python超人-宇宙模拟器

上级 4b91b8a3
...@@ -199,10 +199,19 @@ def find_texture(texture): ...@@ -199,10 +199,19 @@ def find_texture(texture):
""" """
if os.path.exists(texture): if os.path.exists(texture):
return texture return texture
paths = [os.path.join('.', 'textures'), os.path.join('..', 'textures'), os.path.join('..', '..', 'textures')] paths = [
os.path.join('..', '..', 'textures'),
os.path.join('..', '..', 'objs', 'textures'),
os.path.join('..', 'textures'),
os.path.join('..', 'objs', 'textures'),
os.path.join('.', 'textures'),
os.path.join('.', 'objs', 'textures')
]
for path in paths: for path in paths:
p = os.path.join(path, texture) p = os.path.join(path, texture)
if os.path.exists(p): if os.path.exists(p):
if p.endswith(".mtl"):
p = p[:-4]
return p return p
return "" return ""
......
...@@ -2,3 +2,5 @@ from objs.diamond import Diamond ...@@ -2,3 +2,5 @@ from objs.diamond import Diamond
from objs.football import Football from objs.football import Football
from objs.satellite import Satellite, Satellite2 from objs.satellite import Satellite, Satellite2
from objs.space_ship import SpaceShip from objs.space_ship import SpaceShip
from objs.rock_snow import RockSnow
from objs.rock import Rock, create_rock
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# title :地球 # title :钻石
# description :地球 # description :钻石
# author :Python超人 # author :Python超人
# date :2023-02-11 # date :2023-02-11
# link :https://gitcode.net/pythoncr/ # link :https://gitcode.net/pythoncr/
......
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# title : # title :
# description : # description :
# author :Python超人 # author :Python超人
# date :2023-02-11 # date :2023-02-11
# link :https://gitcode.net/pythoncr/ # link :https://gitcode.net/pythoncr/
......
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
...@@ -316,8 +316,8 @@ class Obj(metaclass=ABCMeta): ...@@ -316,8 +316,8 @@ class Obj(metaclass=ABCMeta):
return self.__density return self.__density
def __repr__(self): def __repr__(self):
return '<%s(%s)> m=%.3e(kg), d=%.3e(kg/m³), p=[%.3e,%.3e,%.3e](km), v=%s(km/s)' % \ return '<%s(%s):%s(%s)> m=%.3e(kg), d=%.3e(kg/m³), p=[%.3e,%.3e,%.3e](km), v=%s(km/s)' % \
(self.name, self.__class__.__name__, self.mass, self.density, (self.name, self.__class__.__name__, os.path.basename(self.model), os.path.basename(self.texture), self.mass, self.density,
self.position[0], self.position[1], self.position[2], self.velocity) self.position[0], self.position[1], self.position[2], self.velocity)
def ignore_gravity_with(self, body): def ignore_gravity_with(self, body):
......
# -*- coding:utf-8 -*-
# title :岩石
# description :岩石
# author :Python超人
# date :2023-02-11
# link :https://gitcode.net/pythoncr/
# python_version :3.8
# ==============================================================================
from objs.obj import Obj
class Rock(Obj):
"""
岩石
"""
def __init__(self, name="岩石", mass=5.97237e24,
init_position=[0, 0, 0],
init_velocity=[0, 0, 0],
texture="rock1.png", size_scale=1.0, distance_scale=1.0,
ignore_mass=False, density=1e3, color=(7, 0, 162),
trail_color=None, show_name=False,
model="rock1.obj", rotation=(0, 0, 0),
parent=None, gravity_only_for=[]):
params = {
"name": name,
"mass": mass,
"init_position": init_position,
"init_velocity": init_velocity,
"density": density,
"color": color,
"texture": texture,
"size_scale": size_scale,
"distance_scale": distance_scale,
"ignore_mass": ignore_mass,
"trail_color": trail_color,
"show_name": show_name,
"parent": parent,
"rotation": rotation,
"gravity_only_for": gravity_only_for,
"model": model
}
super().__init__(**params)
def create_rock(no: str = None, **kwargs):
if no is not None:
kwargs["model"] = f"rock{no}.obj"
kwargs["texture"] = f"rock{no}.png"
rock = Rock(**kwargs)
return rock
if __name__ == '__main__':
for i in range(10):
rock = create_rock(no=i % 7 + 1, name=f'岩石{i + 1}')
print(rock)
# -*- coding:utf-8 -*-
# title :带雪的岩石
# description :带雪的岩石
# author :Python超人
# date :2023-02-11
# link :https://gitcode.net/pythoncr/
# python_version :3.8
# ==============================================================================
from objs.obj import Obj
class RockSnow(Obj):
"""
带雪的岩石
"""
def __init__(self, name="雪岩", mass=5.97237e24,
init_position=[0, 0, 0],
init_velocity=[0, 0, 0],
texture="rock_snow.jpg", size_scale=1.0, distance_scale=1.0,
ignore_mass=False, density=1e3, color=(7, 0, 162),
trail_color=None, show_name=False,
model="rock_snow.obj", rotation=(0, 0, 0),
parent=None, gravity_only_for=[]):
params = {
"name": name,
"mass": mass,
"init_position": init_position,
"init_velocity": init_velocity,
"density": density,
"color": color,
"texture": texture,
"size_scale": size_scale,
"distance_scale": distance_scale,
"ignore_mass": ignore_mass,
"trail_color": trail_color,
"show_name": show_name,
"parent": parent,
"rotation": rotation,
"gravity_only_for": gravity_only_for,
"model": model
}
super().__init__(**params)
if __name__ == '__main__':
rock_snow = RockSnow()
print(rock_snow)
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# title :地球 # title :卫星
# description :地球 # description :卫星
# author :Python超人 # author :Python超人
# date :2023-02-11 # date :2023-02-11
# link :https://gitcode.net/pythoncr/ # link :https://gitcode.net/pythoncr/
......
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# title :地球 # title :太空飞船
# description :地球 # description :太空飞船
# author :Python超人 # author :Python超人
# date :2023-02-11 # date :2023-02-11
# link :https://gitcode.net/pythoncr/ # link :https://gitcode.net/pythoncr/
......
# -*- coding:utf-8 -*-
# title :岩石坠落地球
# description :岩石坠落地球
# author :Python超人
# date :2023-04-09
# link :https://gitcode.net/pythoncr/
# python_version :3.8
# ==============================================================================
from bodies import Moon, Earth, Body
from objs import RockSnow, Rock, create_rock
from common.consts import SECONDS_PER_HOUR, SECONDS_PER_HALF_DAY, SECONDS_PER_DAY, SECONDS_PER_WEEK, SECONDS_PER_MONTH
from sim_scenes.func import mayavi_run, ursina_run, camera_look_at, two_bodies_colliding
import math
import random
from simulators.ursina.entities.body_timer import TimeData
from simulators.ursina.ursina_config import UrsinaConfig
from simulators.ursina.ursina_event import UrsinaEvent
if __name__ == '__main__':
"""
岩石坠落地球
"""
# 地球在中心位置
earth = Earth(init_position=[0, 0, 0], size_scale=1, texture="earth_hd.jpg", init_velocity=[0, 0, 0])
bodies = [earth]
infos = [
{"position": [0, 0, 10002], "velocity": [2.3, 0, 0]},
{"position": [0, 0, -12000], "velocity": [1.75, 0, 0]},
{"position": [0, 8000, 0], "velocity": [1.05, 0, 0]},
{"position": [0, -12002, 0], "velocity": [1.75, 0, 0]},
{"position": [0, 18000, 0], "velocity": [1.05, 0, 0]},
{"position": [0, -22002, 0], "velocity": [1.75, 0, 0]},
{"position": [0, 0, 8002], "velocity": [0, 1.05, 0]},
{"position": [0, 0, -10000], "velocity": [0, 1.3, 0]},
]
for i, info in enumerate(infos):
rock = create_rock(no=i % 7 + 1, name=f'岩石{i + 1}', mass=4.4e10, size_scale=5e2, color=(255, 200, 0),
init_position=info["position"],
init_velocity=info["velocity"])
info["rock"] = rock
bodies.append(rock)
def on_ready():
camera_look_at(earth, rotation_z=0)
UrsinaConfig.trail_length = 150
UrsinaConfig.trail_type = "line"
pass
def on_timer_changed(time_data: TimeData):
for obj in bodies:
# 循环判断每个抛出物与地球是否相碰撞
if two_bodies_colliding(obj, earth):
# 如果抛出物与地球相碰撞了,则静止不动(抛出物停止并忽略引力)
obj.stop_and_ignore_gravity()
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 / 6,
position=(30000, 10000, -20000),
show_trail=True,
show_timer=True,
view_closely=0.001)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册