鱼C论坛

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

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

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

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

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

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

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

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

G = 0.3


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

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

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


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

    size = width, height = 1920, 1020
    bg = (0, 0, 0)

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

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

    clock = pygame.time.Clock()

    step = 1

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

        screen.fill((0, 0, 0))
        for i in range(len(stars)):
            star = stars[i]
            for star2 in stars[(i + 1):]:
                distance = math.dist(star.position, star2.position)
                if distance < 100:
                    distance = 100
                # 万有引力公式F=GMm/r**2,0.1是为了适当缩小万有引力,否则会出现天体一直在转圈,你可以自己调整数值
                f = (G * (star.mass * star2.mass)) / distance ** 2 * 0.1
                relative_position = np.subtract(star.position, star2.position)
                angle = math.atan2(*relative_position[::-1])
                dx = math.cos(angle) * f
                dy = math.sin(angle) * f
                star.speed = np.subtract(star.speed, [dx, dy])
                star2.speed = np.add(star2.speed, [dx, dy])
            star.speed = [x - math.copysign(0.001, x) for x in star.speed]  # 虽然天体是在做匀速直线运动,但不做衰减的话速度会一直叠加导致过快
            star.move()
        pygame.display.flip()
初中生,不懂天体运动,找了个github项目作参考
https://github.com/zkytech/Simulation-of-Star-Motion
这个项目是在网页上模拟的,我移植到pygame来,并改了一些重要的地方
有什么bug或者疑惑的请告诉我
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

回帖奖励 +4 鱼币

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

使用道具 举报

发表于 2023-2-8 19:39:02 | 显示全部楼层
所以这道题是你的作业吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-2-8 21:55:25 | 显示全部楼层
你要啥样的运动,是一个天体围绕另一个运动,还是两个天体互相影响的那种
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

互相影响
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

G = 0.3


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

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

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


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

    size = width, height = 1920, 1020
    bg = (0, 0, 0)

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

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

    clock = pygame.time.Clock()

    step = 1

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

        screen.fill((0, 0, 0))
        for i in range(len(stars)):
            star = stars[i]
            for star2 in stars[(i + 1):]:
                distance = math.dist(star.position, star2.position)
                if distance < 100:
                    distance = 100
                # 万有引力公式F=GMm/r**2,0.1是为了适当缩小万有引力,否则会出现天体一直在转圈,你可以自己调整数值
                f = (G * (star.mass * star2.mass)) / distance ** 2 * 0.1
                relative_position = np.subtract(star.position, star2.position)
                angle = math.atan2(*relative_position[::-1])
                dx = math.cos(angle) * f
                dy = math.sin(angle) * f
                star.speed = np.subtract(star.speed, [dx, dy])
                star2.speed = np.add(star2.speed, [dx, dy])
            star.speed = [x - math.copysign(0.001, x) for x in star.speed]  # 虽然天体是在做匀速直线运动,但不做衰减的话速度会一直叠加导致过快
            star.move()
        pygame.display.flip()
初中生,不懂天体运动,找了个github项目作参考
https://github.com/zkytech/Simulation-of-Star-Motion
这个项目是在网页上模拟的,我移植到pygame来,并改了一些重要的地方
有什么bug或者疑惑的请告诉我

评分

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

查看全部评分

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

使用道具 举报

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

谢,可是本小学生(歌者)平时无电脑且上不了github
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

没事,那个github是如果你要修改一些细节的东西就给你做参考的,你看那个python代码没啥问题就行
想知道小甲鱼最近在做啥?请访问 -> 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的实际数据写过日-地,结果地球总是直线穿过太阳又穿回来然后掉到屏幕下面
想知道小甲鱼最近在做啥?请访问 -> 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
围绕天体转动和多个天体互相影响运动在物理中好像差别还蛮大的,我的建议是你最好去向一些专业的人寻求帮助,编程论坛一般帮助不大
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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