提交 74450e58 编写于 作者: M march3

太阳系三体模拟器

上级 3316b505
......@@ -93,14 +93,16 @@ from simulators.views.ursina_view import UrsinaView, UrsinaPlayer
from simulators.simulator import Simulator
from common.system import System
import time
import datetime
ursina_views = []
player = None
class UrsinaSimulator(Simulator):
def __init__(self, bodies_sys: System):
self.app = Ursina()
self.ursina_views = []
window.color = color.black
super().__init__(bodies_sys, UrsinaView)
......@@ -112,17 +114,31 @@ class UrsinaSimulator(Simulator):
# pos = tuple(body.position)
# ursina_view = UrsinaView(body)
view.update()
ursina_views.append(view)
self.ursina_views.append(view)
# planets.append(newPlanet)
# x += cp[i] * 10
def check_elapsed_time(self):
"""检查时间间隔是否已过"""
now = datetime.datetime.now()
elapsed_time = now - self.last_time
return elapsed_time >= self.interval
def check_and_evolve(self):
if self.check_elapsed_time():
super().evolve(self.evolve_dt)
def run(self, dt, **kwargs):
self.evolve_dt = dt
# 设定时间间隔为1秒
self.interval = datetime.timedelta(seconds=1)
self.last_time = datetime.datetime.now()
self.app.run()
if __name__ == '__main__':
from bodies import Sun, Earth
from common.consts import SECONDS_PER_WEEK
from common.consts import SECONDS_PER_WEEK, SECONDS_PER_DAY
"""
3个太阳、1个地球
......@@ -151,16 +167,16 @@ if __name__ == '__main__':
body_sys = System(bodies)
simulator = UrsinaSimulator(body_sys)
player = UrsinaPlayer((0, -849597870.700, 0))
player = UrsinaPlayer((0, 0, 849597870.700),simulator.ursina_views)
def update():
# print('OK')
global ursina_views, player
for ursina_view in ursina_views:
for ursina_view in simulator.ursina_views:
simulator.check_and_evolve()
ursina_view.update()
# ursina_view.entity.turn(ursina_view.entity.angle)
player._update()
simulator.run(SECONDS_PER_WEEK)
simulator.run(SECONDS_PER_DAY)
......@@ -7,7 +7,7 @@
# python_version :3.8
# ==============================================================================
# pip install -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com ursina
from ursina import Ursina, window, Entity, camera, color, mouse, Vec2, Vec3, load_texture, held_keys
from ursina import Ursina, window, Entity, SmoothFollow, camera, color, mouse, Vec2, Vec3, load_texture, held_keys
from math import sin, cos, radians
from ursina.prefabs.first_person_controller import FirstPersonController
import sys
......@@ -20,21 +20,35 @@ import numpy as np
from math import sin, cos, radians
import os
SCALE_FACTOR = 1e-6
SCALE_FACTOR = 1e-7
class UrsinaPlayer(FirstPersonController):
def __init__(self, position):
def __init__(self, position, targets=None):
# global planets
super().__init__()
# pos = planets[0].position
camera.fov = 100
if targets is not None:
# planets = []
# targets = [view.planet.parent for view in targets]
targets_parent = Entity()
for view in targets:
view.planet.parent = targets_parent
# planets.append(view.planet)
camera.add_script(SmoothFollow(targets_parent, offset=(0, 8, -20)))
pos = np.array(position) * SCALE_FACTOR
self.position = Vec3(pos[0], pos[1], pos[2])
# self.position = Vec3(pos[0], pos[1], pos[2])
# 将摄像机位置设置为 x=0、y=1、z=0 的位置
# camera.position = Vec3(pos[0], pos[1], pos[2])
self.position = Vec3(pos[0], pos[1], pos[2])
# 将摄像机的观察角度绕 x 轴旋转 45 度,绕 y 轴旋转 0 度,绕 z 轴旋转 0 度
camera.rotation = Vec3(45, 90, 0)
self.gravity = 0
self.vspeed = 40
self.speed = 100
self.vspeed = 4000
self.speed = 10000
self.mouse_sensitivity = Vec2(160, 160)
self.on_enable()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册