-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnbody-pygame.py
153 lines (117 loc) · 4.01 KB
/
nbody-pygame.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import numpy as np
from settings import *
DT = 0.1
G = 0.1
class Camera:
def __init__(self, camera_pos, camera_angle, display_pos):
self.camera_pos = camera_pos
self.camera_angle = camera_angle
self.display_pos = display_pos
self.rot_x = np.empty((3, 3))
self.rot_y = np.empty((3, 3))
self.rot_z = np.empty((3, 3))
self.update_rotation_matrices()
def update_rotation_matrices(self):
theta = self.camera_angle
cx = np.cos(theta[0])
sx = np.sin(theta[0])
cy = np.cos(theta[1])
sy = np.sin(theta[1])
cz = np.cos(theta[2])
sz = np.sin(theta[2])
self.rot_x = np.array([[1.0, 0.0, 0.0],
[0.0, cx, sx],
[0.0, -sx, cx]])
self.rot_y = np.array([[cy, 0.0, -sy],
[0.0, 1.0, 0.0],
[sy, 0.0, cy]])
self.rot_z = np.array([[cz, sz, 0.0],
[-sz, cz, 0.0],
[0.0, 0.0, 1.0]])
def to_screen(self, pos):
dd = pos - self.camera_pos
rot_xyz = (self.rot_y * self.rot_z) * self.rot_x
d = dd @ rot_xyz.T
bx = self.display_pos[2] * d[:, 0] / d[:, 2] + self.display_pos[0]
by = self.display_pos[2] * d[:, 1] / d[:, 2] + self.display_pos[1]
x = np.rint(bx).astype(int)
y = np.rint(by).astype(int)
return x, y
class Bodies:
def __init__(self, m, pos, velocity):
"""
Body initialization
:param m: Body mass
:param pos: Body position vector (x, y) in world units
:param velocity: Body velocity (vx, vy)
"""
self.m = m
self.pos = pos
self.velocity = velocity
self.acceleration = np.zeros((m.shape[0], 3))
def update(self):
softening_sq = 0.01
mass = self.m
dt = DT
self.velocity += self.acceleration * dt * 0.5
self.pos += self.velocity * dt
x = self.pos[:, 0:1]
y = self.pos[:, 1:2]
z = self.pos[:, 2:3]
dx = x.T - x
dy = y.T - y
dz = z.T - z
dx2 = dx * dx
dy2 = dy * dy
dz2 = dz * dz
r2 = dx2 + dy2 + dz2
inv_r3 = (r2 + softening_sq)**(-1.5)
ax = G * (dx * inv_r3) @ mass
ay = G * (dy * inv_r3) @ mass
az = G * (dz * inv_r3) @ mass
self.acceleration = np.stack((ax, ay, az), axis=1)
self.velocity += self.acceleration * dt * 0.5
def draw(self, surface, camera):
bx, by = camera.to_screen(self.pos)
d = self.pos - camera.camera_pos
dx2 = d[:, 0] * d[:, 0]
dy2 = d[:, 1] * d[:, 1]
dz2 = d[:, 2] * d[:, 2]
r = np.sqrt(dx2 + dy2 + dz2)
rr = np.rint(80.0/r).astype(int)
for i in range(bx.shape[0]):
pg.draw.circle(surface, "white", (bx[i], by[i]), rr[i])
def main():
ds, clk = initialize()
m = np.asarray([1000.0, 1.0, 1.0, 1.0])
pos = np.asarray([[0.0, 0.0, 0.0],
[-10.0, 0.0, 0.0],
[-20.0, 0.0, 0.0],
[-30.0, 0.0, 0.0]])
vel = np.asarray([[0.0, 0.0, 0.0],
[0.0, 10.0, 0.0],
[0.0, 6.0, 0.0],
[2.0, 2.0, 2.0]])
bodies = Bodies(m, pos, vel)
camera = Camera(np.array([0.0, 0.0, 10.0]),
np.array([0.0, 0.8, 0.0]),
np.array([600.0, 400.0, 100.0]))
transparent = pg.Surface(DISPLAY_RES)
transparent.set_alpha(80)
do_game = True
while do_game:
for event in pg.event.get():
if event.type == pg.QUIT:
do_game = False
# Draw objects
transparent.fill("black")
bodies.draw(transparent, camera)
ds.blit(transparent, (0, 0))
# Perform updates
bodies.update()
# Prepare for next frame
pg.display.flip()
clk.tick(FPS)
pg.quit()
if __name__ == "__main__":
main()