Полный код можно найти здесь: https://pastebin.com/Y0NVQwhm
При добавлении одной планеты за раз симуляция работает в основном так, как я ожидал, но когда я добавляю много тел с незначительной массой с помощью цикла for, это каким-то образом выбрасывает все остальные тела.
Стабильные орбиты создаются по одной, вручную настраивая их начальные параметры
Каждое второе тело выбрасывается при использовании цикла for для создания множества тел с незначительной массой p>
Мне удалось даже полуточно получить гравитацию, но я совершенно не могу понять, почему добавление тел ослабляет притяжение между всеми остальными телами.< /p>
Вот код управления гравитация:
Код: Выделить всё
for obj in Planet.instances:
for other in Planet.instances:
if obj.name != other.name:
pygame.draw.circle(screen, obj.color, (obj.x, obj.y), obj.rad)
#if (other.name != "SOL" and obj.name != "SOL"):
#pygame.draw.line(screen, blue, (obj.x, obj.y), (other.x, other.y), 2)
labler = my_font.render(obj.name, False, red)
if (obj.name != "MOON"):
screen.blit(labler, (obj.x, obj.y + obj.rad))
'''Increasing position at a steady rate to make velocity'''
obj.x += obj.xv
obj.y += obj.yv
'''Gets the distance between bodies. The 10 coefficient is to artificially increase distance to make orbits slower so I can fully enjoy their trajectories'''
xDiff = 10 * (abs(obj.x - other.x))
yDiff = 10 * (abs(obj.y - other.y))
'''This scales how strong the gravity will be in either axis depending on how in-line the planet is to the central body'''
xForce = xDiff / (xDiff + yDiff)
yForce = yDiff / (xDiff + yDiff)
'''The main gravity equation'''
gravity = (obj.mass * other.mass) / ((xDiff ** 2) + (yDiff ** 2)) * constant
'''If body A is above/below body B, add velocity upwards/downwards. Extrapolate for X axis'''
if obj.x = other.x:
obj.xv -= (xForce * gravity) * (other.mass / obj.mass)
if obj.y = other.y:
obj.yv -= (yForce * gravity) * (other.mass / obj.mass)
Подробнее здесь: https://stackoverflow.com/questions/792 ... -planets-i