from visual import * print """ Bruce Sherwood Fall 2000 Illustrates the momentum principle (Newton's 2nd law). Click in right window, then drag force vector with mouse button up. Object in left window moves under the influence of this single force. """ h = w = 500 scene.width = w scene.height = h scene.x = scene.y = 0 wide = 1. scene.fov = 0.001 scene.range = wide ball = sphere(pos=(-0.9*wide,0,0), radius=wide/20, color=color.cyan) ball.mass = 200. ball.p = vector() trail = curve(color=ball.color) dt = 0.1 Foffset = vector(0,0,-ball.radius) Fvec = arrow(pos=ball.pos+Foffset, axis=(0,0,0), shaftwidth=wide/30., color=color.green) pvec = arrow(pos=ball.pos, axis=(0,0,0), shaftwidth=wide/30., color=color.red) Fmouse = 1. # F mouse scale factor Fview = 1. # F view scale factor pview = 0.05 # p view scale factor scene2 = display(x=w, y=0, width=w, height=h) scene2.range = wide Fvec2 = arrow(display=scene2, axis=(0,0,0), shaftwidth=wide/30., color=color.green) sphere(display=scene2, radius=Fvec2.shaftwidth/2., color=Fvec2.color) scene.select() scene2.mouse.getclick() while 1: rate(40) F = Fmouse*scene2.mouse.pos Fvec2.axis = Fview*F ball.p = ball.p+F*dt ball.pos = ball.pos+(ball.p/ball.mass)*dt trail.append(pos=ball.pos) if not (-wide <= ball.x <= wide): trail.visible = 0 trail = curve(color=ball.color) if ball.p.x > 0: ball.x = -wide else: ball.x = wide if not (-wide <= ball.y <= wide): trail.visible = 0 trail = curve(color=ball.color) if ball.p.y > 0: ball.y = -wide else: ball.y = wide Fvec.pos = ball.pos+Foffset Fvec.axis = Fview*F pvec.pos = ball.pos pvec.axis = pview*ball.p