Repository navigation
Expand file tree
/
Copy pathsn2_v2_util.py
More file actions
600 lines (513 loc) · 17.5 KB
/
Copy pathsn2_v2_util.py
File metadata and controls
600 lines (513 loc) · 17.5 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
"""
This file contains class defs and helper functions for the sn2_v0.2 game.
"""
# import
from fisix import *
from grafix import *
from typing import List
import yaml
"""
Classes
"""
# types
Vec2 = Annotated[NDArray[np.float64], (2,)]
Vec4 = Annotated[NDArray[np.float64], (4,)]
# planet
class sn2_planet:
def __init__(self, fiz: "fizix_thing", graf: "grafix_sphere", name: str):
self.fiz = fiz
self.graf = graf
self.name = name
# spaceship helper classes
class sn2_main_engine:
def __init__(self, t_max_n: float, r_b2e_b: Vec3, t_hat_b: Vec3):
self.t_max_n = t_max_n
self.r_b2e_b = r_b2e_b
self.t_hat_b = t_hat_b
class rcs:
def __init__(self, t_max_n: float, r_b2rcs_b: List[Vec3], t_hat_b: List[Vec3]):
self.t_max_n = t_max_n
self.r_b2rcs_b = r_b2rcs_b
self.t_hat_b = t_hat_b
# spaceship
class sn2_spaceship:
def __init__(
self,
fiz: "fizix_thing",
graf: "grafix_tri_prism",
main_engine: sn2_main_engine,
rcs: rcs,
):
self.fiz = fiz
self.graf = graf
self.main_engine = main_engine
self.rcs = rcs
# calculate moment matrix (its expensive, so only do it once)
num_rcs = len(self.rcs.r_b2rcs_b)
moment_matrix = np.zeros((3, num_rcs))
for i_rcs in range(num_rcs):
moment_matrix[:, i_rcs] = np.linalg.cross(
self.rcs.r_b2rcs_b[i_rcs], self.rcs.t_hat_b[i_rcs]
)
self.pinv_moment_matrix = np.linalg.pinv(moment_matrix)
self.thruster_forces = np.zeros((num_rcs))
self.throttle_pct = 0
def add_thrusts(self, cmd_moment_pct_b: Vec3, throttle_pct: float):
# convert percent commands to actual commands
self.throttle_pct = throttle_pct
cmd_moment_b = cmd_moment_pct_b * 2000
throttle = throttle_pct * self.main_engine.t_max_n
# add engine thrust
self.fiz.force_b = self.fiz.force_b + throttle * self.main_engine.t_hat_b
# RCS
# get command scalars
self.thruster_forces = np.clip(
self.pinv_moment_matrix @ cmd_moment_b, 0, self.rcs.t_max_n
)
# get resultant force and moment of firing RCS thrusters
f_rcs_b = np.zeros((3))
m_rcs_b = np.zeros((3))
for i_rcs in range(len(self.rcs.r_b2rcs_b)):
# get force of single thruster t_hat * thrust
cur_f_b = self.rcs.t_hat_b[i_rcs] * self.thruster_forces[i_rcs]
# add to net force
f_rcs_b = f_rcs_b + cur_f_b
# get current moment (r x f) and add to net moment
m_rcs_b = m_rcs_b + np.linalg.cross(self.rcs.r_b2rcs_b[i_rcs], cur_f_b)
self.fiz.momen_b = m_rcs_b
self.fiz.force_b = self.fiz.force_b + f_rcs_b
def draw_thrusts(self, cam: "grafix_camera", screen: pygame.Surface):
# main engine
if self.throttle_pct > 0:
draw_thruster(
throttle_pct=self.throttle_pct,
t_hat_b=self.main_engine.t_hat_b,
pos_thruster_b=self.main_engine.r_b2e_b,
plume_length=10,
jitter_factor=1,
q_g2b=self.fiz.q_g2b,
r_g2b_g=self.fiz.r_g2p_g,
cam=cam,
screen=screen,
)
# rcs
for i in range(len(self.rcs.r_b2rcs_b)):
rcs_throttle_pct = self.thruster_forces[i] / self.rcs.t_max_n
if rcs_throttle_pct > 0:
draw_thruster(
throttle_pct=rcs_throttle_pct,
t_hat_b=self.rcs.t_hat_b[i],
pos_thruster_b=self.rcs.r_b2rcs_b[i],
plume_length=1,
jitter_factor=1,
q_g2b=self.fiz.q_g2b,
r_g2b_g=self.fiz.r_g2p_g,
cam=cam,
screen=screen,
)
# little userInput class
class sn2_userInput:
def __init__(
self, throttle_pct: float, cmd_moment_pct_b: Vec3
):
self.throttle_pct = throttle_pct
self.cmd_moment_pct_b = cmd_moment_pct_b
"""
YAML
"""
# import planets
def import_planets(filepath: str, dt: float):
with open(filepath, "r") as f:
planet_data = yaml.safe_load(f)
# pre-allocate list
planet_list = []
# import planets
for planet_name in planet_data:
# extract data from current planet
cur_planet = planet_data[planet_name]
# build fisix object
cur_fiz = cur_planet["fiz"]
fiz_obj = fizix_thing(
r_g2p_g=np.array(cur_fiz["r_g2p_g"]),
v_g2p_g=np.array(cur_fiz["v_g2p_g"]),
a_g2p_g=np.array([0, 0, 0]),
q_g2b=np.array(cur_fiz["q_g2b"]),
ome_g2b=np.array(cur_fiz["ome_g2b"]),
alp_g2b=np.array([0, 0, 0]),
M=float(cur_fiz["M"]),
I=np.array(cur_fiz["I"]),
force_b=np.array([0, 0, 0]),
momen_b=np.array([0, 0, 0]),
dt=dt,
)
# build grafix object
cur_graf = cur_planet["graf"]
graf_obj = grafix_sphere(
r_g2p_g=np.array(cur_fiz["r_g2p_g"]),
radius=float(cur_graf["radius"]),
color=tuple(cur_graf["color"]),
)
# build planet object
planet_list.append(
sn2_planet(fiz=fiz_obj, graf=graf_obj, name=cur_planet["name"])
)
return planet_list
# import ship
def import_ship(filepath: str, dt: float):
with open(filepath, "r") as f:
ship_data = yaml.safe_load(f)
# build fisix object
cur_fiz = ship_data["fiz"]
fiz_obj = fizix_thing(
r_g2p_g=np.array(cur_fiz["r_g2p_g"]),
v_g2p_g=np.array(cur_fiz["v_g2p_g"]),
a_g2p_g=np.array([0, 0, 0]),
q_g2b=np.array(cur_fiz["q_g2b"]),
ome_g2b=np.array(cur_fiz["ome_g2b"]),
alp_g2b=np.array([0, 0, 0]),
M=float(cur_fiz["M"]),
I=np.array(cur_fiz["I"]),
force_b=np.array([0, 0, 0]),
momen_b=np.array([0, 0, 0]),
dt=dt,
)
# build grafix object
cur_graf = ship_data["graf"]
graf_obj = grafix_tri_prism(
r_g2p_g=np.array(cur_fiz["r_g2p_g"]),
q_g2b=np.array(cur_fiz["q_g2b"]),
side=float(cur_graf["side"]),
height=float(cur_graf["height"]),
color=tuple(cur_graf["color"]),
)
# build engine object
cur_eng = ship_data["main_engine"]
eng_obj = sn2_main_engine(
t_hat_b=np.array(cur_eng["t_hat_b"]),
r_b2e_b=np.array(cur_eng["r_b2e_b"]),
t_max_n=float(cur_eng["t_max_n"]),
)
# build rcs object
cur_rcs = ship_data["rcs"]
r_b2rcs_b_LIST = []
t_hat_b_LIST = []
for i in range(len(cur_rcs["r_b2rcs_b"])):
r_b2rcs_b_LIST.append(np.array(cur_rcs["r_b2rcs_b"][i]))
t_hat_b_LIST.append(np.array(cur_rcs["t_hat_b"][i]))
rcs_obj = rcs(
t_hat_b=t_hat_b_LIST,
r_b2rcs_b=r_b2rcs_b_LIST,
t_max_n=float(cur_rcs["t_max_n"]),
)
# build ship
return sn2_spaceship(
fiz=fiz_obj,
graf=graf_obj,
main_engine=eng_obj,
rcs=rcs_obj,
)
"""
Misc helpers
"""
# create star graphics objects
def make_stars(NUM_STARS: float, STAR_DISTANCE: float):
stars = []
for _ in range(NUM_STARS):
# random azimuth + elevation
az = np.random.uniform(0, 2 * np.pi)
el = np.random.uniform(-np.pi / 2, np.pi / 2)
# convert to cartesian at fixed radius
x = STAR_DISTANCE * np.cos(el) * np.cos(az)
y = STAR_DISTANCE * np.cos(el) * np.sin(az)
z = STAR_DISTANCE * np.sin(el)
stars.append(
grafix_sphere(np.array([x, y, z]), 10000000, pygame.Color("white"))
)
return stars
# get user input
def getKeyInput(throttle_pct: float):
# get state of keyb
keys = pygame.key.get_pressed()
# handle commanded moment
cmd_moment_pct_b = np.array([0.0, 0.0, 0.0])
if keys[pygame.K_w]:
cmd_moment_pct_b[0] = 1
if keys[pygame.K_s]:
cmd_moment_pct_b[0] = -1
if keys[pygame.K_a]:
cmd_moment_pct_b[1] = 1
if keys[pygame.K_d]:
cmd_moment_pct_b[1] = -1
if keys[pygame.K_q]:
cmd_moment_pct_b[2] = -1
if keys[pygame.K_e]:
cmd_moment_pct_b[2] = 1
# handle throttle
if keys[pygame.K_LSHIFT]:
throttle_pct += 1 / 100
if keys[pygame.K_LCTRL]:
throttle_pct -= 1 / 100
throttle_pct = np.clip(throttle_pct, 0, 1)
return sn2_userInput(
throttle_pct=throttle_pct,
cmd_moment_pct_b=cmd_moment_pct_b,
)
# draw_thruster
def draw_thruster(
throttle_pct: float,
t_hat_b: Vec3,
pos_thruster_b: Vec3, # vector from body origin to plume rotation point expressed in body coords.
plume_length: float,
jitter_factor: float,
q_g2b: Quat,
r_g2b_g: Vec3, # vector from global origin to body origin expressed in global coords.
cam: "grafix_camera",
screen: pygame.Surface,
):
# obtain q_b2p
q_b2p = quat_from_to(np.array([0, 0, 1]), -t_hat_b)
# calculate jitter modifier
flame_jitter = (np.random.rand() - 0.5) * throttle_pct * jitter_factor
plume_height = throttle_pct * plume_length + flame_jitter
# transform rotation point 2 plume origin into global frame.
r_r2p_p = np.array([0, 0, plume_height / 2])
r_r2p_g = quatrotate(quatinv(quatmultiply(q_g2b, q_b2p)), r_r2p_p)
# get plume rotation point position in global frame
r_b2r_g = quatrotate(quatinv(q_g2b), pos_thruster_b)
# add them all up
r_g2p_g = r_g2b_g + r_b2r_g + r_r2p_g
# create plume grafix object
grafix_tri_prism(
r_g2p_g=r_g2p_g,
side=plume_length / 10,
height=plume_height,
color=pygame.Color("orange"),
q_g2b=quatmultiply(q_g2b, q_b2p),
).draw(cam, screen)
# keplerian function
def keplerian(ship: sn2_spaceship, planet: sn2_planet, G: float):
# obtain relative position and velocity
R = ship.fiz.r_g2p_g - planet.fiz.r_g2p_g
V = ship.fiz.v_g2p_g - planet.fiz.v_g2p_g
r = np.linalg.norm(R)
v = np.linalg.norm(V)
# calculate intermediate variables
mu = planet.fiz.M * G
H = np.cross(R, V)
h = np.linalg.norm(H)
r_hat = R / r
E = np.cross(V, H) / mu - r_hat # eccentricity vector
epsilon = v**2 / 2 - mu / r # specific orbital energy
# orbital elements
e = np.linalg.norm(E) # eccentricity
i_rad = np.arccos(H[2] / h) # inclination
a = -mu / (2 * epsilon) if epsilon != 0 else np.inf # semi-major axis
b = a * np.sqrt(1 - e**2) if e < 1 else np.nan # semiminor axis
# periapsis & apoapsis
if e < 1:
rp = a * (1 - e)
ra = a * (1 + e)
else:
rp = a * (1 - e)
ra = np.inf
# true anomaly (ν)
# dot(E,R) = e*r*cos(nu)
cos_nu = np.dot(E, R) / (e * r)
sin_nu = np.dot(R, V) / (e * h) # <-- FIX
nu = np.arctan2(sin_nu, cos_nu)
c3 = 2 * epsilon # characteristic energy
alt = r - planet.graf.radius # altitude
return [e, i_rad, rp, ra, c3, v, alt, a, b, nu]
def hud(
hud_pos: Vec2,
ship: sn2_spaceship,
nearest_planet: sn2_planet,
pointing_setting: int,
screen: pygame.Surface,
click_array: Vec3,
font: pygame.font.Font,
font_small: pygame.font.Font,
G: float
):
red = np.array([255, 0, 89])
green = np.array([0, 255, 0])
blue = np.array([0, 157, 255])
"""pointer box definition and click checking"""
# define names of settings
box_texts = [
"OFF", # 0
"SAS", # 1
"PRO", # 2
"RET", # 3
"R-IN", # 4
"R-OUT", # 5
"+H", #6
"-H", # 7
]
# define setting colors
box_text_colors = [
(255,255,255),
(255,255,255),
(0,255,0),
(0,255,0),
(52, 171, 235),
(52, 171, 235),
(177, 52, 235),
(177, 52, 235),
]
# pointing box sizes
box_positions = [
np.array([15, 15]),
np.array([15, 48]),
np.array([15, 81]),
np.array([15, 114]),
np.array([63, 15]),
np.array([63, 48]),
np.array([63, 81]),
np.array([63, 114]),
]
box_size = np.array([40, 24])
# if you're clicking, see if you're in a box. Else, don't change the pointing_setting
mouse_x = click_array[1]
mouse_y = click_array[2]
if click_array[0]:
for i in range(len(box_positions)):
box_min_xy = hud_pos + box_positions[i]
box_max_xy = box_min_xy + box_size
in_x = mouse_x > box_min_xy[0] and mouse_x < box_max_xy[0]
in_y = mouse_y > box_min_xy[1] and mouse_y < box_max_xy[1]
if in_x and in_y:
pointing_setting = i
"""draw actual hud boxes"""
def box(
hud_pos: Vec2,
box_pos: Vec2,
size: Vec2,
screen: pygame.Surface,
border_color: Vec3 = np.array([255, 255, 255]),
inner_color: Vec3 = np.array([0, 0, 0]),
):
box_coords = np.concatenate((np.array([0, 0]), size))
global_pos = hud_pos + box_pos
box_coords[0:2] = box_coords[0:2] + global_pos
inner_box_coords = box_coords + np.array([1.5, 1.5, -3, -3])
pygame.draw.rect(screen, tuple(border_color), tuple(box_coords))
pygame.draw.rect(screen, tuple(inner_color), tuple(inner_box_coords))
# main rectangle
box(hud_pos, np.array([0, 0]), np.array([450, 150]), screen)
# draw the pointing boxes
box(
hud_pos,
np.array([8, 8]),
np.array([105, 135]),
screen,
border_color=red,
)
for i in range(len(box_positions)):
if i == pointing_setting:
box(
hud_pos,
box_positions[i],
box_size,
screen,
inner_color=np.array([100, 100, 100]),
)
else:
box(hud_pos, box_positions[i], box_size, screen)
screen.blit(font.render(box_texts[i],False,box_text_colors[i]),tuple(hud_pos+box_positions[i]+np.array([3,3])))
# draw the orbit vis box
box(
hud_pos,
np.array([120, 8]),
np.array([210, 135]),
screen,
border_color=green,
)
box(hud_pos, np.array([128, 15]), np.array([195, 27]), screen)
# add nearest planet name
screen.blit(font.render(f"Focus: {nearest_planet.name}",False,green),tuple(hud_pos+np.array([128, 15])+np.array([5,3])))
## draw the mini orbit with you on it
# 0 1 2 3 4 5 6 7 8 9
# [e, i_rad, rp, ra, c3, v, alt, a b nu]
box_pos = np.array([128, 49])
box_size = np.array([195, 85])
box(hud_pos, box_pos, box_size, screen)
kep_params = keplerian(ship, nearest_planet, G)
if(kep_params[4] < 0): # bound orbit only
# box geometry
top_left = hud_pos + box_pos
center_pos = top_left + box_size / 2.0
# semimajor/minor axes
a = float(kep_params[7])
b = float(kep_params[8])
e = float(kep_params[0])
nu = float(kep_params[9])
# scale to fit orbit inside box
pad = np.array([8.0, 8.0])
inner_size = box_size - 2*pad
s_orbit = min(inner_size[0] / (2.0*a), inner_size[1] / (2.0*b))
a_scaled = a * s_orbit
b_scaled = b * s_orbit
# focus offset
c = e * a
c_scaled = c * s_orbit
# ellipse top-left for pygame
ellipse_rect = (
int(center_pos[0] - a_scaled),
int(center_pos[1] - b_scaled),
int(2.0 * a_scaled),
int(2.0 * b_scaled),
)
pygame.draw.ellipse(screen, red, ellipse_rect, width=1)
# draw planet at right focus
planet_x = center_pos[0] + c_scaled
planet_y = center_pos[1]
pygame.draw.circle(
screen,
nearest_planet.graf.color,
(int(planet_x), int(planet_y)),
nearest_planet.graf.radius * s_orbit
)
# draw periapsis (left side)
pe_x = center_pos[0] + a_scaled
pe_y = center_pos[1]
pygame.draw.circle(screen, (255, 204, 0), (int(pe_x), int(pe_y)), 3, width=1)
# draw apoapsis (right side)
ap_x = center_pos[0] - a_scaled
ap_y = center_pos[1]
pygame.draw.circle(screen, blue, (int(ap_x), int(ap_y)), 3, width=1)
# draw current spacecraft position (convert ν -> E for center-param ellipse)
E = 2.0 * np.arctan2(np.tan(nu / 2.0), np.sqrt((1.0 + e) / (1.0 - e)))
x_orbit = a_scaled * np.cos(E)
y_orbit = b_scaled * np.sin(E)
ship_dot_x = center_pos[0] + x_orbit
ship_dot_y = center_pos[1] - y_orbit # pygame Y is down!
pygame.draw.circle(screen, green, (int(ship_dot_x), int(ship_dot_y)), 3)
else:
screen.blit(font.render(f"Escape Trajectory!",False,red),tuple(hud_pos+np.array([128, 50])+np.array([5,3])))
# draw the orbit stats box
box(
hud_pos,
np.array([338, 8]),
np.array([105, 135]),
screen,
border_color=blue,
)
# fill in stats box
# build the list of text
stat_texts = [
f"v: {kep_params[5]:+.4e}",
f"alt: {kep_params[6]:+.4e}",
f"ap: {kep_params[3]:+.4e}",
f"pe: {kep_params[2]:+.4e}",
f"i: {np.rad2deg(kep_params[1]):+.4}",
f"e: {kep_params[0]:+.4}",
f"c3: {kep_params[4]:+.4}",
]
# blit it
x_orbit_text = hud_pos[0] + 345
y_orbit_text = hud_pos[1] + 12
for i in range(len(stat_texts)):
screen.blit(font_small.render(stat_texts[i],False,blue),(x_orbit_text,y_orbit_text))
y_orbit_text += 18
return pointing_setting