Tuesday, March 30, 2010

Strizich_VPython_Kinematics



from visual import *
from visual.graph import *

ballOne=sphere(pos=(-17.5,-5,0), radius=1, color=color.red)
ballOne.velocity=vector(0,0,0)

ballOne.trail = curve(color=ballOne.color)

wallBot = box(pos=(4,-6,0), size=(50,0.2,12), color=color.green)

vscale = 0.1
varr = arrow(pos=ballOne.pos, axis=vscale*ballOne.velocity, color=color.yellow)

t=0
deltat=0.01

pick = None
picked=False
draging=True
firstClick=True

scene.autoscale = True

graph1 = gdisplay(x=429, y=0, width=600, height=350,
title='"Y" Velocity vs. Time', xtitle='time (s)', ytitle='v (m/s)',
xmax=20, xmin=0., ymax=20, ymin=-20,
foreground=color.black, background=color.white)
ballY = gcurve(gdisplay = graph1, color = color.black)

graph2 = gdisplay(x=429, y=350, width=600, height=350,
title='"X" Velocity vs. Time', xtitle='time (s)', ytitle='v (m/s)',
xmax=20, xmin=0., ymax=20, ymin=-20,
foreground=color.black, background=color.white)
ballX = gcurve(gdisplay = graph2, color = color.black)



while True:
rate(100)
if ballOne.pos.y > wallBot.pos.y+ ballOne.radius:

ballOne.pos=ballOne.pos + ballOne.velocity*deltat

if picked:
ballOne.velocity.y=ballOne.velocity.y-(9.8*deltat)

varr.pos=ballOne.pos
varr.axis=vscale*ballOne.velocity

ballOne.trail.append(pos=ballOne.pos)

if scene.mouse.events:
m1 = scene.mouse.getevent() # get event
if m1.drag and m1.pick == ballOne: # if touched ball
drag_pos = m1.pickpos # where on the ball
pick = m1.pick # pick now true (not None)
if firstClick:
posOne=m1.pos
t1=t
firstClick=False
elif m1.drop: # released at end of drag
pick = None# end dragging (None is false)
picked=True
posTwo=m1.pos
t2=t
draging=false
if pick:
# project onto xy plane, even if scene rotated:
new_pos = scene.mouse.project(normal=(0,0,1))
if new_pos != drag_pos: # if mouse has moved
# offset for where the ball was clicked:
pick.pos += new_pos - drag_pos
drag_pos = new_pos # update drag position

if not draging:
velocityScale=0.5
ballOne.velocity.x=velocityScale*((posTwo.x-posOne.x)/(t2-t1))
ballOne.velocity.y=velocityScale*((posTwo.y-posOne.y)/(t2-t1))

draging=True
firstClick=True

#Graph Velocities
ballY.plot(pos = (t, ballOne.velocity.y))
ballX.plot(pos = (t, ballOne.velocity.x))

t=t+deltat

No comments:

Post a Comment