Advertisement
furas

Python - Spring #2 - (StackOverflow.com)

Jan 26th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. #
  4. # http://stackoverflow.com/questions/41862541/simple-physics-string
  5. #
  6.  
  7. import pygame
  8.  
  9. # --- constants ---
  10.  
  11. WHITE = (255, 255, 255)
  12. BLACK = (  0,   0,   0)
  13.  
  14. FPS = 60
  15.  
  16. # --- classes ---
  17.  
  18. class Node():
  19.  
  20.     def __init__(self, position, mass=100, velocity=None):
  21.  
  22.         # default value for velocity,
  23.         # list can't be used directly in `def`
  24.         if velocity is None:
  25.             velocity = [0, 1]
  26.  
  27.         self.position = pygame.math.Vector2(position)
  28.         self.mass = mass
  29.         self.velocity = pygame.math.Vector2(velocity)
  30.  
  31.     def update(self):
  32.         self.position += self.velocity
  33.  
  34. class String():
  35.  
  36.     def __init__(self, nodes, gravity=.981, spring_constant=10, iterations=1):
  37.         self.nodes = nodes
  38.         self.gravity = gravity
  39.         self.spring_constant = spring_constant
  40.         self.iterations = iterations
  41.         self.set_distance = 1
  42.  
  43.     def calculateForces(self):
  44.         for x in range(self.iterations):
  45.             for i in range(1, len(self.nodes)):
  46.  
  47.                 previous = self.nodes[i-1]
  48.                 current = self.nodes[i]
  49.  
  50.                 dxy = current.position - previous.position
  51.  
  52.                 #distance = current.position.distance_squared_to(previous.position)
  53.                 distance = dxy.distance_squared_to((0,0))
  54.  
  55.                 force = -self.spring_constant * (distance - self.set_distance)
  56.                 force = force / current.mass
  57.  
  58.                 nDistanceVector = dxy * force / distance
  59.  
  60.                 current.velocity = 0.71 * current.velocity + nDistanceVector
  61.  
  62.                 current.velocity.y += self.gravity
  63.  
  64.                 current.update()
  65.  
  66. # --- main ---
  67.  
  68. # - init -
  69.  
  70. pygame.init()
  71.  
  72. screen = pygame.display.set_mode((500, 500))
  73.  
  74. # - objects -
  75.  
  76. a = [Node([250 + i * 10, 100], 140) for i in range(25)]
  77. s = String(a)
  78.  
  79. # - mainloop -
  80.  
  81. clock = pygame.time.Clock()
  82.  
  83. while True:
  84.  
  85.     # - events -
  86.  
  87.     for event in pygame.event.get():
  88.         if event.type == pygame.QUIT:
  89.             pygame.quit()
  90.             quit()
  91.  
  92.     # - updates -
  93.  
  94.     s.calculateForces()
  95.     points = [node.position for node in a]
  96.  
  97.     # - draws -
  98.  
  99.     screen.fill(WHITE)
  100.     pygame.draw.aalines(screen, BLACK, False, points)
  101.     pygame.display.update()
  102.  
  103.     # - FPS -
  104.  
  105.     clock.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement