def setup(): size(900, 450) # we will update position & velocity frame-by-frame, # so we store them in these "global" variables y = 1.49 # need to change this to make anything happen! vy = 0.0 def draw(): dt = 0.01 k = 20.0 m = 1.0 g = 9.8 Lrelaxed = 1.0 global y, vy y = y + vy*dt Fy = m*g - k*(y-Lrelaxed) vy = vy + (Fy/m)*dt xbob = 0 ybob = Lrelaxed + y xpixel_anchor = 0.5*width ypixel_anchor = 0.01*height scale = 100.0 xpixel_bob = xpixel_anchor + scale*xbob ypixel_bob = ypixel_anchor + scale*ybob # clear the screen for each new frame background(200) # draw the bob rbob = 15 ellipse(xpixel_bob, ypixel_bob, 2*rbob, 2*rbob) # draw the spring as a series of zig-zag lines nzigzag = 20 for i in range(nzigzag): spring_top = ypixel_anchor spring_bottom = ypixel_bob - rbob dy = (spring_bottom-spring_top)/nzigzag xzig = xpixel_anchor - 20 yzig = ypixel_anchor + i*dy xzag = xpixel_anchor + 20 ymid = yzig + 0.5*dy yzag = yzig + dy line(xzig, yzig, xzag, ymid) line(xzag, ymid, xzig, yzag)