/
Body.py
49 lines (40 loc) · 1.31 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
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import scipy.constants
from Vector import Vector
class Body:
def __init__(self, position, velocity, mass):
self._position = position
self._velocity = velocity
self._mass = mass
def move(self, force, dt): # dt = time elapsed
acceleration = force / self._mass
# self._velocity = self._velocity + acceleration * dt
self._velocity += acceleration * dt
self._position += self._velocity * dt
return self
def force_from(self, other):
G = scipy.constants.G
delta = other._position - self._position
dist = abs(delta)
magnitude = (G * self._mass * other._mass) / (dist ** 2)
return magnitude * delta.direction()
def get_patch(self, radius=None):
if radius is None:
radius = self._mass/22
x, y = self._position
return patches.Circle((x, y), radius, facecolor='black')
def main():
pos = Vector([3, 4])
velocity = Vector([.5, .1])
body = Body(pos, velocity, 50)
body.move(Vector([.2, .1]), 2)
fig, ax = plt.subplots()
ax.axis('equal')
ax.add_patch(body.get_patch())
ax.set_xlim(-10, 10)
ax.set_ylim(-10, 10)
plt.savefig('tes.png')
plt.show()
if __name__ == "__main__":
main()