鱼C论坛

 找回密码
 立即注册
查看: 2541|回复: 9

[已解决]模拟天体运动,matplotlib也行

[复制链接]
发表于 2023-2-7 18:01:55 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 歌者文明清理员 于 2023-4-28 00:32 编辑

限制:
1 啥都行,就是得计算引力加速度,不能直接画圆
2 像
  1. Stars = [Star(radius, mass), Star(radius, mass), …]
复制代码
这样(名字不一定),可以给个示例,我自己改
3 想不到吧,原来的3太离谱所以删了
看之前的帖子冷清了,就再发一次,怕是之前的没人看了

新规矩,链接放后面未知链接
不知道为什么,发不了悬赏
最佳答案
2023-2-10 21:02:49
  1. import pygame
  2. import sys
  3. import math
  4. import numpy as np
  5. import random

  6. G = 0.3


  7. class Star:
  8.     def __init__(self, star_id, radius, mass):
  9.         self.star_id = star_id
  10.         self.radius = radius
  11.         self.mass = mass
  12.         self.position = [random.randrange(width), random.randrange(height)]  # 随机初始化位置
  13.         self.speed = [int(random.choice([1, -1]) * 6 * np.random.rand(1)), int(random.choice([1, -1]) * 6 * np.random.rand(1))]  # 随机初始化速度

  14.     def move(self):
  15.         self.position[0] += self.speed[0]
  16.         self.position[1] += self.speed[1]
  17.         # 跑出窗口从另一边回来
  18.         self.position[0] -= self.position[0] // width * width
  19.         self.position[1] -= self.position[1] // height * height

  20.         pygame.draw.circle(screen, (255, 255, 255), self.position, self.radius)


  21. if __name__ == '__main__':
  22.     pygame.init()

  23.     size = width, height = 1920, 1020
  24.     bg = (0, 0, 0)

  25.     screen = pygame.display.set_mode(size)
  26.     pygame.display.set_caption("天体运动")

  27.     stars = [Star(star_id, random.randrange(3, 8), random.randrange(100, 200)) for star_id in range(10)]  # 随机生成天体

  28.     clock = pygame.time.Clock()

  29.     step = 1

  30.     while True:
  31.         clock.tick(60)
  32.         for event in pygame.event.get():
  33.             if event.type == pygame.QUIT:
  34.                 sys.exit(0)

  35.         screen.fill((0, 0, 0))
  36.         for i in range(len(stars)):
  37.             star = stars[i]
  38.             for star2 in stars[(i + 1):]:
  39.                 distance = math.dist(star.position, star2.position)
  40.                 if distance < 100:
  41.                     distance = 100
  42.                 # 万有引力公式F=GMm/r**2,0.1是为了适当缩小万有引力,否则会出现天体一直在转圈,你可以自己调整数值
  43.                 f = (G * (star.mass * star2.mass)) / distance ** 2 * 0.1
  44.                 relative_position = np.subtract(star.position, star2.position)
  45.                 angle = math.atan2(*relative_position[::-1])
  46.                 dx = math.cos(angle) * f
  47.                 dy = math.sin(angle) * f
  48.                 star.speed = np.subtract(star.speed, [dx, dy])
  49.                 star2.speed = np.add(star2.speed, [dx, dy])
  50.             star.speed = [x - math.copysign(0.001, x) for x in star.speed]  # 虽然天体是在做匀速直线运动,但不做衰减的话速度会一直叠加导致过快
  51.             star.move()
  52.         pygame.display.flip()
复制代码

初中生,不懂天体运动,找了个github项目作参考
https://github.com/zkytech/Simulation-of-Star-Motion
这个项目是在网页上模拟的,我移植到pygame来,并改了一些重要的地方
有什么bug或者疑惑的请告诉我
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-2-8 19:38:10 | 显示全部楼层

回帖奖励 +4 鱼币

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

使用道具 举报

发表于 2023-2-8 19:39:02 | 显示全部楼层
所以这道题是你的作业吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-2-8 21:55:25 | 显示全部楼层
你要啥样的运动,是一个天体围绕另一个运动,还是两个天体互相影响的那种
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-2-9 07:22:25 | 显示全部楼层
鱼cpython学习者 发表于 2023-2-8 21:55
你要啥样的运动,是一个天体围绕另一个运动,还是两个天体互相影响的那种

互相影响
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-2-10 21:02:49 | 显示全部楼层    本楼为最佳答案   
  1. import pygame
  2. import sys
  3. import math
  4. import numpy as np
  5. import random

  6. G = 0.3


  7. class Star:
  8.     def __init__(self, star_id, radius, mass):
  9.         self.star_id = star_id
  10.         self.radius = radius
  11.         self.mass = mass
  12.         self.position = [random.randrange(width), random.randrange(height)]  # 随机初始化位置
  13.         self.speed = [int(random.choice([1, -1]) * 6 * np.random.rand(1)), int(random.choice([1, -1]) * 6 * np.random.rand(1))]  # 随机初始化速度

  14.     def move(self):
  15.         self.position[0] += self.speed[0]
  16.         self.position[1] += self.speed[1]
  17.         # 跑出窗口从另一边回来
  18.         self.position[0] -= self.position[0] // width * width
  19.         self.position[1] -= self.position[1] // height * height

  20.         pygame.draw.circle(screen, (255, 255, 255), self.position, self.radius)


  21. if __name__ == '__main__':
  22.     pygame.init()

  23.     size = width, height = 1920, 1020
  24.     bg = (0, 0, 0)

  25.     screen = pygame.display.set_mode(size)
  26.     pygame.display.set_caption("天体运动")

  27.     stars = [Star(star_id, random.randrange(3, 8), random.randrange(100, 200)) for star_id in range(10)]  # 随机生成天体

  28.     clock = pygame.time.Clock()

  29.     step = 1

  30.     while True:
  31.         clock.tick(60)
  32.         for event in pygame.event.get():
  33.             if event.type == pygame.QUIT:
  34.                 sys.exit(0)

  35.         screen.fill((0, 0, 0))
  36.         for i in range(len(stars)):
  37.             star = stars[i]
  38.             for star2 in stars[(i + 1):]:
  39.                 distance = math.dist(star.position, star2.position)
  40.                 if distance < 100:
  41.                     distance = 100
  42.                 # 万有引力公式F=GMm/r**2,0.1是为了适当缩小万有引力,否则会出现天体一直在转圈,你可以自己调整数值
  43.                 f = (G * (star.mass * star2.mass)) / distance ** 2 * 0.1
  44.                 relative_position = np.subtract(star.position, star2.position)
  45.                 angle = math.atan2(*relative_position[::-1])
  46.                 dx = math.cos(angle) * f
  47.                 dy = math.sin(angle) * f
  48.                 star.speed = np.subtract(star.speed, [dx, dy])
  49.                 star2.speed = np.add(star2.speed, [dx, dy])
  50.             star.speed = [x - math.copysign(0.001, x) for x in star.speed]  # 虽然天体是在做匀速直线运动,但不做衰减的话速度会一直叠加导致过快
  51.             star.move()
  52.         pygame.display.flip()
复制代码

初中生,不懂天体运动,找了个github项目作参考
https://github.com/zkytech/Simulation-of-Star-Motion
这个项目是在网页上模拟的,我移植到pygame来,并改了一些重要的地方
有什么bug或者疑惑的请告诉我

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
歌者文明清理员 + 1 + 1 可惜只有一分

查看全部评分

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

使用道具 举报

 楼主| 发表于 2023-2-10 21:57:21 | 显示全部楼层
鱼cpython学习者 发表于 2023-2-10 21:02
初中生,不懂天体运动,找了个github项目作参考
https://github.com/zkytech/Simulation-of-Star-Motion ...

谢,可是本小学生(歌者)平时无电脑且上不了github
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-2-10 22:12:40 | 显示全部楼层
歌者文明清理员 发表于 2023-2-10 21:57
谢,可是本小学生(歌者)平时无电脑且上不了github

没事,那个github是如果你要修改一些细节的东西就给你做参考的,你看那个python代码没啥问题就行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-2-10 22:14:16 | 显示全部楼层
本帖最后由 歌者文明清理员 于 2023-2-10 22:18 编辑
鱼cpython学习者 发表于 2023-2-10 22:12
没事,那个github是如果你要修改一些细节的东西就给你做参考的,你看那个python代码没啥问题就行


本人python擅长pygame基础,但是没啥游戏灵感,代码我回复完就看
我是说how to打开github?打开后怎么用?

60FA57F3-834B-4B01-B278-C9E82B9C8931.png

我之前用turtle的实际数据写过日-地,结果地球总是直线穿过太阳又穿回来然后掉到屏幕下面
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-2-10 22:22:04 | 显示全部楼层
歌者文明清理员 发表于 2023-2-10 22:14
本人python擅长pygame基础,但是没啥游戏灵感,代码我回复完就看
我是说how to打开github?打开后怎么 ...

这个你可以去查,有很多方法,有加速插件,有改hosts之类的,很容易搜到的
例如:
https://blog.csdn.net/weixin_42121805/article/details/107711983
https://blog.csdn.net/nuannuanwfm/article/details/104119932
围绕天体转动和多个天体互相影响运动在物理中好像差别还蛮大的,我的建议是你最好去向一些专业的人寻求帮助,编程论坛一般帮助不大
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 20:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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