Tuesday, March 30, 2010

Strizich_VPython_Mobile



from __future__ import division
from visual import *
from visual.controls import *
from visual.graph import *

scene.width=1000
scene.height=400

folcrum=cone(pos=(0,-2.5,0), axis=(0,2,0), radius=1, color=color.green)
#floor = box(pos=(0,-2.5,0), length = 25, height = 0.25, width = 7, color = color.cyan)


device = frame()

ball1 = sphere(frame = device, pos = (-10,0,0), radius = 0.5, color = color.red)
ball2 = sphere(frame = device, pos = (-7.5,0,0), radius = 0.5, color = color.red)
ball3 = sphere(frame = device, pos = (-5,0,0), radius = 0.5, color = color.red)
ball4 = sphere(frame = device, pos = (-2.5,0,0), radius = 0.5, color = color.red)

ball5 = sphere(frame = device, pos = (0,0,0), radius = 0.5, color = color.red)

ball6 = sphere(frame = device, pos = (+2.5,0,0), radius = 0.5, color = color.red)
ball7 = sphere(frame = device, pos = (+5,0,0), radius = 0.5, color = color.red)
ball8 = sphere(frame = device, pos = (+7.5,0,0), radius = 0.5, color = color.red)
ball9 = sphere(frame = device, pos = (+10,0,0), radius = 0.5, color = color.red)

rod = box(frame = device, length = 20, height = 0.25, width = 0.25, color = color.blue) #We'll consider these
#dimensions to be in meters

def world_space_pos(frame, local): #To get the pos in "world space" of balls

x_axis = norm(frame.axis)
z_axis = norm(cross(frame.axis, frame.up))
y_axis = norm(cross(z_axis, x_axis))
return frame.pos+local.x*x_axis+local.y*y_axis+local.z*z_axis


rod.mass=660# Used desity of Oregon Pine Wood...530 kg/m^3...to get masses

omega=0

normalForce=0

ballMass=270
massScale=0.5

ball1.mass=massScale*ballMass
ball2.mass=massScale*ballMass
ball3.mass=massScale*ballMass
ball4.mass=massScale*ballMass

ball6.mass=massScale*ballMass
ball7.mass=massScale*ballMass
ball8.mass=massScale*ballMass
ball9.mass=massScale*ballMass



c = controls(x=0, y=400, width=1000, height=200)
slide1=slider( pos=(-66,-15), width=4, length=30, min=0.0, max=1.0, axis=(0,1,0))
slide2=slider( pos=(-50,-15), width=4, length=30, min=0.0, max=1.0, axis=(0,1,0))
slide3=slider( pos=(-34,-15), width=4, length=30, min=0.0, max=1.0, axis=(0,1,0))
slide4=slider( pos=(-18,-15), width=4, length=30, min=0.0, max=1.0, axis=(0,1,0))

slide6=slider( pos=(18,-15), width=4, length=30, min=0.0, max=1.0, axis=(0,1,0))
slide7=slider( pos=(34,-15), width=4, length=30, min=0.0, max=1.0, axis=(0,1,0))
slide8=slider( pos=(50,-15), width=4, length=30, min=0.0, max=1.0, axis=(0,1,0))
slide9=slider( pos=(66,-15), width=4, length=30, min=0.0, max=1.0, axis=(0,1,0))


slide1.value=massScale
slide2.value=massScale
slide3.value=massScale
slide4.value=massScale

slide6.value=massScale
slide7.value=massScale
slide8.value=massScale
slide9.value=massScale


while 1:
rate (100)
c.interact()

ball1.mass=slide1.value*ballMass
ball2.mass=slide2.value*ballMass
ball3.mass=slide3.value*ballMass
ball4.mass=slide4.value*ballMass

ball6.mass=slide6.value*ballMass
ball7.mass=slide7.value*ballMass
ball8.mass=slide8.value*ballMass
ball9.mass=slide9.value*ballMass

g=9.8

ball1.torque=ball1.mass*world_space_pos(device, ball1).x*g
ball2.torque=ball2.mass*world_space_pos(device, ball2).x*g
ball3.torque=ball3.mass*world_space_pos(device, ball3).x*g
ball4.torque=ball4.mass*world_space_pos(device, ball4).x*g

ball6.torque=ball6.mass*world_space_pos(device, ball6).x*g
ball7.torque=ball7.mass*world_space_pos(device, ball7).x*g
ball8.torque=ball8.mass*world_space_pos(device, ball8).x*g
ball9.torque=ball9.mass*world_space_pos(device, ball9).x*g

torqueNet=ball1.torque+ball2.torque+ball3.torque+ball4.torque+ball6.torque+ball7.torque+ball8.torque+ball9.torque-normalForce

Ipivot=((20*20)*(rod.mass))/12

alpha=torqueNet/Ipivot

omega=omega+alpha*0.01

theta=-omega*0.01

device.rotate(angle=theta, axis=(0,0,1), origin=(0,0,0))

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

Strizich_VPython_Collisions



from visual import *
from visual.controls import *
from visual.graph import *

ballOne = sphere(pos=(1,0,0), radius=0.4, color=color.red)
ballTwo = sphere(pos=(-1,-2,0), radius=0.4, color=color.blue)
wallR = box(pos=(6,0,0), size=(0.2,12,12), color=color.green)
wallL = box(pos=(-6,0,0), size=(0.2,12,12), color=color.green)
wallTop = box(pos=(0,6,0), size=(12,0.2,12), color=color.green)
wallBot = box(pos=(0,-6,0), size=(12,0.2,12), color=color.green)
#wallBack = box(pos=(0,0,-6), size=(12,12,0.2), color=color.green)


ballOne.velocity=vector(-5,-5,0)
ballTwo.velocity=vector(0,0,0)
mOne=1
mTwo=1
deltat = 0.005
coeffRest=1
t = 0

c = controls(x=429, y=0)
slideC=slider( pos=(14,14), width=7, length=70, min=0.0, max=1.0, axis=(0,1,0))#, action=lambda: change(slideC.value) )


#def change(x):
# coeffRest=x

slideC.value=coeffRest

graph1 = gdisplay(x=429, y=300, width=600, height=350,
title='Red "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)
ballRY = gcurve(gdisplay = graph1, color = color.black)

graph2 = gdisplay(x=429, y=650, width=600, height=350,
title='Red "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)
ballRX = gcurve(gdisplay = graph2, color = color.black)

graph3 = gdisplay(x=1029, y=300, width=600, height=350,
title='Blue "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)
ballBY = gcurve(gdisplay = graph3, color = color.black)

graph4 = gdisplay(x=1029, y=650, width=600, height=350,
title='Blue "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)
ballBX = gcurve(gdisplay = graph4, color = color.black)

while t<20:
rate(100)
c.interact()
coeffRest = slideC.value

#Ball One

if ballOne.pos.x > wallR.pos.x-0.5:
ballOne.velocity.x = -ballOne.velocity.x*coeffRest
if ballOne.pos.x < wallL.pos.x+0.5:
ballOne.velocity.x = -ballOne.velocity.x*coeffRest
if ballOne.pos.y > wallTop.pos.y-0.5:
ballOne.velocity.y = -ballOne.velocity.y*coeffRest
if ballOne.pos.y < wallBot.pos.y+0.5:
ballOne.velocity.y = -ballOne.velocity.y*coeffRest
ballOne.pos=ballOne.pos + ballOne.velocity*deltat

#Ball Two

if ballTwo.pos.x > wallR.pos.x-0.5:
ballTwo.velocity.x = -ballTwo.velocity.x*coeffRest
if ballTwo.pos.x < wallL.pos.x+0.5:
ballTwo.velocity.x = -ballTwo.velocity.x*coeffRest
if ballTwo.pos.y > wallTop.pos.y-0.5:
ballTwo.velocity.y = -ballTwo.velocity.y*coeffRest
if ballTwo.pos.y < wallBot.pos.y+0.5:
ballTwo.velocity.y = -ballTwo.velocity.y*coeffRest
ballTwo.pos=ballTwo.pos + ballTwo.velocity*deltat

#Ball to Ball Collisions

if ((((ballTwo.pos.x-ballOne.pos.x)*(ballTwo.pos.x-ballOne.pos.x))+ ((ballTwo.pos.y-ballOne.pos.y)*(ballTwo.pos.y-ballOne.pos.y))) < ((ballOne.radius+ballTwo.radius)*(ballOne.radius+ballTwo.radius))):
lamdaV = atan2((ballOne.velocity.y - ballTwo.velocity.y),(ballOne.velocity.x - ballTwo.velocity.x))

lamdaXY= atan2((ballTwo.pos.y-ballOne.pos.y),(ballTwo.pos.x-ballOne.pos.x))

distance= sqrt(((ballTwo.pos.x-ballOne.pos.x)*(ballTwo.pos.x-ballOne.pos.x))+ ((ballTwo.pos.y-ballOne.pos.y)*(ballTwo.pos.y-ballOne.pos.y)))

alpha=asin(distance*sin(lamdaXY-lamdaV)/(ballOne.radius+ballTwo.radius))

a=tan(lamdaV+alpha)

dvx = 2*(ballOne.velocity.x-ballTwo.velocity.x + a*(ballOne.velocity.y-ballTwo.velocity.y)) / ((1+(a*a))*(1+mTwo /mOne ))

ballTwo.velocity.x = ballTwo.velocity.x + dvx
ballTwo.velocity.y = ballTwo.velocity.y + (a*dvx)
ballOne.velocity.x = ballOne.velocity.x - ((mTwo/mOne)*dvx)
ballOne.velocity.y = ballOne.velocity.y - (a*(mTwo/mOne)*dvx)

#Graph the Velocities
ballRY.plot(pos = (t, ballOne.velocity.y))
ballRX.plot(pos = (t, ballOne.velocity.x))
ballBY.plot(pos = (t, ballTwo.velocity.y))
ballBX.plot(pos = (t, ballTwo.velocity.x))

t=t+deltat