Browse Source

Working, job done

Pete Shadbolt 6 months ago
parent
commit
648c993360
4 changed files with 22 additions and 8 deletions
  1. 2
    2
      README.md
  2. BIN
      demo.webm
  3. 17
    2
      main.py
  4. 3
    4
      verlet.py

+ 2
- 2
README.md View File

@@ -1,3 +1,3 @@
1
-# patrick_wheels
1
+# Driven coupled parametric oscillators
2 2
 
3
-Simulating Patrick's wheels
3
+Settling a discussion from the pub.

BIN
demo.webm View File


+ 17
- 2
main.py View File

@@ -12,6 +12,8 @@ pygame.display.set_caption("Patrick")
12 12
 running = True
13 13
 clock = pygame.time.Clock()
14 14
 
15
+data = []
16
+
15 17
 
16 18
 def physics():
17 19
     for ball in balls:
@@ -36,13 +38,22 @@ def boost():
36 38
     norm = np.array([vector[1], -vector[0]])
37 39
     balls[1].velocity += norm * .5
38 40
 
41
+def modulate():
42
+    x = pygame.mouse.get_pos()[0] 
43
+    fraction = x / SIZE[0]
44
+    springs[-1].k = fraction*3 + 0.05
45
+    springs[-2].k = fraction*3 + 0.05
46
+    pygame.draw.rect(screen, (30, 30, 30), (0, 0, x, SIZE[1]))
47
+
39 48
 
40 49
 if __name__ == '__main__':
41 50
     balls = [Ball(0., 0., fixed=True),
42 51
              Ball(0., 100.),
43 52
              Ball(20., 120.),
44 53
              Ball(-20., 120.),
45
-             Ball(0., 80.)]
54
+             Ball(0., 80.),
55
+             Ball(40., 100.),
56
+             Ball(-40., 100.)]
46 57
     springs = [
47 58
         Spring(balls[0], balls[1], length=100, k=3),
48 59
         Spring(balls[1], balls[2], k=5),
@@ -50,7 +61,9 @@ if __name__ == '__main__':
50 61
         Spring(balls[2], balls[3], k=5),
51 62
         Spring(balls[1], balls[4], k=5),
52 63
         Spring(balls[2], balls[4], k=5),
53
-        Spring(balls[3], balls[4], k=5)
64
+        Spring(balls[3], balls[4], k=5),
65
+        Spring(balls[3], balls[5], k=.1, color=(255, 0, 0)),
66
+        Spring(balls[2], balls[6], k=.1, color=(255, 0, 0))
54 67
     ]
55 68
 
56 69
     while running:
@@ -60,7 +73,9 @@ if __name__ == '__main__':
60 73
 
61 74
         screen.fill((0, 0, 0))
62 75
         physics()
76
+        physics()
63 77
         boost()
78
+        modulate()
64 79
         draw()
65 80
 
66 81
         pygame.display.flip()

+ 3
- 4
verlet.py View File

@@ -45,17 +45,18 @@ class Ball(object):
45 45
 
46 46
 
47 47
 class Spring(object):
48
-    def __init__(self, a, b, length=None, k=0.3):
48
+    def __init__(self, a, b, length=None, k=0.3, color=WHITE):
49 49
         self.a = a
50 50
         self.b = b
51 51
         self.k = k
52 52
         self.length = self.compute_length() if length is None else length
53
+        self.color = color
53 54
         print(self.length)
54 55
 
55 56
     def render(self, screen):
56 57
         x1, y1 = self.a.pos + CENTER
57 58
         x2, y2 = self.b.pos + CENTER
58
-        pygame.draw.line(screen, WHITE, [x1, y1], [x2, y2])
59
+        pygame.draw.line(screen, self.color, [x1, y1], [x2, y2])
59 60
 
60 61
     def compute_length(self):
61 62
         x1, y1 = self.a.pos + CENTER
@@ -69,5 +70,3 @@ class Spring(object):
69 70
         stretch = (current_length - self.length) / self.length
70 71
         self.a.velocity += stretch * delta * self.k
71 72
         self.b.velocity -= stretch * delta * self.k
72
-
73
-