66
77# simulation setup
88m = 1000 # mass of particle
9- x = np .array ([0.1 , 0.0 ]) # position of particle
9+ x = np .array ([0.3 , 0.0 ]) # position of particle
1010v = np .array ([0.0 , 0.0 ]) # velocity of particle
11- g = np .array ([0 , - 9.81 ]) # gravitational acceleration
11+ g = np .array ([0.0 , - 10.0 ]) # gravitational acceleration
1212spring_rest_len = 0.3 # rest length of the spring ###
13- spring_stiffness = 1000 # stiffness of the spring ###
14- h = 0.1 # time step size in seconds
13+ spring_stiffness = 1e5 # stiffness of the spring ###
14+ h = 0.01 # time step size in seconds
1515
1616# visualization/rendering setup
1717pygame .init ()
@@ -34,10 +34,9 @@ def screen_projection(x): # convert simulated coordinates to window co
3434 # update the frame to display according to render_FPS
3535 if time_step % int (math .ceil ((1.0 / render_FPS ) / h )) == 0 :
3636 # fill the background with white color, display simulation time at the top,
37- # render a floor at y=-1, draw the spring segment, and render the particle as a circle:
37+ # draw the spring segment, and render the particle as a circle:
3838 screen .fill ((255 , 255 , 255 ))
3939 pygame .display .set_caption ('Current time: ' + f'{ time_step * h : .2f} s' )
40- pygame .draw .aaline (screen , (0 , 0 , 255 ), screen_projection ([- 2 , - 1 ]), screen_projection ([2 , - 1 ]))
4140 pygame .draw .aaline (screen , (0 , 0 , 255 ), screen_projection ([0 , 0 ]), screen_projection (x )) ###
4241 pygame .draw .circle (screen , (0 , 0 , 255 ), screen_projection (x ), 0.1 * scale )
4342 pygame .display .flip () # flip the display
@@ -47,12 +46,7 @@ def screen_projection(x): # convert simulated coordinates to window co
4746 spring_cur_len = math .sqrt (x [0 ] * x [0 ] + x [1 ] * x [1 ]) ###
4847 spring_displacement = spring_cur_len - spring_rest_len ###
4948 spring_force = - spring_stiffness * spring_displacement * (x / spring_cur_len ) ###
50- v += h * (g + spring_force ) / m
49+ v += h * (g + spring_force / m )
5150 x += h * v
5251
53- # pause the simulation when the particle touches on the ground
54- if x [1 ] <= - 1 :
55- input ()
56- break
57-
5852 time_step += 1 # update time step counter
0 commit comments