/
body.py
107 lines (94 loc) · 4.23 KB
/
body.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
from physics.vector2 import Vector2
from physics.forces import calc_gravitational_force
# Array
# class Body:
# def __init__(self, position, velocity, mass):
# self.position = position
# self.velocity = velocity
# self.mass = mass
# self.resulting_force = Vector2(0, 0)
# # Will be assigned later
# self.index = None
# self.calculated_bodies_indexes = []
# self.resulting_force = Vector2(0, 0)
#
# def __repr__(self):
# return 'Body at {} with mass of {}kg'.format(self.position, self.mass)
#
# def update_position(self, dt):
# self.position += self.velocity * dt
#
# def update_velocity(self, dt):
# # F_res = m / a
# acceleration = self.resulting_force / self.mass
# delta_velocity = acceleration * dt
# self.velocity += delta_velocity
#
# def get_gravitational_force_to(self, other_body):
# # if other_body.index not in self.calculated_bodies_indexes:
# vector_between = other_body.position - self.position
# distance_between = vector_between.get_length()
#
# direction_of_force = vector_between.get_unitvector()
# magnitude_of_force = calc_gravitational_force(self.mass, other_body.mass, distance_between)
#
# gravitational_force = direction_of_force * magnitude_of_force
# # print(gravitational_force)
# # Add calculated gravitational force to 'the forces_to_other_body' dictionary for optimisation
# other_body.resulting_force += gravitational_force
# other_body.calculated_bodies_indexes.append(self.index)
#
# return gravitational_force
#
# def set_resulting_gravitational_force(self, bodies):
# for body in bodies:
# if body is not self and body.index not in self.calculated_bodies_indexes:
# f_g = self.get_gravitational_force_to(body)
# self.resulting_force += f_g
# self.calculated_bodies_indexes = []
# Dictionary
class Body:
def __init__(self, position, velocity, mass):
self.position = position
self.velocity = velocity
self.mass = mass
self.resulting_force = Vector2(0, 0)
# The index of this dictionary will be the index of the other body in the body array
# The content will be the force from that body
self.forces_to_other_bodies = {}
# Temporary value which will be replaced later
self.index = -1
def __repr__(self):
return 'Body at {} with mass of {}kg'.format(self.position, self.mass)
def update_position(self, dt):
self.position += self.velocity * dt
def update_velocity(self, dt):
# F_res = m / a
acceleration = self.resulting_force / self.mass
delta_velocity = acceleration * dt
self.velocity += delta_velocity
def get_gravitational_force_to(self, other_body):
if other_body.index in self.forces_to_other_bodies:
return self.forces_to_other_bodies[other_body.index]
else:
vector_between = other_body.position - self.position
distance_between = vector_between.get_length()
direction_of_force = vector_between.get_unitvector()
magnitude_of_force = calc_gravitational_force(self.mass, other_body.mass, distance_between)
gravitational_force = direction_of_force * magnitude_of_force
# Add calculated gravitational force to 'the forces_to_other_body' dictionary for optimisation
other_body.forces_to_other_bodies[self.index] = gravitational_force * -1
return gravitational_force
def set_resulting_gravitational_force(self, bodies):
self.resulting_force = Vector2(0, 0)
for body in bodies:
if body is not self:
self.resulting_force += self.get_gravitational_force_to(body)
# After we have calculated the resulting force the values in 'forces_to_other_bodies' have been
# used and are now outdated so we clear the dictionary to save memory
self.forces_to_other_bodies = {}
# Debugging
if __name__ == '__main__':
a = Body(Vector2(10e5, 0), Vector2(0, 0), 1.989e30)
b = Body(Vector2(-10e5, 0), Vector2(0, 0), 1.989e30)
print(b.get_gravitational_force_to(a).get_length())