-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgalaxy.py
62 lines (50 loc) · 1.98 KB
/
galaxy.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
from globals import *
from star import Star
class Galaxy(object):
def __init__(self, num_stars, pos, vel, radius, thickness, color):
self.pos = pos
self.vel = vel
self.radius = radius
# Gaussian distributions
sigma_mass = AVG_SOLAR_MASS / 3.0
masses = [clamp(gauss(mu=AVG_SOLAR_MASS, sigma=sigma_mass), MIN_SOLAR_MASS, MAX_SOLAR_MASS)
for i in range(num_stars)]
# Galaxy mass is sum of all stars
self.mass = fsum(masses)
# Gaussian distribution of positions
sigma_x = radius * 0.1
sigma_y = thickness * 0.10
sigma_z = radius * 0.1
# Generate list of all positions
positions = []
for i in range(num_stars):
pos = vector(
clamp(gauss(mu=0, sigma=sigma_x), -radius, radius),
clamp(gauss(mu=0, sigma=sigma_y), -thickness, thickness),
clamp(gauss(mu=0, sigma=sigma_z), -radius, radius)
)
# Limit radius to avoid particles shooting to nowhere
if pos.mag < MIN_ORBITAL_RADIUS:
pos.mag = MIN_ORBITAL_RADIUS
positions.append(pos)
def calc_orbital_velocity(center_mass, radius):
return sqrt(G * center_mass / radius)
# Generate list of all stars
stars = []
up = vector(0.0, 1.0, 0.0)
for i in range(num_stars):
# Find normalized vector along direction of travel
absolute_pos = positions[i] + self.pos
relative_pos = positions[i]
vec = relative_pos.cross(up).norm()
relative_vel = vec * \
calc_orbital_velocity(self.mass, relative_pos.mag)
absolute_vel = relative_vel + vel
stars.append(Star(
mass=masses[i],
radius=STAR_RADIUS,
pos=absolute_pos,
vel=absolute_vel,
color=color
))
self.stars = np.array(stars)