-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloth.py
76 lines (65 loc) · 1.97 KB
/
cloth.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
import math
import numpy as np
import particle as P
import spring as S
class Cloth:
def __init__(self):
x_offset = 0.05
y_offset = 0.05
x_spacing = 0.05
y_spacing = 0.05
self.num_iterations = 2
self.points = []
self.springs = []
num_x_points = self.num_x_points = 20
num_y_points = self.num_y_points = 12
y = y_offset
for i in range(0, num_y_points):
self.points.append([])
x = x_offset
for j in range(0, num_x_points):
point = P.Particle(x, y)
print('x', x, 'y', y)
self.points[i].append(point)
if i > 0:
spring = S.Spring(self.points[i - 1][j], self.points[i][j])
self.springs.append(spring)
if j > 0:
spring = S.Spring(self.points[i][j - 1], self.points[i][j])
self.springs.append(spring)
x = x + x_spacing
y = y + y_spacing
# pin the top right, top middle, and top left.
self.points[0][0].mass = float('inf')
self.points[0][math.floor(num_x_points / 2)].mass = float('inf')
self.points[0][num_x_points - 1].mass = float('inf')
self.num_constraints = len(self.springs)
def update(self):
num_x = self.num_x_points
num_y = self.num_y_points
num_c = self.num_constraints
num_i = self.num_iterations
for i in range(0, num_y):
for j in range(0, num_x):
self.points[i][j].force = np.array([0, 0.005])
# pass #TODO 1: set self.points[i][j].force to fake gravity
for i in range(0, num_i):
for j in range(0, num_c):
self.springs[j].apply_force()
# pass #TODO 2: for each spring, compute its force
for i in range(0, num_y):
for j in range(0, num_x):
self.points[i][j].update()
# pass #TODO 3: update each self.points[i][j] using euler method
def getClosestPoint(self, pos):
min_dist = 1
min_point = None
num_x = self.num_x_points
num_y = self.num_y_points
for i in range(0, num_y):
for j in range(0, num_x):
dist = np.linalg.norm(pos - self.points[i][j].pos)
if dist < min_dist:
min_dist = dist
min_point = self.points[i][j]
return min_point