鱼C论坛

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

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

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

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

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

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


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

  20. scene.range = 100
  21. scene.width, scene.height = 1800, 1000
  22. scene.follow(太阳)
  23. mainloop(1)
复制代码

calc.py
  1. # -*- coding:utf-8 -*-
  2. """
  3. @Author: 歌者文明清理员1410382
  4. @Datetime: 2023-03-05 14:07:57
  5. @Name: calc.py
  6. @OriginLink:
  7. """
  8. # -*- coding:utf-8 -*-
  9. """
  10. @Author: 歌者文明清理员1410382
  11. @Datetime: 2023-03-04 16:21:03
  12. @Name: calc.py
  13. @OriginLink:
  14. """
  15. from vpython import *


  16. def obj(name, mass, radius, momentum, emissive, pos, lock=False, tc=None, color=color.white, texture=None):
  17.     a = sphere(color=color, make_trail=True, retain=150, trail_width=radius / 10)
  18.     a.name = name
  19.     a.mass = mass
  20.     a.radius = radius
  21.     a.momentum = momentum
  22.     a.trail_color = tc if tc else color
  23.     a.texture = texture if texture else a.texture
  24.     a.emissive = emissive
  25.     a.pos = pos
  26.     a.locked = lock
  27.     if a.emissive:
  28.         attach_light(a)
  29.     stars.append(a)
  30.     return a


  31. def move(g=1):
  32.     for sa in stars:
  33.         sa.pos += sa.momentum * (not sa.locked)
  34.         for sb in stars:
  35.             if sa == sb:
  36.                 continue  # same star
  37.             distance = mag2(sa.pos-sb.pos)**1.5
  38.             limit = max(sa.radius, sb.radius) * 2.44
  39.             if distance <= limit:
  40.                 big, small = (sa, sb) if sa.mass > sb.mass else (sb, sa)
  41.                 delta = small.mass * (1 - distance / limit)
  42.                 small.mass -= delta
  43.                 small.radius -= delta * (4 / 3 * pi * small.radius ** 3)
  44.                 big.mass += delta
  45.                 big.radius += delta * (4 / 3 * pi * big.radius * 3)
  46.                 print(f'{small.name}进入了{big.name}的洛希极限({limit},实际距离为{distance}),质量变为{small.mass}')
  47.                 if small.mass <= 1e-17 or distance < small.radius + big.radius:
  48.                     print(f'{small.name} 已被毁灭')
  49.                     small.visible = False
  50.                     small.clear_trail()
  51.                     big.mass += small.mass
  52.                     big.radius += small.radius
  53.                     big.momentum += small.momentum * small.mass / big.mass
  54.                     big.color = big.trail_color = (big.trail_color + small.trail_color) / 2
  55.                     small.mass = 0
  56.                     stars.remove(small)
  57.                     continue
  58.             force = g * sa.mass / \
  59.                 distance * (sa.pos-sb.pos)
  60.             sb.momentum += force



  61. def mainloop(dt=1, g=1, bef=None, aft=None):
  62.     while True:
  63.         rate(dt * 100)
  64.         if callable(bef):
  65.             bef()
  66.         move(g)
  67.         if callable(aft):
  68.             aft()

  69. stars = []
复制代码

准备
  1. pip install vpython -i 镜像源
复制代码

其中镜像源可以搜索“pip国内”得到

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

                               
登录/注册后可看大图

哦对了这个不能忘

                               
登录/注册后可看大图

评分

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

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-3 20:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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