Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- #
- # http://stackoverflow.com/questions/41862541/simple-physics-string
- #
- import pygame
- # --- constants ---
- WHITE = (255, 255, 255)
- BLACK = ( 0, 0, 0)
- FPS = 60
- # --- classes ---
- class Node():
- def __init__(self, position, mass=100, velocity=None):
- # default value for velocity,
- # list can't be used directly in `def`
- if velocity is None:
- velocity = [0, 1]
- self.position = pygame.math.Vector2(position)
- self.mass = mass
- self.velocity = pygame.math.Vector2(velocity)
- def update(self):
- self.position += self.velocity
- class String():
- def __init__(self, nodes, gravity=.981, spring_constant=10, iterations=1):
- self.nodes = nodes
- self.gravity = gravity
- self.spring_constant = spring_constant
- self.iterations = iterations
- self.set_distance = 1
- def calculateForces(self):
- for x in range(self.iterations):
- for i in range(1, len(self.nodes)):
- previous = self.nodes[i-1]
- current = self.nodes[i]
- dxy = current.position - previous.position
- #distance = current.position.distance_squared_to(previous.position)
- distance = dxy.distance_squared_to((0,0))
- force = -self.spring_constant * (distance - self.set_distance)
- force = force / current.mass
- nDistanceVector = dxy * force / distance
- current.velocity = 0.71 * current.velocity + nDistanceVector
- current.velocity.y += self.gravity
- current.update()
- # --- main ---
- # - init -
- pygame.init()
- screen = pygame.display.set_mode((500, 500))
- # - objects -
- a = [Node([250 + i * 10, 100], 140) for i in range(25)]
- s = String(a)
- # - mainloop -
- clock = pygame.time.Clock()
- while True:
- # - events -
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- # - updates -
- s.calculateForces()
- points = [node.position for node in a]
- # - draws -
- screen.fill(WHITE)
- pygame.draw.aalines(screen, BLACK, False, points)
- pygame.display.update()
- # - FPS -
- clock.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement