Sunday, April 25, 2010

Haley's Visual Python - Diver

Project Summery:

For my visual python project, I decided on one extensive project comprised of three smaller components. The project is of a diver, jumping on a spring board about a pivot and then going into projectile motion off a jump and then slowing down in the pool due to the drag force. The diver starts some distance away from the pivot point on the diving board and then oscillates in simple harmonic motion up and down for a wile, till jumping off the board and continuing in projectile motion in the x and y direction. Then the diver enters the pool, where both the drag force and the gravitational force act on it. This project is suppose to simulate the motions and different components of physics a diver goes through on a simple jump.

videos:
(oscillates longer in reality... but could not film and adjust frame at same time)





Code:

from __future__ import division # decimal rule
from visual import *

divingboard = frame()
spring = helix(frame=divingboard, pos=(-2,0,1), axis=(0,2,0), raduis=2.0, length=5, thickness=(1/4))
board = box(frame=divingboard, pos=(1,2,1), axis=(2,0,0), size=(18,0.5,3), color=color.blue)
pivot = cone(frame=divingboard, axis=(0,1.75,0), pos=(-6,0,1), radius=(1), length=(2))

diver = frame()
body = ellipsoid(frame=diver, pos=(0,0,0), axis=(1,0,0),length=.3, height=1.5, width=.75)
head = sphere(frame=diver, pos=(.0,.75,0), radius=.3)
armR = curve(frame=diver, pos=[(0,.18),(.4,0),(.3,-.5)], radius=0.03)
armL = curve(frame=diver, pos=[(0,.18),(-.4,0),(-.3,-.5)], radius=0.03)
legR = curve(frame=diver, pos=[(.07,-.5),(.3,-1),(.25,-1.5),(.5,-1.5)], radius=0.03)
legL = curve(frame=diver, pos=[(-.07,-.5),(-.3,-1),(-.25,-1.5),(-.5,-1.5)], radius=0.03)
diver.pos=(9.5,3.85,1)
diver.axis=(0,0,1)
diver.velocity=vector(0,0,0)

#constants
board.mass = 50
k = 30000.0
B = 100 # apt to change - drag constant

ground = frame()
floorL = box(frame=ground, pos=(-5.5,0,1), size=(10,0.2,30), color=color.orange, material=materials.rough)
floorR = box(frame=ground, pos=(34.5,0,1), size=(10,0.2,30), color=color.orange, material=materials.rough)
floorT = box(frame=ground, pos=(14.5,0,-15), size=(50,0.2,10), color=color.orange, material=materials.rough)
floorB = box(frame=ground, pos=(14.5,0,15), size=(50,0.2,10), color=color.orange, material=materials.rough)
poolT = box(frame=ground, pos=(15,-10,-10), size=(31,20,0.2), color=color.green)
poolB = box(frame=ground, pos=(15,-10,10), size=(31,20,0.2), color=color.green)
poolL = box(frame=ground, pos=(-0.5,-10,0), size=(0.2,20,20), color=color.green)
poolR = box(frame=ground, pos=(30,-10,0), size=(0.2,20,20), color=color.green)
poolfloor = box(frame=ground, pos=(15,-20,0), size=(31,0.2,20), color=color.green)


pool = frame()
water = box(frame=pool, pos=(14.5,-11,0), size=(30,18,20), color=color.cyan, opacity=0.5)


#costants for person
#mass
body.mass = 40
head.mass = 5
armR.mass = 7.5
armL.mass = 7.5
legR.mass = 10
legL.mass = 10
diver.mass = (body.mass+head.mass+armR.mass+armL.mass+legR.mass+legL.mass) #mass of the body total
g = 9.8 # magnitude of gravitational field in m/s^2
r = (diver.pos-pivot.pos) #radius of the moment arm

#forces
F_grav = vector(0,-diver.mass*g,0)*diver.mass # gravitational force



# constants of simulation - note upward and rightward are + (positive) in sign
g = 9.8 # magnitude of gravitational field in m/s^2
m = 1 # mass kilograms
dt = 0.01 # time step in seconds
t = 0 # elapsed time
omega = 0 # initial angular velocity
alpha = 0 # initial angular acceleration
theta = 0.0 # intial angle
# distances
a = 3.0 # pivot to spring
b = 7 # pivot to board cm
c = 16 # pivot to person
rotInertia = 25880 #constant 1/12*ml^2 + Sat-cm board + sat person

t = 0
while (t < 10):
rate(100)
t = t + dt
# calculate torques
torque = -k*a**2*sin(theta)*cos(theta)+(diver.mass*b*g*cos(theta))+(board.mass*c*g*cos(theta))
# calculate angular acceleration
alpha = torque / rotInertia #
omega = omega + alpha*dt
theta = theta + omega*dt
# rotate the system a little bit
divingboard.rotate(angle = omega*dt, axis = (0,0,1), origin = (-6,0,1))
diver.rotate(angle = omega*dt, axis = (0,0,1), origin = (-6,0,1))
spring.length = 1+a*sin(theta)


#projectile motion
t = 0
while diver.pos.y>-2:
t = t + dt
rate(100)
#diver.pos.x = diver.pos.x + diver.velocity*cos(theta)*t
#diver.pos.y = diver.pos.y + diver.velocity*sin(theta)*t+1/2*g*t**2
alpha = torque/rotInertia
acceleration = c*alpha
accelerationX = acceleration*cos(theta)
if t==5:
accelerationY = acceleration*sin(theta)
else:
accelerationY = -9.8
diver.velocity.x = diver.velocity.x + accelerationX*t
diver.velocity.y = diver.velocity.y + accelerationY*t
diver.pos.x = diver.pos.x + diver.velocity.x*dt + 1/2*accelerationX*dt**2
diver.pos.y = diver.pos.y + diver.velocity.y*dt + 1/2*accelerationY*dt**2





t=0
while diver.pos.y<=-2:
t = t + dt
rate(100)
F_grav = diver.mass*g
F_drag = B*diver.velocity.y #sqrt(diver.velocity.y**2+diver.velocity.x**2)
#F_netx = F_drag*cos(theta)
F_nety = F_drag*sin(theta) - F_grav
#accelerationX = F_netx/diver.mass
accelerationY = F_nety/diver.mass
#diver.velocity.x = diver.velocity.x + accelerationX*t
diver.velocity.y = diver.velocity.y + accelerationY*t
#diver.pos.x = diver.pos.x + diver.velocity.x*dt + 1/2*accelerationX*dt**2
diver.pos.y = diver.pos.y + diver.velocity.y*dt + 1/2*accelerationY*dt**2
if diver.pos.y<-17.5:
diver.velocity.y = -1*diver.velocity.y
else:
diver.velocity.y = diver.velocity.y

Monday, April 12, 2010

Description: This is a project on a bird pooping on a statue and the statue attempting to defend itself with a cannon aimed in the air at an angle. The bird flies in at a constant velocity in the x and y directions. At a specified time, different poop particles, each of different mass, are dropped from the bird. Then, with gravity, drag and eventually friction and normal forces acting on them, the poos drop toward the statue (or at least near it). Meanwhile the statue fires its gun at the bird in anticipation of its fly over and attempts to hit it with two bullets of different mass. These encounter the same force as the poo particles.

Code:
from __future__ import division
from visual import*

#bird
bird=frame()
headb= sphere(frame=bird, pos=(0,0,0), radius=1, color=color.blue)
bodyb= sphere(frame=bird, pos=(-3,0,0), radius=2, color=color.blue)
tailb= pyramid(frame=bird, pos=(4.5,0,0), size=(2,.125,2), color=color.blue)
tailb.rotate(angle=3.142, axis=(0,0,-1), origin=(0,0,0))
wing=pyramid(frame=bird, pos=(2,0,0), size=(2.5,.125,12), color=color.blue)
wing.rotate(angle=3.142, axis=(0,0,-1), origin=(0,0,0))
beak= cone(frame=bird, pos=(-1,0,0), axis=(3,0,0), radius=.5, color=color.blue)
bird.pos=(-50,20,0)
bird.velocity=vector(5,0,0)

#statue
stat=frame()
heads= sphere(frame=stat, pos=(0,0,0), radius=.5, color=color.red)
bodys= cylinder(frame=stat, pos=(0,-.5,0), axis=(0,-2,0), radius=1, color=color.red)
arm = cylinder(frame=stat, pos=(-2,-.75,0), axis=(4,0,0), radius=.25, color=color.red)
legL= cylinder(frame=stat, pos=(-.4,-2,0), axis=(0,-2,0), radius=.33, color=color.red)
legR= cylinder(frame=stat, pos=(.4,-2,0), axis=(0,-2,0), radius=.33, color=color.red)
stat.pos=(0,4,0)

#ground
ground=frame()
ground= box(frame=ground, pos=(0,0,0), size=(100,.25,100), color=color.green)

#dropping
dropping1= sphere(pos=(-50,20,0), radius=.25, color=color.white)
dropping1.velocity=vector(5,0,0)
dropping1.trail=curve(color=dropping1.color)

dropping2= sphere(pos=(-50,20,0), radius=.25, color=color.yellow)
dropping2.velocity=vector(5,0,0)
dropping2.trail=curve(color=dropping2.color)

dropping3= sphere(pos=(-50,20,0), radius=.25, color=color.orange)
dropping3.velocity=vector(5,0,0)
dropping3.trail=curve(color=dropping3.color)

#gun
gun=cylinder(frame=stat, pos=(4,-2,0), axis=(-1,-2,0), radius=.66, color=color.orange)

#bullett
bullet4= sphere(pos=(3.5,1,0), radius=.66, color=color.cyan)
bullet4.velocity=vector(4,30,0)
bullet4.trail=curve(color=bullet4.color)

bullet5= sphere(pos=(3.5,1,0), radius=.66, color=color.yellow)
bullet5.velocity=vector(4,20,0)
bullet5.trail=curve(color=bullet5.color)


#equation constants
deltat=.01
t=0
g = 9.8 # magnitude of gravitational field in m/s^2
m1= 1 # mass kilograms
m2=2
m3=3
m4=2
m5=1
dt = 0.01 # time step in seconds
b = 0.4 # drag force constant, where F_drag = -bv^n
n = 1.0 # drag force power, where (again) F_drag = -bv^n
coeff_restitution_g = 0.80# fractional decrease in speed at each collision
coeff_restitution_s = .8
coeff_friction = 0.15 # coefficient of rolling friction between dropping and ground

F_grav1 = vector(0,-m1*g,0) # gravitational force
F_drag1 = -norm(dropping1.velocity) *abs( b * math.pow(dropping1.velocity.mag, n))
F_fric1= vector(0,0,0)
F_net1 = F_grav1 +F_fric1
a1 = F_net1 / m1

F_grav2 = vector(0,-m2*g,0) # gravitational force
F_drag2 = -(norm(dropping2.velocity) * abs(b * math.pow(dropping2.velocity.mag, n)))
F_fric2= vector(0,0,0)
F_net2 = F_grav2 +F_fric2
a2= F_net2 / m2

F_grav3 = vector(0,-m3*g,0) # gravitational force
F_drag3 = - (norm(dropping3.velocity) * abs(b * math.pow(dropping3.velocity.mag, n)))
F_fric3= vector(0,0,0)
F_net3 = F_grav3 +F_fric3
a3= F_net3 / m3

F_grav4 = vector(0,-m4*g,0) # gravitational force
F_drag4 = - (norm(bullet4.velocity) * abs(b * math.pow(bullet4.velocity.mag, n)))
F_fric4= vector(0,0,0)
F_net4= F_grav4 +F_fric4
a4= F_net4 / m4

F_grav5= vector(0,-m5*g,0) # gravitational force
F_drag5= - (norm(bullet5.velocity) * abs(b * math.pow(bullet5.velocity.mag, n)))
F_fric5= vector(0,0,0)
F_net5= F_grav5 +F_fric5
a5= F_net5 / m5

#motion
while t<20:
rate(500)
bird.pos=bird.pos+bird.velocity*deltat
t=t+deltat
dropping1.pos=dropping1.pos+dropping1.velocity*deltat
dropping2.pos=dropping2.pos+dropping2.velocity*deltat
dropping3.pos=dropping3.pos+dropping3.velocity*deltat
if bird.pos.x>-8.748:
dropping2.velocity=dropping2.velocity+a2*deltat
if bird.pos.x>-11:
dropping1.velocity=dropping1.velocity+a1*deltat
if bird.pos.x>-6:
dropping3.velocity=dropping3.velocity+a3*deltat
if -2< dropping1.pos.x <2:
if dropping1.pos.y<=stat.pos.y:
dropping1.velocity.y=-dropping1.velocity.y
if -2< dropping2.pos.x <2:
if dropping2.pos.y< stat.pos.y:
dropping2.velocity.y=-dropping2.velocity.y
if -2 < dropping3.pos.x <2:
if dropping3.pos.y < stat.pos.y:
dropping3.velocity.y=-dropping3.velocity.y
if dropping1.pos.y < ground.pos.y:
dropping1.velocity.y=-dropping1.velocity.y*coeff_restitution_g
if dropping2.pos.y < ground.pos.y:
dropping2.velocity.y=-dropping2.velocity.y * coeff_restitution_g
if dropping3.pos.y < ground.pos.y:
dropping3.velocity.y=-dropping3.velocity.y*coeff_restitution_g
if bird.pos.x > -6:
bullet4.velocity= bullet4.velocity+a4*deltat
bullet4.pos=bullet4.pos+bullet4.velocity*deltat
if bird.pos.x > -8:
bullet5.velocity= bullet5.velocity+a5*deltat
bullet5.pos=bullet5.pos+bullet5.velocity*deltat
if bullet4.pos.y < ground.pos.y:
bullet4.velocity.y=-bullet4.velocity.y*coeff_restitution_g
if bullet5.pos.y < ground.pos.y:
bullet5.velocity.y=-bullet5.velocity.y
dropping1.trail.append(pos=dropping1.pos)
dropping2.trail.append(pos=dropping2.pos)
dropping3.trail.append(pos=dropping3.pos)