Verlet integrator, to deal with an argument from the pub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.5KB

  1. # Import the pygame library and initialise the game engine
  2. import pygame
  3. import numpy as np
  4. from verlet import Ball, Spring
  5. pygame.init()
  6. # Initialize Pygame
  7. SIZE = (800, 800)
  8. WHITE = (255, 255, 255)
  9. screen = pygame.display.set_mode(SIZE)
  10. pygame.display.set_caption("Patrick")
  11. running = True
  12. clock = pygame.time.Clock()
  13. def physics():
  14. for ball in balls:
  15. ball.update()
  16. for spring in springs:
  17. spring.update()
  18. def draw():
  19. for ball in balls:
  20. ball.render(screen)
  21. for spring in springs:
  22. spring.render(screen)
  23. def boost():
  24. vector = springs[0].b.pos - springs[0].a.pos
  25. length = springs[0].compute_length()
  26. vector /= length
  27. norm = np.array([vector[1], -vector[0]])
  28. balls[1].velocity += norm * .5
  29. if __name__ == '__main__':
  30. balls = [Ball(0., 0., fixed=True),
  31. Ball(0., 100.),
  32. Ball(20., 120.),
  33. Ball(-20., 120.),
  34. Ball(0., 80.)]
  35. springs = [
  36. Spring(balls[0], balls[1], length=100, k=3),
  37. Spring(balls[1], balls[2], k=5),
  38. Spring(balls[1], balls[3], k=5),
  39. Spring(balls[2], balls[3], k=5),
  40. Spring(balls[1], balls[4], k=5),
  41. Spring(balls[2], balls[4], k=5),
  42. Spring(balls[3], balls[4], k=5)
  43. ]
  44. while running:
  45. for event in pygame.event.get():
  46. if event.type == pygame.QUIT:
  47. running = False
  48. screen.fill((0, 0, 0))
  49. physics()
  50. boost()
  51. draw()
  52. pygame.display.flip()
  53. clock.tick(60)
  54. pygame.quit()