-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfabric.py
More file actions
86 lines (58 loc) · 3.32 KB
/
Copy pathfabric.py
File metadata and controls
86 lines (58 loc) · 3.32 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
import pygame, math
import utils.config as config
pygame.init()
width,height = config.get_window_size()
window = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
fab_width, fab_height, node_spacing, node_mass, spring_stiffness = config.get_fabric_parameters()
gravity, time_step = config.get_physics_parameters()
x_spacing = (width - (fab_width * node_spacing[0])) / 2
y_spacing = (height - (fab_height * node_spacing[1])) / 2
points = [{"pos": [(x * node_spacing[0]) + x_spacing, (y * node_spacing[1]) + y_spacing], "prev_pos": [(x * node_spacing[0]) + x_spacing, (y * node_spacing[1]) + y_spacing], "pin": (y == 0)} for x in range(fab_width) for y in range(fab_height)]
# print(points)
threads = [[i, i+1, node_spacing[0], spring_stiffness]for i in range(len(points)) if (i+1) % fab_width] + [[i,i+fab_width, node_spacing[1], spring_stiffness] for i in range(len(points)-fab_width)]
# print(threads)
while True:
if any(e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE for e in pygame.event.get()):
exit()
break
window.fill((0,5,10))
mouse_x, mouse_y = pygame.mouse.get_pos()
on_click = pygame.mouse.get_pressed()[0]
for index, value in enumerate(points):
node_x, node_y = value["pos"]
if not value["pin"]:
temp = value["pos"][:]
x, y = value['pos']
prev_x, prev_y = value['prev_pos']
force_x, force_y = 0, 0
if on_click and math.hypot(mouse_x - node_x, mouse_y - node_y) < 10:
force_x, force_y = max(-20, min(20, (mouse_x - node_x) * 0.9)), max(-20, min(20, (mouse_y - node_y) * 0.9))
value['pos'][0] += (value['pos'][0] - value['prev_pos'][0]) + (force_x / node_mass)
value['pos'][1] += value['pos'][1] - value['prev_pos'][1] + ((force_y + gravity[1])/ node_mass)
value['prev_pos'] = temp[:]
for _ in range(5):
for i, j, length, stiffness in threads:
dist_x = points[j]["pos"][0] - points[i]["pos"][0]
dist_y = points[j]["pos"][1] - points[i]["pos"][1]
distance = math.hypot(dist_x, dist_y)
if distance == 0:
continue
force = (distance - length) * stiffness
correction_x = (dist_x / distance) * force * 0.5
correction_y = (dist_y / distance) * force * 0.5
if not points[i]["pin"]:
points[i]["pos"][0] += correction_x
points[i]["pos"][1] += correction_y
if not points[j]["pin"]:
points[j]["pos"][0] -= correction_x
points[j]["pos"][1] -= correction_y
ratio = max(-1, min(1, (distance - length) / 20))
if ratio > 0:
r, g, b = int(255 * ratio), int(255 * (1 - ratio)), 0
else:
r,g,b = 0, int(255 * (1 + ratio)), int(255 * -ratio)
colour = (max(0, min(255, r)),max(0, min(255, g)),max(0, min(255, b)))
pygame.draw.line(window, colour, (points[i]["pos"][0], points[i]["pos"][1]), (points[j]["pos"][0], points[j]["pos"][1]), 2)
pygame.display.flip()
clock.tick(60)