from visual import * #VPython simulation of the Monkey and Gun problem #Starting properties scene.width = 800 scene.height = 600 ball = sphere(pos=(110,140,0), color=color.red) ball.radius = 10 ball.velocity = vector(0,0,0) bullet = sphere(pos=(-110,0,0), color=color.blue) bullet.radius = 10 bullet.velocity = 2.7*vector(82,14,0) #This are just sets up axes and labels xaxis = arrow(pos=(-110,0,0),axis=(250,0,0),shaftwidth=0.1) xlabel = label(pos=xaxis.pos+vector(225,0,0), text='x', xoffset=0, yoffset=10,zoffset=10, height=20, line = 0, box = 0) yaxis = arrow(pos=(-110,0,0),axis=(0,250,0),shaftwidth=0.1) ylabel = label(pos=yaxis.pos+vector(0,225,0), text='y', xoffset=10, yoffset=0,height=20, line = 0, box = 0) zaxis = arrow(pos=(-110,0,0),axis=(0,0,250),shaftwidth=0.1) zlabel = label(pos=xaxis.pos+vector(0,0,225), text='z', xoffset=10, yoffset=10,zoffset = 0,height=20, line = 0, box = 0) #autoscale = 0 prevents automatic re-scaling - do it after the inial set-up scene.autoscale = 0 dt = 0.025 g = 10 #hit checks for a collision hit = vector(ball.pos-bullet.pos) bullet.trajectory = curve(color=color.green, radius = 2) ball.trajectory = curve(color=color.red, radius = 2) while mag(hit)>0.5: rate(75) if scene.mouse.clicked: hit = vector(ball.pos-bullet.pos) ball.pos = ball.pos + ball.velocity*dt bullet.pos = bullet.pos + bullet.velocity*dt ball.velocity.y = ball.velocity.y - g*dt bullet.velocity.y = bullet.velocity.y -g*dt bullet.trajectory.append(pos=bullet.pos) ball.trajectory.append(pos=ball.pos) #print ball.velocity.y