CodeCodeCode

Crazy bouncing cart

Mar 29th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. from __future__ import division
  2. from visual import *
  3. scene.y = 400 # move animation window 400 pixels from top of screen
  4. from visual.graph import *
  5.  
  6. track = box(pos = vector(0.0, -0.05, 0.0), size = (1.0, 0.05, 0.10))
  7. cart = box(pos = vector(0.0, 0.04, 0.0), size = (0.1, 0.04, 0.06), color = color.green)
  8.  
  9. cart.m = 0.80
  10. cart.v = vector(0.7, 1.0 ,0.0)
  11. cart.p = cart.m * cart.v
  12.  
  13. deltat = 0.005
  14. t = 0
  15.  
  16. #position vs time graph
  17. gdisplay(xtitle = 'Time', ytitle = 'Position', x = 500, y = 0, width = 600, height = 300)
  18. posgraph = gcurve(color = color.red) # to make position graph
  19. #momentum vs time graph
  20. gdisplay(xtitle = 'Time', ytitle = 'Momentum', x = 500, y = 300, width = 600, height = 300)
  21. pgraph = gcurve(color = color.green) # to make momentum graph
  22. #net force vs time graph
  23. gdisplay(xtitle = 'Time', ytitle = 'Net Force', x = 500, y = 600, width = 600, height = 300)
  24. Fgraph = gcurve(color = color.blue) # to make net force graph
  25.  
  26.  
  27. while t < 5.0:
  28.     rate(100)
  29.     Fnet = vector(-0.4, -7.84, 0.0)
  30.     cart.p = cart.p + Fnet * deltat
  31.     print "t = ", t, "cart.p = ", cart.p, "cart.pos = ", cart.pos
  32.     cart.pos = cart.pos + (cart.p / cart.m) * deltat
  33.     t = t + deltat
  34.     if (cart.pos.y <= 0) & (cart.pos.x >= -.5):
  35.             cart.p.y = abs(cart.p.y) # make the cart move upwards
  36.     posgraph.plot(pos = (t, cart.pos.x))
  37.     pgraph.plot(pos = (t, cart.p.x))
  38.     Fgraph.plot(pos = (t, Fnet.x))
Advertisement
Add Comment
Please, Sign In to add comment