Advertisement
billysback

gravity

May 7th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 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.     def getMod(self):
  45.         return math.sqrt( (self.x*self.x) + (self.y*self.y) )
  46.  
  47.     def getArg(self):
  48.         return getArg(self.x, self.y)
  49.  
  50.     def add(self, vector):
  51.         self.x = self.x + vector.x
  52.         self.y = self.y + vector.y
  53.  
  54.     def addXY(self, x, y):
  55.         self.x = self.x + x
  56.         self.y = self.y + y
  57.  
  58.     def getDist(self, vector):
  59.         return math.sqrt( ( (self.x-vector.x)*(self.x-vector.x) ) + ( (self.y-vector.y)*(self.y-vector.y) ) )
  60.  
  61.     def getDiff(self, vector):
  62.         x, y = (self.x-vector.x), (self.y-vector.y)
  63.         temp = createVector(1, 3)
  64.         temp.x, temp.y = x, y
  65.         return temp
  66.  
  67. def createVector(x, y):
  68.     return Vector(math.sqrt( (x*x) + (y*y) ), getArg(x, y))
  69.  
  70. counter = 0
  71. counter_limit = 10
  72. interval = 100
  73. class Particle:
  74.     def __init__(self, pos, vel, mass, acc):
  75.         self.pos = pos
  76.         self.vel = vel
  77.         self.mass = mass
  78.         self.acc = acc
  79.  
  80.     def tick(self):
  81.         if counter >= counter_limit:
  82.             self.doGravity(earth)
  83.             self.vel.add(self.acc)
  84.             #self.pos.add(self.vel)
  85.         else:
  86.             self.pos.addXY(self.vel.x/counter_limit, self.vel.y/counter_limit)
  87.  
  88.     def doGravity(self, particle):
  89.         dist = self.pos.getDist(particle.pos)*math.pow(10, 5.5)
  90.         force = ((G*self.mass*particle.mass)/(dist*dist))
  91.        
  92.         acc = (force/self.mass)
  93.         print(" ")
  94.         print("acceleration ",acc)
  95.         diffVec = self.pos.getDiff(particle.pos)
  96.         print("difference: ", diffVec.x,",",diffVec.y)
  97.         print("position: ", self.pos.x,",",self.pos.y)
  98.         aArg = diffVec.getArg()
  99.         aV = Vector(acc, aArg)
  100.        
  101.         self.acc = aV
  102.  
  103.     def draw(self, target):
  104.         pygame.draw.circle(target, [255, 255, 255], [int(self.pos.x), int(self.pos.y)], 5)
  105.  
  106.  
  107. moon = Particle(createVector(150, 150-75), Vector(5, getArg(3, 0)), 7.34*math.pow(10, 22), createVector(0, 0))
  108. earth = Particle(createVector(150, 150), createVector(0, 0), 5.97*math.pow(10, 24), createVector(0, 0))
  109. def tickGame():
  110.     moon.tick()
  111.    
  112.     screen.fill([0,0,0])
  113.     moon.draw(screen)
  114.     earth.draw(screen)
  115.  
  116.     pygame.display.flip()
  117.  
  118.  
  119.    
  120. tickGame()
  121.  
  122. #test = getArg(-3, 6)
  123. #print(math.degrees(test))
  124.  
  125.  
  126. 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):
  127.     counter = counter + 1
  128.     tickGame()
  129.     if counter >= counter_limit:
  130.         counter = 0
  131.         #print(" ")
  132.         #print("Moon position: ", moon.pos.x, ", ", moon.pos.y)
  133.         #print("Moon Velocity: ", moon.vel.x, ", ", moon.vel.y)
  134.         #print("Moon Acceleration: ", moon.acc.x, ", ", moon.acc.y)
  135.    
  136.     pygame.time.wait(int(interval/counter_limit))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement