-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpygame_mass_spring_damper.py
60 lines (41 loc) · 1000 Bytes
/
pygame_mass_spring_damper.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 26 23:37:07 2017
@author: kaswan
"""
import pygame, sys, math
pygame.init()
FPS = 60 # frames per second setting
fpsClock = pygame.time.Clock()
# set up the window
screen = pygame.display.set_mode((400, 300), 0, 32)
pygame.display.set_caption('Mass Spring Damper System')
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
delta_t = 0.1
m = 1
k = 0.5 #Spring Constant
c = 0.1 # 1.5 #Damper Constant
x = 0
y = 250
vx = 10
vy = 0
while True:
#screen.fill(BLACK)
fx = 0
fy = -k * (y-150) -c * vy
vx = vx + (fx / m) * delta_t
vy = vy + (fy / m) * delta_t
x = x + vx * delta_t
y = y + vy * delta_t
if x > 400:
screen.fill(BLACK)
x = 0
pygame.draw.circle(screen, WHITE, (int(x), int(y)), 3)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
fpsClock.tick(FPS)