-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathweaving_cloth.py
71 lines (56 loc) · 1.66 KB
/
weaving_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
import pygame, sys, math
pygame.init()
FPS = 40 # frames per second setting
fpsClock = pygame.time.Clock()
# set up the window
WIDTH = 900
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
pygame.display.set_caption('Verlet Particle System')
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
class Particle:
def __init__(self, x, y, m = 1.0):
self.m = m
self.x = x
self.y = y
self.oldx = x - 5
self.oldy = y
self.newx = x
self.newy = y
self.ax = 0
self.ay = 9.8
def update(self, delta_t):
# 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
# Verlet Integrator
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
def draw(self, surf, size):
pygame.draw.circle(surf, WHITE, (int(self.x), int(self.y)), size)
delta_t = 0.1
# create particles
particles = []
p = Particle(0, 0)
particles.append(p)
while True:
#screen.fill(BLACK)
# particle update
for i in range(len(particles)):
particles[i].update(delta_t)
# particle draw
for i in range(len(particles)):
particles[i].draw(screen, 3)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
fpsClock.tick(FPS)