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

No comments:

Post a Comment