-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulationCore.py
157 lines (138 loc) · 5.4 KB
/
SimulationCore.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
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
import numpy as np
import math
import os
#overwrite pygame welcome message
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "1"
import pygame
#Physical constants
g = 6.647 * 10**(-11)
k = 8.987511 * 10**9
class circle:
def __init__(self, xc, yc, r, color):
self.xc=xc
self.yc=yc
self.color=color
self.r = r
def draw(self,window):
pygame.draw.circle(window, self.color,#np:(0, 0, 255) or "blue",
[self.xc, self.yc], self.r#no outline
)
class massObject(circle):
def __init__(self,xc,yc, r, color, mass = 0, q = 0, v = list([0,0])):
self.r=r
self.xc=xc
self.yc=yc
self.color = color
self.mass = mass
self.v = v
self.q = q
circle.__init__(self,xc,yc,self.r,color)
#Change the input, to be an obj list, not a single obj!
def SimulateGravity(self, obj):
templst = [self.xc, self.yc]
templst2 = [obj.xc, obj.yc]
vctr = np.array(templst)
vctrobj = np.array(templst2)
mag = np.linalg.norm(vctrobj-vctr)
self.a = g*(obj.mass/((mag)**3))*((vctrobj-vctr))
self.v += self.a
#scaling of v
if self.v[0] >= 3 or self.v[1] >= 3:
self.v *= 0.5
self.xc += self.v[0]
self.yc += self.v[1]
#Accepts vector of type string :
#"v", "a", etc.
def DrawVector(self, vector, scale, window):
#Checking which vector to use
#There's better ways of doing this, passing a value is infinitley much better, and more readable than this, methods can just return values which can be used
#example, simgrav can return whichever value the user needed by calling another arg into it that can be checked, simelectric doesnt need that arg, which is even better
if vector == "v":
vector = self.v
elif vector == "a":
vector = self.a
elif vector == "E":
#if self.q != None:
vector = self.E
#else:
# print("bad args")
# return
scaledVector = vector * scale
#Choose the ending position of vector
#WARNING, the numpy vector is made into a list here!!!!
scaledVectorEnd = [scaledVector[0]+self.xc, scaledVector[1]+self.yc]
#draw vector
pygame.draw.line(window,"green",[self.xc, self.yc],scaledVectorEnd,2)
rotation = math.degrees(math.atan2([self.xc, self.yc][1]-scaledVectorEnd[1], scaledVectorEnd[0]-[self.xc, self.yc][0]))+90
#first point is the point that is parallel to the line (or on it), second is the one to the right when the triangle is facing north
pygame.draw.polygon(window, "green", ((scaledVectorEnd[0]+5*math.sin(math.radians(rotation)), scaledVectorEnd[1]+5*math.cos(math.radians(rotation))), (scaledVectorEnd[0]+5*math.sin(math.radians(rotation-120)), scaledVectorEnd[1]+5*math.cos(math.radians(rotation-120))), (scaledVectorEnd[0]+5*math.sin(math.radians(rotation+120)), scaledVectorEnd[1]+5*math.cos(math.radians(rotation+120)))))
def SimElectricField(self, objlist):
self.E = 0
for obj in objlist:
templst = [self.xc, self.yc]
templst2 = [obj.xc, obj.yc]
vctr = np.array(templst)
vctrobj = np.array(templst2)
mag = np.linalg.norm(vctr-vctrobj)
etemp = (obj.q/(mag**2))*(vctr-vctrobj)
self.E += etemp
self.E *= k
#this method should also check for where the value will be drawn, as it sometimes can be drawn outside the visible area
def DrawValue(self, value:int, window):
font = pygame.font.Font(None, 24)
text = font.render(str(value), True, (10, 10, 10))
textpos = text.get_rect(centerx=self.xc, y=self.yc - 20)
window.blit(text, textpos)
#position : list of list
#amount : int
#mass : list
#speed : list of list
#color : list
#q : list
def ObjSpawn(position:list, amount:int, mass, color, q, speed):
objlist = []
for i in range(amount):
#temp var, change name please
xc = int(position[i][0])
yc = int(position[i][1])
if mass == 0:
mass = [0] * amount
if color == 0:
color = ["black"] * amount
if q == 0:
q = [0] * amount
if speed == 0:
speed = [[0, 0]] * amount
objlist.append(massObject(xc, yc, 5, color[i], int(mass[i]),int(q[i]), ConvertListTypeToInt(speed[i])) )
return objlist
def ObjSim(objlist, window, check):
for i in objlist:
i.draw(window)
i.DrawVector("v", 100, window)
if check == "g":
for n in range(len(objlist)):
if objlist[n] != i:
i.SimulateGravity(objlist[n])
#elif check == "e":
# i.SimElectricField(objlist)
#functions below this could really use their own module
#this function splits the string fed into it and makes a list.
def SplitStringIntoList(string):
li = list(string.split(" "))
return li
#rename, makes a list of lists from a list
def afterConvert(list):
pa = []
templist = []
for n in list:
templist = list[:2]
list.pop(0)
list.pop(0)
pa.append(templist)
return pa
#Converts args inside list into int
def ConvertListTypeToInt(list):
templist = []
for i in range(len(list)):
templist.append(int(list[i]))
return templist