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

Python超人-宇宙模拟器

上级 4897c35d
from objs.diamond import Diamond from objs.diamond import Diamond
from objs.football import Football from objs.football import Football
from objs.satellite import Satellite from objs.satellite import Satellite, Satellite2
from objs.space_ship import SpaceShip from objs.space_ship import SpaceShip
此差异已折叠。
...@@ -17,10 +17,43 @@ class Satellite(Obj): ...@@ -17,10 +17,43 @@ class Satellite(Obj):
def __init__(self, name="卫星", mass=5.97237e24, def __init__(self, name="卫星", mass=5.97237e24,
init_position=[0, 0, 0], init_position=[0, 0, 0],
init_velocity=[0, 0, 0], init_velocity=[0, 0, 0],
texture="satelite.png", size_scale=1.0, distance_scale=1.0, texture="satellite.png", size_scale=1.0, distance_scale=1.0,
ignore_mass=False, density=1e3, color=(7, 0, 162), ignore_mass=False, density=1e3, color=(7, 0, 162),
trail_color=(255, 255, 255), show_name=False, trail_color=(255, 255, 255), show_name=False,
model="satelite.obj", model="satellite.obj",
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,
"gravity_only_for": gravity_only_for,
"model": model
}
super().__init__(**params)
class Satellite2(Obj):
"""
卫星
"""
def __init__(self, name="卫星", mass=5.97237e24,
init_position=[0, 0, 0],
init_velocity=[0, 0, 0],
texture="satellite2.jpg", size_scale=1.0, distance_scale=1.0,
ignore_mass=False, density=1e3, color=(7, 0, 162),
trail_color=(255, 255, 255), show_name=False,
model="satellite2.obj",
parent=None, gravity_only_for=[]): parent=None, gravity_only_for=[]):
params = { params = {
"name": name, "name": name,
...@@ -43,5 +76,5 @@ class Satellite(Obj): ...@@ -43,5 +76,5 @@ class Satellite(Obj):
if __name__ == '__main__': if __name__ == '__main__':
satellite = Satellite() satellite = Satellite2()
print(satellite) print(satellite)
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
# python_version :3.8 # python_version :3.8
# ============================================================================== # ==============================================================================
from bodies import Moon, Earth, Body from bodies import Moon, Earth, Body
from objs import Satellite from objs import Satellite, Satellite2
from common.consts import SECONDS_PER_HOUR, SECONDS_PER_HALF_DAY, SECONDS_PER_DAY, SECONDS_PER_WEEK, SECONDS_PER_MONTH 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 from sim_scenes.func import mayavi_run, ursina_run, camera_look_at
import math import math
...@@ -42,56 +42,56 @@ from simulators.ursina.ursina_config import UrsinaConfig ...@@ -42,56 +42,56 @@ from simulators.ursina.ursina_config import UrsinaConfig
from simulators.ursina.ursina_event import UrsinaEvent from simulators.ursina.ursina_event import UrsinaEvent
def get_satellite_position_velocity(earth_mass, earth_position, earth_radius, altitude): # def get_satellite_position_velocity(earth_mass, earth_position, earth_radius, altitude):
# 万有引力常数 # # 万有引力常数
G = 6.6743 * 10 ** (-11) # G = 6.6743 * 10 ** (-11)
# 地球质量 # # 地球质量
M = earth_mass # M = earth_mass
# 地球半径+海拔高度 # # 地球半径+海拔高度
R = earth_radius + altitude # R = earth_radius + altitude
#
# 随机生成卫星的位置,确保在球面上 # # 随机生成卫星的位置,确保在球面上
phi = random.uniform(0, 2 * math.pi) # phi = random.uniform(0, 2 * math.pi)
costheta = random.uniform(-1, 1) # costheta = random.uniform(-1, 1)
u = random.uniform(0, 1) # u = random.uniform(0, 1)
#
theta = math.acos(costheta) # theta = math.acos(costheta)
r = R * (u ** (1 / 3)) # r = R * (u ** (1 / 3))
#
x = r * math.sin(theta) * math.cos(phi) # x = r * math.sin(theta) * math.cos(phi)
y = r * math.sin(theta) * math.sin(phi) # y = r * math.sin(theta) * math.sin(phi)
z = r * math.cos(theta) # z = r * math.cos(theta)
#
# 计算速度的方向 # # 计算速度的方向
# 位置矢量 # # 位置矢量
r_vec = [x, y, z] # r_vec = [x, y, z]
# 速度方向和位置矢量垂直,采用向量叉积的性质 # # 速度方向和位置矢量垂直,采用向量叉积的性质
v_dir = [-y, x, 0] # v_dir = [-y, x, 0]
# 归一化 # # 归一化
v_dir_norm = [v / math.sqrt(x ** 2 + y ** 2) for v in v_dir] # v_dir_norm = [v / math.sqrt(x ** 2 + y ** 2) for v in v_dir]
#
# 计算速度大小 # # 计算速度大小
v = math.sqrt(G * M / R) # v = math.sqrt(G * M / R)
#
# 计算速度矢量 # # 计算速度矢量
v_vec = [v * dir for dir in v_dir_norm] # v_vec = [v * dir for dir in v_dir_norm]
#
# 计算卫星的位置和速度 # # 计算卫星的位置和速度
position = [earth_position[0] + x, earth_position[1] + y, earth_position[2] + z] # position = [earth_position[0] + x, earth_position[1] + y, earth_position[2] + z]
velocity = v_vec # velocity = v_vec
#
return tuple(position), [0, 0, 0] # return tuple(position), [0, 0, 0]
#
#
def generate_satellite_coordinates(earth_radius, altitude): # def generate_satellite_coordinates(earth_radius, altitude):
theta = random.uniform(0, 2 * math.pi) # 在0~2π内随机生成一个角度 # theta = random.uniform(0, 2 * math.pi) # 在0~2π内随机生成一个角度
phi = random.uniform(0, math.pi) # 在0~π内随机生成一个角度 # phi = random.uniform(0, math.pi) # 在0~π内随机生成一个角度
r = earth_radius + altitude # 地球半径+海拔高度 # r = earth_radius + altitude # 地球半径+海拔高度
x = r * math.sin(phi) * math.cos(theta) # 计算x坐标 # x = r * math.sin(phi) * math.cos(theta) # 计算x坐标
y = r * math.sin(phi) * math.sin(theta) # 计算y坐标 # y = r * math.sin(phi) * math.sin(theta) # 计算y坐标
z = r * math.cos(phi) # 计算z坐标 # z = r * math.cos(phi) # 计算z坐标
#
return [x, y, z] # return [x, y, z]
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册