Advertisement
billysback

Gravity

May 7th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.08 KB | None | 0 0
  1. import math, pygame, sys
  2. pygame.init()
  3.  
  4. width, height = 300, 300
  5.  
  6. screen = pygame.display.set_mode((width, height))
  7. screen.fill([0,0,0])
  8.  
  9. def getArg(x, y):
  10.    
  11.     if y == 0:
  12.         arg = 0
  13.         if x < 0:
  14.             arg = pi
  15.         return arg
  16.     elif x == 0:
  17.         arg = pi/2
  18.         if y < 0:
  19.             arg = -(pi/2)
  20.         return arg
  21.     else:
  22.         if y > 0 and x > 0:
  23.             print("#1")
  24.             return math.atan( math.fabs(y) / math.fabs(x) )
  25.         elif y < 0 and x > 0:
  26.             print("#2")
  27.             return pi - math.atan( math.fabs(y) / math.fabs(x) )
  28.         elif y > 0 and x < 0:
  29.             print("#3")
  30.             return -(math.atan( math.fabs(y) / math.fabs(x) ))
  31.         elif y < 0 and x < 0:
  32.             print("#4")
  33.             return -(pi - math.atan( math.fabs(y) / math.fabs(x) ))
  34.         else:
  35.             return 0
  36.  
  37. pi = math.pi
  38. G = (6.67 * math.pow(10, -11) )
  39. class Vector:
  40.     def __init__(self, mod, arg):
  41.         self.x = mod*math.cos(arg)
  42.         self.y = mod*math.sin(arg)
  43.  
  44.         if (self.x < 0 and self.y > 0) or (self.x > 0 and self.y < 0):
  45.             self.x = self.x*-1
  46.             self.y = self.y*-1
  47.  
  48.     def getMod(self):
  49.         return math.sqrt( (self.x*self.x) + (self.y*self.y) )
  50.  
  51.     def getArg(self):
  52.         return getArg(self.x, self.y)
  53.  
  54.     def add(self, vector):
  55.         self.x = self.x + vector.x
  56.         self.y = self.y + vector.y
  57.  
  58.     def addXY(self, x, y):
  59.         self.x = self.x + x
  60.         self.y = self.y + y
  61.  
  62.     def getDist(self, vector):
  63.         return math.sqrt( ( (self.x-vector.x)*(self.x-vector.x) ) + ( (self.y-vector.y)*(self.y-vector.y) ) )
  64.  
  65.     def getDiff(self, vector):
  66.         x, y = (vector.x-self.x), (vector.y-self.y)
  67.         return createVector(x, y)
  68.  
  69. def createVector(x, y):
  70.     return Vector(math.sqrt( (x*x) + (y*y) ), getArg(x, y))
  71.  
  72. counter = 0
  73. counter_limit = 10
  74. interval = 100
  75. class Particle:
  76.     def __init__(self, pos, vel, mass, acc):
  77.         self.pos = pos
  78.         self.vel = vel
  79.         self.mass = mass
  80.         self.acc = acc
  81.  
  82.     def tick(self):
  83.         if counter >= counter_limit:
  84.             self.doGravity(earth)
  85.             self.vel.add(self.acc)
  86.             #self.pos.add(self.vel)
  87.         else:
  88.             self.pos.addXY(self.vel.x/counter_limit, self.vel.y/counter_limit)
  89.  
  90.     def doGravity(self, particle):
  91.         dist = self.pos.getDist(particle.pos)*math.pow(10, 5.5)
  92.         force = ((G*self.mass*particle.mass)/(dist*dist))
  93.        
  94.         acc = (force/self.mass)
  95.         #print(" ")
  96.         #print("acceleration ",acc)
  97.         diffVec = self.pos.getDiff(particle.pos)
  98.         #print("difference: ", diffVec.x,",",diffVec.y)
  99.         #print("position: ", self.pos.x,",",self.pos.y)
  100.         aArg = diffVec.getArg()
  101.         aV = Vector(acc, aArg)
  102.        
  103.         self.acc = aV
  104.  
  105.     def draw(self, target):
  106.         pygame.draw.circle(target, [255, 255, 255], [int(self.pos.x), int(self.pos.y)], 5)
  107.  
  108.  
  109.  
  110. def tickGame():
  111.     moon.tick()
  112.    
  113.     screen.fill([0,0,0])
  114.     moon.draw(screen)
  115.     earth.draw(screen)
  116.  
  117.     pygame.display.flip()
  118.  
  119.  
  120. '''
  121. testx, testy = 3, -6
  122.  
  123. testArg = getArg(testx, testy)
  124. testMod = math.sqrt( (testx*testx) + (testy*testy) )
  125. print(math.degrees(testArg), ",", testMod)
  126.  
  127. testVector = Vector(testMod, testArg)
  128. print("x,y: ", testVector.x, ",", testVector.y)
  129.  
  130. '''
  131. moon = Particle(createVector(150-75, 150), Vector(7, getArg(0, 3)), 7.34*math.pow(10, 22), createVector(0, 0))
  132. earth = Particle(createVector(150, 150), createVector(0, 0), 5.97*math.pow(10, 24), createVector(0, 0))
  133.  
  134. tickGame()
  135.  
  136. while moon.pos.x > 0 and moon.pos.x < width and moon.pos.y > 0 and moon.pos.y < height and (moon.pos.getDist(earth.pos) > 10):
  137.     counter = counter + 1
  138.     tickGame()
  139.     if counter >= counter_limit:
  140.         counter = 0
  141.         #print(" ")
  142.         #print("Moon position: ", moon.pos.x, ", ", moon.pos.y)
  143.         #print("Moon Velocity: ", moon.vel.x, ", ", moon.vel.y)
  144.         #print("Moon Acceleration: ", moon.acc.x, ", ", moon.acc.y)
  145.    
  146.     pygame.time.wait(int(interval/counter_limit))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement