Repository navigation
Expand file tree
/
Copy pathphysics.py
More file actions
98 lines (71 loc) · 2.87 KB
/
Copy pathphysics.py
File metadata and controls
98 lines (71 loc) · 2.87 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
#where the physics actually happens
import math as maths
# --------------------- CONSTANTS ---------------------
#gravitational constant
#G = (6.674 * 10**-11) * ((6 * 10**24)/(10**27)) #units m3 kg-1 s-2
#real value #scale factor
#G = (6.674 * 10**-11) #real value
G = 1 #gravitational constant (arbitrary units)
WORLD_TO_SCREEN = 1/175 #worldspace to screenspace multiplier
dt = 1/10 #timestep
class Body:
def __init__(self, name, mass, x, y, vx, vy, radius, colour):
self.name = name
self.mass = mass
self.x = x
self.y = y
self.vx = vx
self.vy = vy
self.radius = radius
self.colour = colour
def __str__(self):
return f"{self.name} mass: {self.mass} position: [{self.x},{self.y}] radius: {self.radius} colour: {self.colour} "
def initial_velocity(bodies, G):
#determine initial velocity for stable orbit
for body in bodies: #for each body calculate its initial velocity
velocity = 0
total_vx = 0
total_vy = 0
for other in bodies: #calculate force on body from every other body
if other != body: #and (body.mass/other.mass) < 0.01: #only calculate this if mass of body is much smaller than other body
if not (body.x == other.x and body.y == other.y):
dx = other.x - body.x
dy = other.y - body.y
r = maths.sqrt(dx**2 + dy**2)
else:
dx = 0
dy = 0
r = 0.01
velocity += maths.sqrt((G*other.mass)/r) #find velocity by equating gravitational force with required centripetal force
total_vx += velocity * (-dy/r)
total_vy += velocity * (dx/r)
body.vx = total_vx
body.vy = total_vy
print(f'initial vx is {body.vx}, initial vy is {body.vy}')
return bodies
def euler_gravitation(body, bodies, G, dt):
acc_x = 0
acc_y = 0
for other in bodies:
if other != body:
#calculate distance between bodies
dx = other.x - body.x
dy = other.y - body.y
#print(f'dx: {dx}, dy: {dy}')
r = maths.sqrt(dx**2 + dy**2)
#print(f'r:{r}')
#acceleration
acc = G * (other.mass)/(r**2)
#print(f'acc:{acc}')
acc_x += acc * (dx/r) #using trig ratios rather than trig functions to calculate acc
acc_y += acc * (dy/r)
#print(f'acc_x: {acc_x}, acc_y: {acc_y}')
#velocity
body.vx += acc_x * dt #euler integration further pure yayyy
body.vy += acc_y * dt
#print(f'vx:{body.vx}, vy:{body.vy}')
#position
body.x += body.vx * dt
body.y += body.vy * dt
#print(f'x:{body.x}, y:{body.y}')
return body