-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
139 lines (110 loc) · 3.62 KB
/
main.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Simulates the three body problem using pygame
import pygame
import math
import threading
import time
# simulation timestep in seconds
SIM_TIMESTEP = 0.005
# simulation time to real time conversion
SIM_TO_REAL = 0.04
# a class representing a point like object floating in 2D space
class Object:
def __init__(self, mass, pos, vel):
self.mass = mass
self.pos = pygame.math.Vector2(pos)
self.vel = pygame.math.Vector2(vel)
# symmetric
def force_magnitude(self, other):
delta = other.pos - self.pos
r_squared = delta.magnitude_squared()
return self.mass * other.mass / r_squared
# asymmetric
def force_direction(self, other):
delta = other.pos - self.pos
r = delta.magnitude()
return delta / r
# reset the simulation
def reset():
global objects
objects = []
# try changing some of these numbers
# mass,position,velocity
objects.append(Object(60, (-25, 0), (0, -1)))
objects.append(Object(100, (20, 0), (0, 0)))
objects.append(Object(80, (0, 30), (3, 0)))
# simulate the next step
def update():
for i in range(len(objects)):
obj = objects[i]
force_total = pygame.math.Vector2(0, 0)
for j in range(len(objects)):
if i == j:
continue
# if (obj.pos - objects[j].pos).magnitude_squared() < 1:
# force_total += -10000 * obj.force_direction(objects[j])
# else:
force_total += obj.force_magnitude(objects[j]) * \
obj.force_direction(objects[j])
obj.vel += SIM_TIMESTEP * force_total / obj.mass
obj.pos += SIM_TIMESTEP * obj.vel
reset()
# do the physics
def physics():
while True:
ts = time.time()
update()
ts_old = ts
time.sleep(SIM_TIMESTEP * SIM_TO_REAL - (ts_old - ts))
physics_thread = threading.Thread(target=physics, args=()).start()
# pygame setup
running = True
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
traces = [[] for _ in range(len(objects))]
trace_ball = []
trace_center = []
while running:
# poll for events
# pygame.QUIT event means the user clicked X to close your window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# fill the screen with a color to wipe away anything from last frame
screen.fill("black")
width, height = screen.get_size()
mid = pygame.math.Vector2(width / 2, height / 2)
scale = min(width, height)
avg_pos = pygame.math.Vector2(0, 0)
for obj in objects:
avg_pos += obj.pos
avg_pos /= len(objects)
convert = lambda pos: mid + \
(pos - avg_pos) * scale / 100
# draw traces
trace_len = len(traces[0])
for i in range(trace_len):
if i == 0:
continue
for trace in traces:
prev = convert(trace[i - 1])
curr = convert(trace[i])
t = i / trace_len
w = int(scale/100.0)
b = t * 100
pygame.draw.line(screen, (b, b, b), prev, curr, width=w)
# draw circles
for i in range(len(objects)):
obj = objects[i]
trace = traces[i]
trace.append(pygame.math.Vector2(obj.pos))
if len(trace) > 100:
trace.pop(0)
c = scale / 600.0
radius = math.sqrt(obj.mass) * c
pygame.draw.circle(screen, (255, 255, 255), center=convert(obj.pos), radius=radius)
# flip() the display to put your work on screen
pygame.display.flip()
clock.tick(60) # limits FPS to 60
pygame.quit()
physics_thread.join()