This repository has been archived by the owner on Feb 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bodies.py
386 lines (322 loc) · 13.5 KB
/
bodies.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
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
import math
# Static Bodies - X position, Y position, Width, Height, Surface Friction
class StaticBody():
def __init__(self, x_pos, y_pos, width, height, material):
self.x_pos = x_pos
self.y_pos = y_pos
self.width = width
self.height = height
self.material_type = material[0]
self.color = material[1]
self.surface_friction = material[2]
self.energy_return = material[3]
self.max_bounce = material[4]
self.materialList = material
self.collision = False
def get_x_pos(self):
return self.x_pos
def get_y_pos(self):
return self.y_pos
def get_width(self):
return self.width
def get_height(self):
return self.height
# Return positional data of body
def get_body_parameters(self):
return [self.x_pos, self.y_pos, self.width, self.height, self.surface_friction, self.energy_return, self.max_bounce]
def get_material_type(self):
return self.material_type
def get_material_list(self):
return self.materialList
def get_color(self):
return self.color
def set_x_pos(self, x_pos):
self.x_pos = x_pos
def set_y_pos(self, y_pos):
self.y_pos = y_pos
# List of body Parameters - Masks are rectangular defined by [x_pos, y_pos, body_width, body_height]
def detect_collision(self, body_parameters):
self.collision = False
# Collision from the left
if self.x_pos <= body_parameters[0] + body_parameters[2] and self.x_pos >= body_parameters[0]:
if self.y_pos + self.height >= body_parameters[1] and self.y_pos <= body_parameters[1]:
self.collision = True
elif self.y_pos <= body_parameters[1] + body_parameters[3] and self.y_pos >= body_parameters[1]:
self.collision = True
# Collision from the right
elif self.x_pos + self.width >= body_parameters[0] and self.x_pos <= body_parameters[0]:
if self.y_pos + self.height >= body_parameters[1] and self.y_pos <= body_parameters[1]:
self.collision = True
elif self.y_pos <= body_parameters[1] + body_parameters[3] and self.y_pos >= body_parameters[1]:
self.collision = True
# Kinematic Bodies - Vectors, Gravity, Acceleration, Max Velocities, Frictional Constants
class KinematicBody(StaticBody):
MAX_COEFFICIENT_FRICTION = 0.9
MIN_X_VELO = 0.3
MIN_Y_VELO = 0.3
MIN_BOUNCE_VELO = 2
YF_RATIO = 0.1
FRICTION_THRESHOLD = 0.1
def __init__(self, x_pos, y_pos, width, height, material, mass, gravity_magnitude, max_x_velo, max_y_velo, air_friction):
super().__init__(x_pos, y_pos, width, height, material)
self.mass = mass
self.gravity_magnitude = gravity_magnitude
self.max_x_velo = max_x_velo
self.max_y_velo = max_y_velo
self.x_velo = 0
self.y_velo = 0
self.accel_x = 0
self.accel_y = 0
self.prev_x_velo = 0
self.prev_y_velo = 0
self.air_friction = air_friction
self.temp_friction_x = 0
self.temp_friction_y = 0
self.future_x_velo = None
self.future_y_velo = None
self.collision_records = {
"t" : False,
"b" : False,
"l" : False,
"r" : False
}
def get_x_velo(self):
return self.x_velo
def get_y_velo(self):
return self.y_velo
def get_prev_x(self):
return self.prev_x_velo
def get_prev_y(self):
return self.prev_y_velo
def get_mass(self):
return self.mass
def set_x_vector(self, magnitude_x):
self.x_velo = magnitude_x
# Give body vectors
def set_y_vector(self, magnitude_y):
self.y_velo = magnitude_y
def temp_max_x_vector(self, max_min, magnitude):
if max_min == "max" and self.x_velo > magnitude:
self.x_velo = magnitude
elif max_min == "min" and self.x_velo < magnitude:
self.x_velo = magnitude
def temp_max_y_vector(self, max_min, magnitude):
if max_min == "max" and self.y_velo > magnitude:
self.y_velo = magnitude
elif max_min == "min" and self.y_velo < magnitude:
self.y_velo = magnitude
# Give body acceleration
def accelerate(self, magnitude_x, magnitude_y):
self.accel_x += magnitude_x
self.accel_y += magnitude_y
def apply_force(self, force_x, force_y):
self.accel_x += force_x / self.mass
self.accel_y += force_y / self.mass
def apply_momentum(self, momentum_x, momentum_y):
self.x_velo += momentum_x
self.y_velo += momentum_y
def get_x_force(self):
return self.accel_x * self.mass
def get_y_force(self):
return self.accel_y * self.mass
def gravity(self):
self.accelerate(0, self.gravity_magnitude)
def temp_x_friction(self, temp_friction_x):
self.temp_friction_x = temp_friction_x
def temp_y_friction(self, temp_friction_y):
self.temp_friction_y = temp_friction_y
def friction_x(self, x_velo, prev_x, temp_friction_x, air_friction):
if abs(prev_x) > KinematicBody.MIN_X_VELO:
if air_friction == True:
if abs(x_velo) > KinematicBody.MIN_X_VELO:
self.friction_force_x = (self.mass * x_velo * self.air_friction)
else:
self.friction_force_x = x_velo * self.mass
elif temp_friction_x != None:
if abs(x_velo) > KinematicBody.MIN_X_VELO:
self.friction_force_x = (self.mass * x_velo * temp_friction_x)
else:
self.friction_force_x = x_velo * self.mass
elif abs(self.x_velo) > KinematicBody.MIN_X_VELO:
self.friction_force_x = x_velo * self.mass
elif self.accel_x == 0:
self.friction_force_x = x_velo * self.mass
else:
self.friction_force_x = 0
self.temp_x_friction(None)
return -self.friction_force_x
def friction_y(self, y_velo, prev_y, temp_friction_y, air_friction):
if abs(prev_y) > KinematicBody.MIN_Y_VELO:
if air_friction == True:
if abs(y_velo) > KinematicBody.MIN_Y_VELO:
self.friction_force_y = (self.mass * y_velo * self.air_friction)
else:
self.friction_force_y = y_velo * self.mass
elif temp_friction_y != None:
if abs(y_velo) > KinematicBody.MIN_Y_VELO:
self.friction_force_y = (self.mass * y_velo * temp_friction_y)
else:
self.friction_force_y = y_velo * self.mass
elif abs(y_velo) > KinematicBody.MIN_Y_VELO:
self.friction_force_y = y_velo * self.mass
elif self.accel_y == 0:
self.friction_force_y = y_velo * self.mass
else:
self.friction_force_y = 0
self.temp_y_friction(None)
return -self.friction_force_y
def detect_collision(self, staticBodies, *args):
KinematicCollision = False
x_collision = False
net_x = self.x_velo
net_y = self.y_velo
difference_x = self.x_velo
difference_y = self.y_velo
prev_difference_x = self.prev_x_velo
prev_difference_y = self.prev_y_velo
for body in list(args) + staticBodies:
body_parameters = body.get_body_parameters()
super().detect_collision(body_parameters)
material_type = body.get_material_type()
self.sides = {"top": self.y_pos, "bottom": self.y_pos + self.height, "left": self.x_pos, "right": self.x_pos + self.width}
body_sides = {"top": body_parameters[1], "bottom": body_parameters[1] + body_parameters[3], "left": body_parameters[0], "right": body_parameters[0] + body_parameters[2]}
if self.collision == True:
bodyMaterial = body.get_material_list()
side_collisions = { "b": abs(self.sides["bottom"] - body_sides["top"]),
"t": abs(self.sides["top"] - body_sides["bottom"]),
"l": abs(self.sides["left"] - body_sides["right"]),
"r": abs(self.sides["right"] - body_sides["left"])
}
if type(body).__name__ != "KinematicBody":
if abs(self.x_velo) > KinematicBody.MIN_BOUNCE_VELO:
self.bounce_x = abs(self.x_velo) * body_parameters[5]
else:
self.bounce_x = 0
if abs(self.y_velo) > KinematicBody.MIN_BOUNCE_VELO:
self.bounce_y = abs(self.y_velo) * body_parameters[5]
else:
self.bounce_y = 0
if self.bounce_x > body_parameters[6]:
self.bounce_x = body_parameters[6]
if self.bounce_y > body_parameters[6]:
self.bounce_y = body_parameters[6]
if material_type != "Fluid":
if side_collisions["t"] < side_collisions["b"] and side_collisions["t"] < side_collisions["l"] and side_collisions["t"] < side_collisions["r"]:
self.temp_max_y_vector("max", 0)
self.set_y_pos(body_parameters[1] + body_parameters[3])
self.temp_x_friction(body_parameters[4])
self.y_velo += -self.bounce_y
self.collision_records["t"] = True
elif side_collisions["b"] < side_collisions["t"] and side_collisions["b"] < side_collisions["l"] and side_collisions["b"] < side_collisions["r"]:
self.temp_max_y_vector("min", 0)
self.set_y_pos(body_parameters[1] - self.height)
self.temp_x_friction(body_parameters[4])
self.y_velo += self.bounce_y
self.collision_records["b"] = True
elif side_collisions["l"] < side_collisions["r"] and side_collisions["l"] < side_collisions["t"] and side_collisions["l"] < side_collisions["b"]:
self.temp_max_x_vector("min", 0)
self.set_x_pos(body_parameters[0] + body_parameters[2])
self.temp_y_friction(body_parameters[4])
self.x_velo += self.bounce_x
self.collision_records["l"] = True
elif side_collisions["r"] < side_collisions["l"] and side_collisions["r"] < side_collisions["t"] and side_collisions["r"] < side_collisions["b"]:
self.temp_max_x_vector("max", 0)
self.set_x_pos(body_parameters[0] - self.width)
self.temp_y_friction(body_parameters[4])
self.x_velo += -self.bounce_x
self.collision_records["r"] = True
else:
self.temp_x_friction(body_parameters[4])
self.temp_y_friction(body_parameters[4])
else:
KinematicCollision = True
mass = body.get_mass()
net_mass = self.mass + mass
self.systemic_mass = self.mass / net_mass
if side_collisions["t"] < side_collisions["b"] and side_collisions["t"] < side_collisions["l"] and side_collisions["t"] < side_collisions["r"]:
if body.get_y_velo() < self.prev_y_velo:
if body.collision_records["b"] == False:
net_y = self.y_velo + body.get_y_velo()
difference_x = self.x_velo - body.get_x_velo()
prev_difference_x = self.prev_x_velo - body.get_prev_x()
self.y_velo = net_y * (1 - self.systemic_mass)
else:
self.y_velo = 0
self.y_pos = body_parameters[1] + body_parameters[3]
elif side_collisions["b"] < side_collisions["t"] and side_collisions["b"] < side_collisions["l"] and side_collisions["b"] < side_collisions["r"]:
if body.get_y_velo() > self.prev_y_velo:
if body.collision_records["t"] == False:
net_y = self.y_velo + body.get_y_velo()
difference_x = self.x_velo - body.get_x_velo()
prev_difference_x = self.prev_x_velo - body.get_prev_x()
self.y_velo = net_y * (1 - self.systemic_mass)
else:
self.y_velo = 0
self.y_pos = body_parameters[1] - self.height
elif side_collisions["l"] < side_collisions["r"] and side_collisions["l"] < side_collisions["t"] and side_collisions["l"] < side_collisions["b"]:
x_collision = True
if body.get_x_velo() > self.prev_x_velo:
if body.collision_records["r"] == False:
net_x = self.x_velo + body.get_x_velo()
difference_y = self.y_velo - body.get_y_velo()
prev_difference_y = self.prev_y_velo - body.get_prev_y()
self.x_velo = net_x * (1 - self.systemic_mass)
else:
self.x_velo = 0
self.x_pos = body_parameters[0] + body_parameters[2]
elif side_collisions["r"] < side_collisions["l"] and side_collisions["r"] < side_collisions["t"] and side_collisions["r"] < side_collisions["b"]:
x_collision = True
if body.get_x_velo() < self.prev_x_velo:
if body.collision_records["l"] == False:
net_x = self.x_velo + body.get_x_velo()
difference_y = self.y_velo - body.get_y_velo()
prev_difference_y = self.prev_y_velo - body.get_prev_y()
self.x_velo = net_x * (1 - self.systemic_mass)
else:
self.x_velo = 0
self.x_pos = body_parameters[0] - self.width
if KinematicCollision == True:
KinematicCollision = False
if x_collision == True:
self.apply_force(0, self.friction_y(difference_y, prev_difference_y, body_parameters[4], False))
else:
self.apply_force(self.friction_x(difference_x, prev_difference_x, body_parameters[4], False), 0)
def static_collision_side(self):
return self.side_collided
def interact(self, *args):
self.detect_collision(*args)
self.apply_force(self.friction_x(self.x_velo, self.prev_x_velo, self.temp_friction_x, False), self.friction_y(self.y_velo, self.prev_y_velo, self.temp_friction_y, False))
# Update postion of body with vectors
def update(self, *args):
self.gravity()
self.apply_force(self.friction_x(self.x_velo, self.prev_x_velo, self.temp_friction_x, True), self.friction_y(self.y_velo, self.prev_y_velo, self.temp_friction_y, True))
self.x_velo += self.accel_x
self.y_velo += self.accel_y
if abs(self.x_velo) > self.max_x_velo:
self.x_velo = self.max_x_velo * (abs(self.x_velo) / self.x_velo)
elif self.future_x_velo != None:
if abs(self.future_x_velo) > self.max_x_velo:
self.future_x_velo = self.max_x_velo * (abs(self.future_x_velo) / self.future_x_velo)
self.x_velo = self.future_x_velo
else:
self.x_velo = self.future_x_velo + self.accel_x
if abs(self.y_velo) > self.max_y_velo:
self.y_velo = self.max_y_velo * (abs(self.y_velo) / self.y_velo)
elif self.future_y_velo != None:
if abs(self.future_y_velo) > self.max_y_velo:
self.future_y_velo = self.max_y_velo * (abs(self.future_y_velo) / self.future_y_velo)
self.y_velo = self.future_y_velo
else:
self.y_velo = self.future_y_velo + self.accel_y
self.x_pos += self.x_velo
self.y_pos -= self.y_velo
self.prev_y_velo = self.y_velo
self.prev_x_velo = self.x_velo
for key in self.collision_records:
self.collision_records[key] = False
self.accel_x = 0
self.accel_y = 0
self.future_x_pos = None
self.future_y_pos = None
self.future_x_velo = None
self.future_y_velo = None