-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathverletcloth2.py
187 lines (153 loc) · 5.31 KB
/
verletcloth2.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
import pygame, sys, math
pygame.init()
FPS = 60 # frames per second setting
fpsClock = pygame.time.Clock()
# set up the window
WIDTH = 400
HEIGHT = 300
screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
pygame.display.set_caption('Verlet Simple Cloth System')
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
class Particle:
def __init__(self, x, y, m = 1.0):
self.m = m
self.x = x
self.y = y
self.oldx = x
self.oldy = y
self.newx = x
self.newy = y
self.ax = 0
self.ay = 9.8
self.fixed = False
self.selected = False
def update(self, delta_t):
if self.fixed == False:
# Verlet Integration
self.newx = 2.0 * self.x - self.oldx + self.ax * delta_t * delta_t
self.newy = 2.0 * self.y - self.oldy + self.ay * delta_t * delta_t
self.oldx = self.x
self.oldy = self.y
self.x = self.newx
self.y = self.newy
# Collision Process
if self.x < 0 or self.x > WIDTH:
self.x, self.oldx = self.oldx, self.x
if self.y < 0 or self.y > HEIGHT:
self.y, self.oldy = self.oldy, self.y
if mouse == False:
self.selected = False
if self.selected == True:
self.x, self.y = pygame.mouse.get_pos()
def set_pos(self, pos):
self.x, self.y = pos
def draw(self, surf, size):
if self.selected == True:
color = RED
else:
color = WHITE
pygame.draw.circle(surf, color, (int(self.x), int(self.y)), size)
class Constraint:
def __init__(self, index0, index1):
self.index0 = index0
self.index1 = index1
delta_x = particles[index0].x - particles[index1].x
delta_y = particles[index0].y - particles[index1].y
self.restLength = math.sqrt(delta_x * delta_x + delta_y * delta_y)
def update(self):
delta_x = particles[self.index1].x - particles[self.index0].x
delta_y = particles[self.index1].y - particles[self.index0].y
deltaLength = math.sqrt(delta_x * delta_x + delta_y * delta_y)
diff = (deltaLength - self.restLength)/(deltaLength+ 0.001)
if particles[self.index0].fixed == False:
particles[self.index0].x += 0.5 * diff * delta_x
particles[self.index0].y += 0.5 * diff * delta_y
if particles[self.index1].fixed == False:
particles[self.index1].x -= 0.5 * diff * delta_x
particles[self.index1].y -= 0.5 * diff * delta_y
def draw(self, surf, size):
x0 = particles[self.index0].x
y0 = particles[self.index0].y
x1 = particles[self.index1].x
y1 = particles[self.index1].y
pygame.draw.line(surf, WHITE, (int(x0), int(y0)), (int(x1), int(y1)), size)
def find_particle(pos):
for i in range(len(particles)):
dx = particles[i].x - pos[0]
dy = particles[i].y - pos[1]
if (dx*dx + dy*dy) < 300:
particles[i].selected = True
particles[i].set_pos(pos)
break
delta_t = 0.1
NUM_ITER = 3
mouse = False
mouse_pos = (0, 0)
# create particles
NUM_X = 10
NUM_Y = 10
particles = []
for j in range(NUM_Y):
for i in range(NUM_X):
x = 100 + i * 20.0
y = 50 + j * 20.0
p = Particle(x, y)
particles.append(p)
particles[0].fixed = True
particles[NUM_X-1].fixed = True
particles[(NUM_Y-1) * NUM_X].fixed = True
particles[(NUM_Y) * NUM_X - 1].fixed = True
constraints = []
for j in range(NUM_Y):
for i in range(NUM_X):
if i < (NUM_X - 1):
index0 = i + j * NUM_X
index1 = (i + 1) + j * NUM_X
c = Constraint(index0, index1)
constraints.append(c)
if j < (NUM_Y - 1):
index0 = i + j * NUM_X
index1 = i + (j + 1) * NUM_X
c = Constraint(index0, index1)
constraints.append(c)
for j in range(NUM_Y - 1):
for i in range(NUM_X - 1):
index0 = i + j * NUM_X
index1 = (i + 1) + (j + 1) * NUM_X
c = Constraint(index0, index1)
constraints.append(c)
for i in range(1, NUM_X):
index0 = i + j * NUM_X
index1 = (i - 1) + (j + 1) * NUM_X
c = Constraint(index0, index1)
constraints.append(c)
Running = True
while Running:
screen.fill(BLACK)
# particles update
for i in range(len(particles)):
particles[i].update(delta_t)
# constraints update
for i in range(NUM_ITER):
for ii in range(len(constraints)):
constraints[ii].update()
# particles draw
for i in range(len(particles)):
particles[i].draw(screen, 3)
# constraints draw
for i in range(len(constraints)):
constraints[i].draw(screen, 1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
Running = False
if event.type == pygame.MOUSEBUTTONDOWN:
mouse = True
if event.type == pygame.MOUSEBUTTONUP:
mouse = False
if event.type == pygame.MOUSEMOTION and mouse == True:
find_particle(pygame.mouse.get_pos())
pygame.display.update()
fpsClock.tick(FPS)
pygame.quit()