歌者文明清理员 发表于 2023-3-5 16:55:40

【vpython模拟天体运动系列】第一期

这是我突发奇想创作的,天体参数要是不对(就是比例跟现实中的对不上)请在评论区指出
主程序
# -*- coding:utf-8 -*-
"""
@Author: 歌者文明清理员1410382
@Datetime: 2023-03-05 14:23:23
@Name: collision.py
@OriginLink:
"""
from calc import *
from random import uniform


太阳 = obj('太阳', 1, 1, vec(0, 0, 0), True, vec(0, 0, 0), False, color=color.yellow)
obj('比邻星', 0.15, 0.5, vec(0, 0, uniform(0.66, 0.67)), True, vec(5, 0, 0), False, color=color.red)
obj(
    '地球', 0.001 / 318, 0.3, vec(0, uniform(0.6, 0.7), 0), False, vec(3, 0, (random() - 0.5) * 5), False, color.blue,
    texture=textures.earth
)
obj(
    '木星', 0.001, 0.4, vec(0, 0, uniform(0.35, 0.5)), False, vec(0, uniform(7.5, 10), 0), False, color.orange,
    texture=textures.wood
)

scene.range = 100
scene.width, scene.height = 1800, 1000
scene.follow(太阳)
mainloop(1)

calc.py
# -*- coding:utf-8 -*-
"""
@Author: 歌者文明清理员1410382
@Datetime: 2023-03-05 14:07:57
@Name: calc.py
@OriginLink:
"""
# -*- coding:utf-8 -*-
"""
@Author: 歌者文明清理员1410382
@Datetime: 2023-03-04 16:21:03
@Name: calc.py
@OriginLink:
"""
from vpython import *


def obj(name, mass, radius, momentum, emissive, pos, lock=False, tc=None, color=color.white, texture=None):
    a = sphere(color=color, make_trail=True, retain=150, trail_width=radius / 10)
    a.name = name
    a.mass = mass
    a.radius = radius
    a.momentum = momentum
    a.trail_color = tc if tc else color
    a.texture = texture if texture else a.texture
    a.emissive = emissive
    a.pos = pos
    a.locked = lock
    if a.emissive:
      attach_light(a)
    stars.append(a)
    return a


def move(g=1):
    for sa in stars:
      sa.pos += sa.momentum * (not sa.locked)
      for sb in stars:
            if sa == sb:
                continue# same star
            distance = mag2(sa.pos-sb.pos)**1.5
            limit = max(sa.radius, sb.radius) * 2.44
            if distance <= limit:
                big, small = (sa, sb) if sa.mass > sb.mass else (sb, sa)
                delta = small.mass * (1 - distance / limit)
                small.mass -= delta
                small.radius -= delta * (4 / 3 * pi * small.radius ** 3)
                big.mass += delta
                big.radius += delta * (4 / 3 * pi * big.radius * 3)
                print(f'{small.name}进入了{big.name}的洛希极限({limit},实际距离为{distance}),质量变为{small.mass}')
                if small.mass <= 1e-17 or distance < small.radius + big.radius:
                  print(f'{small.name} 已被毁灭')
                  small.visible = False
                  small.clear_trail()
                  big.mass += small.mass
                  big.radius += small.radius
                  big.momentum += small.momentum * small.mass / big.mass
                  big.color = big.trail_color = (big.trail_color + small.trail_color) / 2
                  small.mass = 0
                  stars.remove(small)
                  continue
            force = g * sa.mass / \
                distance * (sa.pos-sb.pos)
            sb.momentum += force



def mainloop(dt=1, g=1, bef=None, aft=None):
    while True:
      rate(dt * 100)
      if callable(bef):
            bef()
      move(g)
      if callable(aft):
            aft()

stars = []

准备
pip install vpython -i 镜像源
其中镜像源可以搜索“pip国内”得到

开始玩吧,用的是随机参数
效果图:
https://xxx.ilovefishc.com/album/202303/05/165500bq0ckuc7jvlyajfz.gif
哦对了这个不能忘
https://xxx.ilovefishc.com/forum/202303/05/145337y4ul39xzjdlxdjum.gif
页: [1]
查看完整版本: 【vpython模拟天体运动系列】第一期