-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkepler47c.py
More file actions
82 lines (69 loc) · 2 KB
/
kepler47c.py
File metadata and controls
82 lines (69 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# importing libraries
import pygame
import numpy
import sys
# setting constants and variables
WIDTH = 420
HEIGHT = 400
m1 = 10
m2 = 0.1
m3 = 0.1
p1 = numpy.array([250.0, 200.0])
p2 = numpy.array([220.0, 200.0])
p3 = numpy.array([170.0, 200.0])
v1 = numpy.array([0.0, 17.0])
v2 = numpy.array([0.0, -900.0])
v3 = numpy.array([0.0, -900.0])
G = 5e6 # real one is 6.674e-11
FPS = 90
MIN_R = 40.0
running = True
dt = 0.01
# creating the screen, clock, and dimming surface
screen = pygame.display.set_mode([WIDTH, HEIGHT])
pygame.display.set_caption("OrbitDance")
screen.fill((0, 0, 0))
clock = pygame.time.Clock()
draw_surface = pygame.Surface((WIDTH,HEIGHT))
draw_surface.set_alpha(10)
draw_surface.fill((0, 0, 0))
def draw():
# drawing the astronomical bodies and making the old paths look dimmer
global p1, p2, p3, screen, draw_surface
screen.blit(draw_surface, (0, 0))
pygame.draw.circle(screen, (0, 200, 200), p1, 10)
pygame.draw.circle(screen, (200, 0, 200), p2, 7)
pygame.draw.circle(screen, (200, 200, 0), p3, 5)
pygame.display.flip()
def update(dt):
global p1, v1, p2, v2, p3, v3, G
# updating the positions
p1 += dt * v1
p2 += dt * v2
p3 += dt * v3
# dealing with crashing
r12 = max(MIN_R, numpy.linalg.norm(p2 - p1))
r13 = max(MIN_R, numpy.linalg.norm(p3 - p1))
r23 = max(MIN_R, numpy.linalg.norm(p2 - p3))
# computing the forces using the gravitational equation
f12 = G * m1 * m2 * (p2 - p1) / pow(r12, 3)
f13 = G * m1 * m3 * (p3 - p1) / pow(r13, 3)
f23 = G * m2 * m3 * (p3 - p2) / pow(r23, 3)
# computing the acceleration
a1 = (f12 + f13) / m1
a2 = (-f12 + f23) / m2
a3 = (-f23 + -f13) / m3
# updating the velocities
v1 += dt * a1
v2 += dt * a2
v3 += dt * a3
while running:
clock.tick(FPS)
# in case of exit
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# drawing and updating everything at 90 FPS
draw()
update(dt)