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

太阳系三体模拟器

上级 3fdecfa7
......@@ -16,14 +16,14 @@ if __name__ == '__main__':
地球、月球
"""
# 地球的Y方向初始速度
EARTH_INIT_VELOCITY = 0.2 # 200m/s
EARTH_INIT_VELOCITY = -0.02 # 200m/s
bodies = [
Earth(init_position=[0, 0, 0],
init_velocity=[0, EARTH_INIT_VELOCITY, 0], size_scale=1e1), # 地球放大 10 倍,距离保持不变
Moon(init_position=[363104, 0, 0],
init_velocity=[0, EARTH_INIT_VELOCITY + 1.023, 0], size_scale=1e1) # 月球放大 10 倍,距离保持不变
]
mayavi_run(bodies, SECONDS_PER_HALF_DAY / 2, view_azimuth=-45)
# mayavi_run(bodies, SECONDS_PER_HALF_DAY / 2, view_azimuth=-45)
# 使用 ursina 查看的运行效果
# ursina_run(bodies, SECONDS_PER_DAY, position=(0, 0, 0))
\ No newline at end of file
ursina_run(bodies, SECONDS_PER_HALF_DAY / 2, position=(0, 0, 0))
......@@ -17,6 +17,7 @@ from simulators.simulator import Simulator
from common.system import System
import time
import datetime
import math
from ursina import EditorCamera, PointLight, SpotLight, AmbientLight
from scenes.func import ursina_run
......@@ -67,7 +68,7 @@ class UrsinaSimulator(Simulator):
if self.check_elapsed_time():
super().evolve(self.evolve_dt)
def cosmic_background(self, texture='../textures/cosmic1.jpg'):
def cosmic_background(self, texture='../textures/cosmic2.jpg'):
"""
加入宇宙背景
:param texture:
......@@ -75,13 +76,67 @@ class UrsinaSimulator(Simulator):
"""
# Add skybox
from ursina import Sky
Sky(texture=texture)
Sky(texture=texture).scale = 10000
# texture = load_texture(texture)
# sky_dome = Entity(model='sky_dome', texture=texture, scale=10000,
# color=color.white,
# position=(0, 0, 0),
# rotation=(0, 0, 0))
def __add_glow(self, entity, intensity=2, light_color=color.white, attenuation=3):
"""
未用,保留代码
:param entity:
:param intensity:
:param light_color:
:param attenuation:
:return:
"""
lights = []
import math
for i in range(5):
glow_entity = Entity(parent=entity, model='sphere', color=color.rgba(1.0, 0.6, 0.2, 1),
scale=math.pow(1.03, i), alpha=0.2)
lights.append(glow_entity)
# 创建一个新的 Entity 对象,作为光晕的容器
# glow_entity = Entity(parent=entity, model='sphere', scale=entity.scale * 1.2)
# 创建 PointLight 对象,并设置它的属性
light = PointLight(parent=lights[0], intensity=intensity, color=light_color, attenuation=attenuation)
lights.append(light)
# 把 Entity 对象放到星星的后面,使得光晕看起来像是从星星发出来的
glow_entity.world_position = entity.world_position
glow_entity.world_parent = entity.parent
glow_entity.y += entity.scale_y * 0.1
glow_entity.depth_test = False
return lights
def create_fixed_star_lights(self, entity):
"""
创建恒星的发光的效果、并作为灯光源
:param entity:
:return:
"""
# 如果是恒星(如:太阳),自身会发光,则需要关闭灯光
entity.set_light_off()
# if hasattr(self, "sun"):
# return
# self.sun = "sun"
lights = []
# 创建多个新的 Entity 对象,作为光晕的容器
for i in range(5):
glow_entity = Entity(parent=entity, model='sphere', color=color.rgba(1.0, 0.6, 0.2, 1),
scale=math.pow(1.03, i), alpha=0.2)
lights.append(glow_entity)
# 创建 PointLight 对象,作为恒星的灯光源
light = PointLight(parent=entity, intensity=10, range=10, color=color.white)
lights.append(light)
return lights
def run(self, dt, **kwargs):
from ursina import EditorCamera, PointLight, SpotLight, AmbientLight, DirectionalLight
self.evolve_dt = dt
......@@ -92,21 +147,7 @@ class UrsinaSimulator(Simulator):
if kwargs["light"]:
for v in self.ursina_views:
if v.body.is_fixed_star:
# v.light = PointLight(parent=v, intensity=10, range=10, color=color.white)
# v.light.brightness = 10
lights = []
for i in range(1): # 可以增加多个光源
light = PointLight(parent=v, intensity=10, range=10, color=color.white)
# light.brightness = 10
# light.attenuation = Vec3(1, 0.1, 0.01)
lights.append(light)
v.lights = v.lights + lights
pass
# PointLight(parent=camera, color=color.white, position=(0, 0, 0))
# AmbientLight(color=color.rgba(100, 100, 100, 0.1))
# DirectionalLight
# SpotLight
self.lights = self.create_fixed_star_lights(v.planet)
if "show_grid" in kwargs:
if kwargs["show_grid"]:
......
......@@ -22,7 +22,7 @@ from simulators.views.body_view import BodyView
import numpy as np
import math
SCALE_FACTOR = 1e-6
SCALE_FACTOR = 5e-7
class UrsinaPlayer(FirstPersonController):
......@@ -103,9 +103,9 @@ class Planet(Entity):
self.rotation_y -= self.rotspeed
def input(self, key):
if key == "enter":
self.fastMode = 1 - self.fastMode
# def input(self, key):
# if key == "enter":
# self.fastMode = 1 - self.fastMode
class UrsinaView(BodyView):
......@@ -121,40 +121,32 @@ class UrsinaView(BodyView):
if body.has_rings:
self.create_rings()
if self.body.is_fixed_star:
# 如果是恒星(如:太阳),自身会发光,则需要关闭灯光
self.planet.set_light_off()
self.lights = []
self.create_light_sphere()
def create_light_sphere(self):
for i in range(5):
light = Entity(parent=self.planet, model='sphere', color=color.rgba(1.0, 0.6, 0.2, 1),
scale=math.pow(1.03, i), alpha=0.2)
# self.lights.append(light)
def create_rings(self):
"""
创建星环(使用土星贴图)
创建星环(使用土星贴图)
:return:
"""
scale = 3 * self.body.diameter * self.body.size_scale * SCALE_FACTOR
pos = self.planet.position
self.ring = Entity(model="circle", texture='../textures/saturnRings.jpg', scale=scale, position=pos,
rotation=(70, 0, 0), double_sided=True)
# 假设星环自身会发光
# 行星环偏移角度
self.ring_rotation_x = 70
# 创建行星环
self.ring = Entity(parent=self.planet, model="circle", texture='../textures/saturnRings.jpg', scale=2,
rotation=(self.ring_rotation_x, 0, 0), double_sided=True)
# 设置行星环不受灯光影响,否则看不清行星环
self.ring.set_light_off()
def update(self):
self.planet.turn()
if hasattr(self, "light"):
self.light.position = Vec3(self.planet.x, self.planet.y, self.planet.z)
if hasattr(self, "lights"):
for light in self.lights:
light.position = Vec3(self.planet.x, self.planet.y, self.planet.z)
"""
:return:
"""
self.planet.turn()
# 如果有行星环
if hasattr(self, "ring"):
self.ring.position = Vec3(self.planet.x, self.planet.y, self.planet.z)
# 如果有行星环,则不让行星环跟随行星转动
self.ring.rotation = -Vec3(self.planet.rotation_x - self.ring_rotation_x,
self.planet.rotation_y,
self.planet.rotation_z)
def appear(self):
pass
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册