logo
Published on Models of Motion (http://www2.evergreen.edu/modelsofmotion)

Gravity and Classes

from visual import *
scene.height=700
scene.width=800
earth = sphere(pos=(0,-1,0),color=color.blue,radius=1.5)
moon = sphere(pos=(0,5,0),color=color.red,radius=.5)
lab1 = label(pos=(-5,-5,0))
s=" "
prose = label(pos=(-5,-4,0))
while ord(s[0])!=10:   
    if scene.kb.keys:
        s = scene.kb.getkey()
        print ord(s[0])
        prose.text += s  
initvel=float(prose.text)     
earth.velocity = vector(0,0,0)
moon.velocity = vector(initvel,0,0)               
mmass=7.4e22
emass=6e24
earthtomoonratio = 384400000/mag(earth.pos-moon.pos)
earthtomoondist = earthtomoonratio*mag(earth.pos-moon.pos)
print earthtomoondist
scene.autoscale=0
d=.25
while 1:
     rate(800)
     etomvec = vector(moon.pos-earth.pos)
     mtoevec = vector(earth.pos-moon.pos)
     etomnorm = norm(etomvec)
     mtoenorm = norm(mtoevec)    
     gforce = (6.7e-11 * emass * mmass)/ earthtomoondist**2
     print gforce
     evec = gforce*etomnorm/emass
     mvec = gforce*mtoenorm/mmass
     earth.velocity=earth.velocity + evec * d
     print "earth velocity is",earth.velocity
     lab1.text = str(mag(earth.velocity))
     moon.velocity=moon.velocity + mvec * d
     earth.pos = earth.pos+earth.velocity*d
     moon.pos = moon.pos+moon.velocity*d
     earthtomoondist = earthtomoonratio*mag(earth.pos-moon.pos)

from visual import *

class Planets:
    def __init__(self,v,name,mass,xpos,ypos):
        self.view = v
        self.name = name
        self.mass = mass

    def getName(self):
        return self.name
   

plist = []
for x in range(3):
   
    name=raw_input("enter name")
    mass=input("enter mass")
    xposition = input("enter x position")
    yposition = input("enter y position")
    view = sphere(pos=(xposition,yposition,0),color=color.blue)
    temp = Planets(view,name,mass,xposition,yposition)
    temp.view
    plist.append(temp)

print plist[1].getName()
print plist[0].mass
newxpos = input("enter new xpos for first planet")
plist[0].view.pos.x=newxpos

from visual import *

scene.autoscale=0
scene.height=700
scene.width=800

class Planets:
 def __init__(self,v,name,mass,xpos,ypos,vel):
  self.view = v
  self.name = name
  self.mass = mass
  #set the velocity
  
 def getPosition(self):
  #return position of the object

 #make get name method
  return self.name

 def findVelocity(self,plist,gratio,d,n):
  tvec = vector(0,0,0)
  for x in range(n):
   if self.name!=plist[x].getName():
    nv=norm(plist[x].getPosition()-self.view.pos)
    #find the distance between the 2 planets
    #find the gravity between the 2 planets
    tvec = tvec + (gforce*nv)/self.mass
  self.velocity=self.velocity + tvec * d

 def updatePosition(self,d):
   print d
  #find the new position using the new velocity and time increment

  
 
names = ["earth","moon1","moon2","moon3"]
masses = [6e+24,7.4e+22,7.4e+22,7.4e+22]
colors=[color.blue,color.red,color.yellow,color.magenta]
xpos = [0,0,-6,0]
ypos = [0,6,0,-6]
radius = [1,.4,.4,.3]
ivelocities = 0,0,0],[.1,0,0],[0,.1,0],[-.1,0,0 [0]
earthtomoonratio = 384400000/6   #dist to moon div by dist on screen             
#make the empty list
numPlanets=4
for x in range(numPlanets):
 
 name=names[x]
 #get the mass
 xposition = xpos[x]
 #get y position
 #get velicity
 view = sphere(pos=(xposition,yposition,0),color=colors[x],radius=radius[x])
 #call constructor for Planet
 plist.append(temp)

d=.01


while 1:
 for x in range(numPlanets):
  #call the find velocity method
  plist[x].updatePosition(d)


Source URL:
http://www2.evergreen.edu/modelsofmotion/modelsofmotion/gravity-and-classes