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)

No comments:

Post a Comment