rendered paste body#Collision.tasha.py
#Tasha Glen, tashaatomic@hotmail.com
from visual import*
scene.autoscale=0
ball1 = sphere(pos=(-5.0,0.0,0.0),radius=2.0,color=color.cyan)
ball2 = sphere(pos=(5.0,0.0,0.0),radius=2.0,color=color.yellow)
m1 = input("What is the mass(kg) of the cyan ball?: ")
v1 = input("What is the initial velocity (m/s) of the cyan ball?: ")
m2 = input("What is the mass(kg) of the yellow ball?: ")
v2 = input("What is the initial velocity (m/s) of the yellow ball?: ")
dt = 0.01
ball1.velocity = vector(v1,0.0,0.0)
ball2.velocity = vector(v2,0.0,0.0)
while (1==1):
rate(50)
ball1.pos = ball1.pos + ball1.velocity*dt
ball2.pos = ball2.pos + ball2.velocity*dt
if ball2.x-ball1.x<=4:
ball1.velocity.x = (((m1-m2)*ball1.velocity.x)+(2*m2*ball2.velocity.x))/(m1+m2)
ball2.velocity.x = (((m2-m1)*ball2.velocity.x)+(2*m1*ball1.velocity.x))/(m1+m2)
if ball2.x>9.0:
ball2.velocity.x = -ball2.velocity.x
print ball2.velocity.x
if ball1.x<-9.0:
ball1.velocity.x = -ball1.velocity.x
print ball1.velocity.x