pygame使一个圆圈绕另一个旋转 [英] Pygame make a circle rotate around another

查看:46
本文介绍了pygame使一个圆圈绕另一个旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在pygame中制作某种太阳系.我已经设法完成了一个固定的任务,但是我认为对行星绕太阳运动,月亮绕行星等运动会更有趣.有没有办法做到这一点(如果可能,请使用pygame)?

I would like to make some kind of solar system in pygame. I've managed to do a fixed one but I thought it would be more interesting to do one with planets moving around the sun and moons around planets etc. Is there a way I could do that (using pygame if possible)?

我想要的是:

Sun = pygame.draw.circle(...)
planet1 = pygame.draw.circle(...)
etc.

a = [planet1, planet2, ...]

for p in a:
    move p[2] to pos(x, y)

这是我认为可行的方法,但我不确定如何执行.另外,我还考虑过删除古代行星并在其旁边绘制一个新行星,但是问题是我使用的是随机特征(例如颜色,与太阳的距离,系统中行星的数量等),并且将不得不保留这些相同的功能.有任何想法吗?

That is what I think would work but I'm not sure how to do it. Also, I've thought about deleting the ancient planet and drawing a new one right next to it, but problem is I'm using random features (like colours, distance to the sun, number of planets in the system etc.) and it would have to keep these same features. Any ideas?

提前谢谢!

推荐答案

您可以使用牛顿万有引力定律和牛顿第二定律来实现引力,以获取行星的加速度.给每个行星一个初始位置,速度和质量.加速度是速度的变化,a = v * dt ,速度是变化的位置, v = r * dt ,因此我们可以积分找到速度和位置.

You can implement gravity with Newton's Law of Universal Gravitation and Newton's Second Law to get the accelerations of the planets. Give each planet an initial position, velocity and mass. Acceleration is change in velocity a = v * dt, velocity is change in position v = r * dt, so we can integrate to find velocity and position.

万有引力: F = G * m1 * m2/r ** 2 其中, F 是物体上的力的大小, G 是重力常数, m1 m2 是物体的质量, r 是物体之间的距离这两个对象.

Universal gravitation: F = G * m1 * m2 / r ** 2 where F is the magnitude of the force on the object, G is the gravitational constant, m1 and m2 are the masses of the objects and r is the distance between the two objects.

牛顿第二定律: F = m1 * a 其中a是加速度.

dt = 0.01  # size of time step
G = 100 # gravitational constant 

def calcGravity(sun, planet): 
  'Returns acceleration of planet with respect to the sun' 
  diff_x = sun.x - planet.x 
  diff_y = sun.y - planet.y 
  acceleration = G * sun.mass / (diff_x ** 2 + diff_y ** 2) 
  accel_x = acceleration * diff_x / (diff_x ** 2 + diff_y ** 2)
  accel_y = acceleration * diff_y / (diff_x ** 2 + diff_y ** 2)
  return accel_x, accel_y

while True: 
  # update position based on velocity 
  planet.x += planet.vel_x * dt 
  planet.y += planet.vel_y * dt 

  # update velocity based on acceleration
  accel_x, accel_y = calcGravity(sun, planet)
  planet.vel_x += accel_x * dt
  planet.vel_y += accel_y * dt 

这可以产生圆形和椭圆形的轨道.制作绕月卫星需要很短的时间步长(dt)进行数字积分.

This can produce circular and elliptical orbits. Creating an orbiting moon requires a very small timestep (dt) for the numeric integration.

注意:由于数字积分的限制,此方法有些不准确.

Note: this approach is subtly inaccurate due to the limits of numeric integration.

此处pygame中的示例实现,包括三个围绕太阳,月亮和基本轨道转移旋转的行星. https://github.com/c2huc2hu/orbital_mechanics

Sample implementation in pygame here, including three planets revolving around a sun, a moon, and a basic orbital transfer. https://github.com/c2huc2hu/orbital_mechanics

这篇关于pygame使一个圆圈绕另一个旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆