鱼C论坛

 找回密码
 立即注册
查看: 1519|回复: 0

[技术交流] 【vpython模拟天体运动系列】第一期

[复制链接]
发表于 2023-3-5 16:55:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这是我突发奇想创作的,天体参数要是不对(就是比例跟现实中的对不上)请在评论区指出
主程序
# -*- 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国内”得到

开始玩吧,用的是随机参数
效果图:

                               
登录/注册后可看大图

哦对了这个不能忘

                               
登录/注册后可看大图

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
一点沙 + 5 + 5 + 3 大力支持

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-24 07:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表