Advertisement
Guest User

32B-planets

a guest
May 1st, 2025
56
0
4 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.35 KB | None | 0 0
  1. import pygame
  2. import math
  3. import sys
  4.  
  5. # Constants
  6. SCREEN_WIDTH, SCREEN_HEIGHT = 800, 800
  7. CENTER_X, CENTER_Y = SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2
  8. SCALE = 150  # pixels per AU (adjust as needed)
  9. TIME_STEP = 0.001  # years per frame (controls animation speed)
  10.  
  11. # Planet data (a in AU, e in [0,1), period in years)
  12. planets = [
  13.     {"name": "Mercury", "color": (180, 180, 180), "a": 0.387, "e": 0.2056, "period": 0.2408},
  14.     {"name": "Venus", "color": (200, 100, 100), "a": 0.723, "e": 0.0068, "period": 0.6152},
  15.     {"name": "Earth", "color": (0, 0, 255), "a": 1.0, "e": 0.0167, "period": 1.0},
  16.     {"name": "Mars", "color": (180, 0, 0), "a": 1.524, "e": 0.0935, "period": 1.882},
  17. ]
  18.  
  19. # Comet data (a in AU, e > 1 for hyperbola)
  20. comet = {"name": "Comet", "color": (255, 255, 255), "a": -1.0, "e": 2.0}
  21.  
  22. # Precompute orbit points for planets and comet
  23. def precompute_orbit_points(obj, scale=SCALE, center=(400, 400)):
  24.     a, e = obj["a"], obj["e"]
  25.     if e < 1:  # Elliptical orbit
  26.         points = []
  27.         for theta in range(0, 360):
  28.             theta_rad = math.radians(theta)
  29.             r = (a * (1 - e**2)) / (1 + e * math.cos(theta_rad))
  30.             x = r * math.cos(theta_rad) * scale + center[0]
  31.             y = r * math.sin(theta_rad) * scale + center[1]
  32.             points.append((x, y))
  33.     else:  # Hyperbolic orbit
  34.         a_abs = abs(a)
  35.         points = []
  36.         H_max = 5.0  # Hyperbolic anomaly range
  37.         steps = 1000
  38.         for i in range(-steps, steps + 1):
  39.             H = i * H_max / steps
  40.             x, y = get_position_hyperbola(a_abs, e, H)
  41.             screen_x = x * scale + center[0]
  42.             screen_y = y * scale + center[1]
  43.             points.append((screen_x, screen_y))
  44.     return points
  45.  
  46. # Position functions
  47. def get_position_ellipse(a, e, E):
  48.     b = a * math.sqrt(1 - e**2)
  49.     x = a * (math.cos(E) - e)
  50.     y = b * math.sin(E)
  51.     return x, y
  52.  
  53. def get_position_hyperbola(a, e, H):
  54.     b = a * math.sqrt(e**2 - 1)
  55.     x = a * (math.cosh(H) - e)
  56.     y = b * math.sinh(H)
  57.     return x, y
  58.  
  59. # Solvers for Kepler's equation
  60. def solve_kepler(M, e):
  61.     E = M
  62.     for _ in range(100):
  63.         delta = (E - e * math.sin(E) - M) / (1 - e * math.cos(E))
  64.         E -= delta
  65.         if abs(delta) < 1e-8:
  66.             break
  67.     return E
  68.  
  69. def solve_hyperbolic_kepler(M, e):
  70.     H = 0.0
  71.     for _ in range(100):
  72.         delta = (e * math.sinh(H) - H - M) / (e * math.cosh(H) - 1)
  73.         H -= delta
  74.         if abs(delta) < 1e-8:
  75.             break
  76.     return H
  77.  
  78. # Main loop
  79. def main():
  80.     pygame.init()
  81.     screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  82.     clock = pygame.time.Clock()
  83.     running = True
  84.     current_time = 0.0  # in years
  85.  
  86.     # Precompute orbit lines
  87.     planet_orbits = [precompute_orbit_points(planet) for planet in planets]
  88.     comet_orbit = precompute_orbit_points(comet)
  89.  
  90.     while running:
  91.         for event in pygame.event.get():
  92.             if event.type == pygame.QUIT:
  93.                 running = False
  94.  
  95.         screen.fill((0, 0, 0))
  96.  
  97.         # Draw Sun
  98.         pygame.draw.circle(screen, (255, 255, 0), (CENTER_X, CENTER_Y), 10)
  99.  
  100.         # Update and draw planets
  101.         for i, planet in enumerate(planets):
  102.             a, e, period = planet["a"], planet["e"], planet["period"]
  103.             M = 2 * math.pi * (current_time / period)
  104.             E = solve_kepler(M, e)
  105.             x, y = get_position_ellipse(a, e, E)
  106.             screen_x = x * SCALE + CENTER_X
  107.             screen_y = y * SCALE + CENTER_Y
  108.             pygame.draw.lines(screen, planet["color"], False, planet_orbits[i], 1)
  109.             pygame.draw.circle(screen, planet["color"], (int(screen_x), int(screen_y)), 5)
  110.  
  111.         # Update and draw comet
  112.         a, e = comet["a"], comet["e"]
  113.         a_abs = abs(a)
  114.         n = math.sqrt(1 / (a_abs ** 3))  # Mean motion for hyperbola
  115.         M = n * current_time
  116.         H = solve_hyperbolic_kepler(M, e)
  117.         x, y = get_position_hyperbola(a_abs, e, H)
  118.         screen_x = x * SCALE + CENTER_X
  119.         screen_y = y * SCALE + CENTER_Y
  120.         pygame.draw.lines(screen, comet["color"], False, comet_orbit, 1)
  121.         pygame.draw.circle(screen, comet["color"], (int(screen_x), int(screen_y)), 3)
  122.  
  123.         pygame.display.flip()
  124.         clock.tick(60)
  125.         current_time += TIME_STEP
  126.  
  127.     pygame.quit()
  128.  
  129. if __name__ == "__main__":
  130.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement