Monday, April 12, 2010

Additional Video

Here's another video for the bowling ball simulation. I tried avoiding the watermark as much as possible on this one.

Winston's Visual Python Project

Sorry about the watermark.
Description: This program simulates a bowling ball rolling down the lane and hitting pins. The bowling ball starts moving due to an initial force. It can be moved to different starting positions and be pushed at different angles to try and hit as many pins as possible. As it rolls, it experiences a frictional force which slows the ball down and also causes it to roll without slipping at different distances depending on the initial force. Lastly, the ball hits pins and knocks them over.

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


Xposition = 0 #ball position start
ball = sphere(pos=(Xposition,1,-20), radius=1.0, material=materials.earth)
floor = box(pos = (0,0,-5), size=(8,.2,40), color = (.48,.20,.10))
pin7 = frame()
pin7t = sphere(frame = pin7, pos = (-2.4,1.9,10), radius = .35)
pin7m = box(frame = pin7, pos = (-2.4,1.5,10), size = (.4,.4,.4), color=color.red, opacity=.8)
pin7b = cylinder(frame = pin7, pos = (-2.4,0,10), axis = (0,1.35,0), radius = .35)
pin10 = frame()
pin10t = sphere(frame = pin10, pos = (2.4,1.9,10), radius = .35)
pin10m = box(frame = pin10, pos = (2.4,1.5,10), size = (.4,.4,.4), color=color.red, opacity=.8)
pin10b = cylinder(frame = pin10, pos = (2.4,0,10), axis = (0,1.35,0), radius = .35)
pin8 = frame()
pin8t = sphere(frame = pin8, pos = (-.8,1.9,10), radius = .35)
pin8m = box(frame = pin8, pos = (-.8,1.5,10), size = (.4,.4,.4), color=color.red, opacity=.8)
pin8b = cylinder(frame = pin8, pos = (-.8,0,10), axis = (0,1.35,0), radius = .35)
pin9 = frame()
pin9t = sphere(frame = pin9, pos = (.8,1.9,10), radius = .35)
pin9m = box(frame = pin9, pos = (.8,1.5,10), size = (.4,.4,.4), color=color.red, opacity=.8)
pin9b = cylinder(frame = pin9, pos = (.8,0,10), axis = (0,1.35,0), radius = .35)
pin6 = frame()
pin6t = sphere(frame = pin6, pos = (1.6,1.9,9), radius = .35)
pin6m = box(frame = pin6, pos = (1.6,1.5,9), size = (.4,.4,.4), color=color.red, opacity=.8)
pin6b = cylinder(frame = pin6, pos = (1.6,0,9), axis = (0,1.35,0), radius = .35)
pin5 = frame()
pin5t = sphere(frame = pin5, pos = (0,1.9,9), radius = .35)
pin5m = box(frame = pin5, pos = (0,1.5,9), size = (.4,.4,.4), color=color.red, opacity=.8)
pin5b = cylinder(frame = pin5, pos = (0,0,9), axis = (0,1.35,0), radius = .35)
pin4 = frame()
pin4t = sphere(frame = pin4, pos = (-1.6,1.9,9), radius = .35)
pin4m = box(frame = pin4, pos = (-1.6,1.5,9), size = (.4,.4,.4), color=color.red, opacity=.8)
pin4b = cylinder(frame = pin4, pos = (-1.6,0,9), axis = (0,1.35,0), radius = .35)
pin2 = frame()
pin2t = sphere(frame = pin2, pos = (-.8,1.9,8), radius = .35)
pin2m = box(frame = pin2, pos = (-.8,1.5,8), size = (.4,.4,.4), color=color.red, opacity=.8)
pin2b = cylinder(frame = pin2, pos = (-.8,0,8), axis = (0,1.35,0), radius = .35)
pin3 = frame()
pin3t = sphere(frame = pin3, pos = (.8,1.9,8), radius = .35)
pin3m = box(frame = pin3, pos = (.8,1.5,8), size = (.4,.4,.4), color=color.red, opacity=.8)
pin3b = cylinder(frame = pin3, pos = (.8,0,8), axis = (0,1.35,0), radius = .35)
pin1 = frame()
pin1t = sphere(frame = pin1, pos = (0,1.9,7), radius = .35)
pin1m = box(frame = pin1, pos = (0,1.5,7), size = (.4,.4,.4), color=color.red, opacity=.8)
pin1b = cylinder(frame = pin1, pos = (0,0,7), axis = (0,1.35,0), radius = .35)

G = 9.8 #m/s^2
Mball = 7.25 #kg
Mpin = 1.6 #kg
coefficient = .15 #friction


ball.velocity = vector(0,0,0)
Fpush = 100 #you can change the force of the push
degree = 0 #you can change the direction of the push
angle = degree * (2*pi)/180
Ffriction = Mball*G*coefficient

ball.acceleration = vector(0,0,0)
ball.acceleration.x = (Ffriction/Mball)
ball.acceleration.y = 0
ball.acceleration.z = (Ffriction)/(Mball)

pin1.velocity = vector(0,0,0)
pin2.velocity = vector(0,0,0)
pin3.velocity = vector(0,0,0)
pin4.velocity = vector(0,0,0)
pin5.velocity = vector(0,0,0)
pin6.velocity = vector(0,0,0)
pin7.velocity = vector(0,0,0)
pin8.velocity = vector(0,0,0)
pin9.velocity = vector(0,0,0)
pin10.velocity = vector(0,0,0)

dt = .01
t = 0

while ball.pos.z<18.5:
rate(100)
t = t + dt
if ball.velocity.z<0:
Ffriction=0
ball.velocity = vector(0,0,0)#makes sure that friction only acts when the ball is moving, also makes sure ball actually stops
if t<.75:
ball.acceleration.z = (Fpush*cos(angle))/(Mball)
ball.velocity.z = ball.velocity.z + ball.acceleration.z*dt
ball.pos.z = ball.pos.z + ball.velocity.z*dt#this is your initial force to get the ball moving
ball.acceleration.x = (Fpush*sin(angle))/Mball
ball.velocity.x = ball.velocity.x + ball.acceleration.x*dt
ball.pos.x = ball.pos.x + ball.velocity.x*dt
if t>.75:
ball.acceleration.z = -(Ffriction*cos(angle))/(Mball)
ball.velocity.z = ball.velocity.z + ball.acceleration.z*dt
ball.pos.z = ball.pos.z + ball.velocity.z*dt#friction
ball.acceleration.x = -(Ffriction*sin(angle))/Mball
ball.velocity.x = ball.velocity.x + ball.acceleration.x*dt
ball.pos.x = ball.pos.x + ball.velocity.x*dt
#pin1
if ball.pos.x + 1.2 >= pin1b.pos.x >= ball.pos.x - 1.2:
if ball.pos.z +1.2 >= pin1b.pos.z >= ball.pos.z -1.2:
ball.velocity = ball.velocity*.999
pin1.velocity = ball.velocity*1.5
pin1.rotate(angle = pi/55, axis = (1,0,0), origin = (pin1t.pos.x,pin1t.pos.y+1,pin1t.pos.z))

#pin2
if ball.pos.x + 1.2 >= pin2b.pos.x >= ball.pos.x - 1.2:
if ball.pos.z + 1.2 >= pin2b.pos.z >= ball.pos.z - 1.2:
ball.velocity = ball.velocity*.999
pin2.velocity = ball.velocity*1.5
pin2.rotate(angle = pi/55, axis = (1,0,0), origin = (pin2t.pos.x,pin2t.pos.y+1,pin2t.pos.z))

#pin3
if ball.pos.x + 1.2 >= pin3b.pos.x >= ball.pos.x - 1.2:
if ball.pos.z + 1.2 >= pin3b.pos.z >= ball.pos.z - 1.2:
ball.velocity = ball.velocity*.999
pin3.velocity = ball.velocity*1.5
pin3.rotate(angle = pi/55, axis = (1,0,0), origin = (pin3t.pos.x,pin3t.pos.y+1,pin3t.pos.z))

#pin4
if ball.pos.x + 1.2 >= pin4b.pos.x >= ball.pos.x - 1.2:
if ball.pos.z + 1.2 >= pin4b.pos.z >= ball.pos.z - 1.2:
ball.velocity = ball.velocity*.999
pin4.velocity = ball.velocity*1.5
pin4.rotate(angle = pi/55, axis = (1,0,0), origin = (pin4t.pos.x,pin4t.pos.y+1,pin4t.pos.z))

#pin5
if ball.pos.x + 1.2 >= pin5b.pos.x >= ball.pos.x - 1.2:
if ball.pos.z + 1.2 >= pin5b.pos.z >= ball.pos.z - 1.2:
ball.velocity = ball.velocity*.999
pin5.velocity = ball.velocity*1.5
pin5.rotate(angle = pi/55, axis = (1,0,0), origin = (pin5t.pos.x,pin5t.pos.y+1,pin5t.pos.z))


#pin6
if ball.pos.x + 1.2 >= pin6b.pos.x >= ball.pos.x - 1.2:
if ball.pos.z + 1.2 >= pin6b.pos.z >= ball.pos.z - 1.2:
ball.velocity = ball.velocity*.999
pin6.velocity = ball.velocity*1.5
pin6.rotate(angle = pi/55, axis = (1,0,0), origin = (pin6t.pos.x,pin6t.pos.y+1,pin6t.pos.z))

#pin7
if ball.pos.x + 1.2 >= pin7b.pos.x >= ball.pos.x - 1.2:
if ball.pos.z + 1.2 >= pin7b.pos.z >= ball.pos.z - 1.2:
ball.velocity = ball.velocity*.999
pin7.velocity = ball.velocity*1.5
pin7.rotate(angle = pi/55, axis = (1,0,0), origin = (pin7t.pos.x,pin7t.pos.y+1,pin7t.pos.z))

#pin8
if ball.pos.x + 1.2 >= pin8b.pos.x >= ball.pos.x - 1.2:
if ball.pos.z + 1.2 >= pin8b.pos.z >= ball.pos.z - 1.2:
ball.velocity = ball.velocity*.999
pin8.velocity = ball.velocity*1.5
pin8.rotate(angle = pi/55, axis = (1,0,0), origin = (pin8t.pos.x,pin8t.pos.y+1,pin8t.pos.z))

#pin9
if ball.pos.x + 1.2 >= pin9b.pos.x >= ball.pos.x - 1.2:
if ball.pos.z + 1.2 >= pin9b.pos.z >= ball.pos.z - 1.2:
ball.velocity = ball.velocity*.999
pin9.velocity = ball.velocity*1.5
pin9.rotate(angle = pi/55, axis = (1,0,0), origin = (pin9t.pos.x,pin9t.pos.y+1,pin9t.pos.z))
#pin10
if ball.pos.x + 1.2 >= pin10b.pos.x >= ball.pos.x - 1.2:
if ball.pos.z + 1.2 >= pin10b.pos.z >= ball.pos.z - 1.2:
ball.velocity = ball.velocity*.999
pin10.velocity = ball.velocity*1.5
pin10.rotate(angle = pi/55, axis = (1,0,0), origin = (pin10t.pos.x,pin10t.pos.y+1,pin10t.pos.z))
pin1.pos = pin1.pos + pin1.velocity*dt
pin2.pos = pin2.pos + pin2.velocity*dt
pin3.pos = pin3.pos + pin3.velocity*dt
pin4.pos = pin4.pos + pin4.velocity*dt
pin5.pos = pin5.pos + pin5.velocity*dt
pin6.pos = pin6.pos + pin6.velocity*dt
pin7.pos = pin7.pos + pin7.velocity*dt
pin8.pos = pin8.pos + pin8.velocity*dt
pin9.pos = pin9.pos + pin9.velocity*dt
pin10.pos = pin10.pos + pin10.velocity*dt

Torquefriction = Ffriction
AngularAcceleration = Torquefriction/(.4*Mball)
AngularVelocity = AngularAcceleration*t
if mag(ball.velocity) <>
ball.rotate(angle=.1, axis=(1,0,0), origin=ball.pos) #rolling without slipping