mixster

mixster

Apr 6th, 2010
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 KB | None | 0 0
  1. #!/usr/bin/python
  2. import sys, pygame
  3. from pygame.locals import *
  4. import math
  5.  
  6. class Point(object):
  7.     def __init__(self, x=0, y=0):
  8.         self.x = x
  9.         self.y = y
  10.  
  11. class Body(object):
  12.     def __init__(self, mass=1, pos=(0, 0), vel=Point(0, 0), radius=0):
  13.         self.pos = pos
  14.         self.radius = radius
  15.         self.acc = Point(0, 0)
  16.         self.vel = vel
  17.         self.mass = mass
  18.  
  19.     def move(self, dx, dy):
  20.         self.pos.x += dx
  21.         self.pos.y += dy
  22.  
  23.     def resetForce(self):
  24.         self.acc = Point(0, 0)
  25.  
  26.     def applyForce(self, force):
  27.         self.acc.x += force.x / self.mass
  28.         self.acc.y += force.y / self.mass
  29.  
  30.     def update(self):
  31.         self.vel.x += self.acc.x# * 10000
  32.         self.vel.y += self.acc.y# * 10000
  33.         self.move(self.vel.x, self.vel.y)
  34.  
  35.     def draw(self, screen, color=(0, 0, 0), width=1):
  36.         pygame.draw.circle(screen, color, (int(self.pos.x), int(self.pos.y)), self.radius, width)
  37.  
  38. def getEvents():
  39.     for event in pygame.event.get():
  40.         if event.type == KEYDOWN:
  41.             if event.key == K_UP:
  42.                 planetC.move(0, -5)
  43.             if event.key == K_DOWN:
  44.                 planetC.move(0, 5)
  45.             if event.key == K_LEFT:
  46.                 planetC.move(-5, 0)
  47.             if event.key == K_RIGHT:
  48.                 planetC.move(5, 0)
  49.             if event.key == ord('q'):
  50.                 return False
  51.     return True
  52.  
  53. def getGravity(bodyA, bodyB):
  54.     dx = bodyA.pos.x - bodyB.pos.x
  55.     dy = bodyA.pos.y - bodyB.pos.y
  56.     d = dx**2 + dy**2
  57.  
  58.     if d == 0:
  59.         return Point(0, 0)
  60.     else:
  61.         f = ((6.673*(10**-11)) * bodyA.mass * bodyB.mass) / d
  62.         a = math.atan2(dy, dx)
  63.         fx = f * -math.cos(a)
  64.         fy = f * -math.sin(a)
  65.  
  66.         #print('dx: ' + str(int(dx)) + '; dy: ' + str(int(dy)))
  67.         #print('fx: ' + str(int(fx)) + '; fy: ' + str(int(fy)))
  68.         #print('f:  ' + str(int(f))  + '; a:  ' + str(int(a * (math.pi/180.0))))
  69.  
  70.         return Point(fx, fy)
  71.  
  72. def invertForce(force):
  73.     return Point(-force.x, -force.y)
  74.  
  75. pygame.init()
  76. screen = pygame.display.set_mode((600, 400))
  77. pygame.display.set_caption('Gravity simulator')
  78. pygame.mouse.set_visible(0)
  79.  
  80. clock = pygame.time.Clock()
  81. planetA = Body(10**8, Point(200, 200), Point(0, 0.065), 25)
  82. planetB = Body(10**8, Point(400, 200), Point(0, -0.065), 25)
  83. planetC = Body(5*10**9, Point(300, 200), Point(0, 0), 10)
  84.  
  85. running = True
  86. while running:
  87.     running = getEvents()
  88.     for i in range(1, 500):
  89.         planetA.resetForce()
  90.         planetB.resetForce()
  91.         planetC.resetForce()
  92.  
  93.         f = getGravity(planetA, planetB)
  94.         planetA.applyForce(f)
  95.         planetB.applyForce(invertForce(f))
  96.  
  97.         f = getGravity(planetC, planetA)
  98.         planetC.applyForce(f)
  99.         planetA.applyForce(invertForce(f))
  100.  
  101.         f = getGravity(planetC, planetB)
  102.         planetC.applyForce(f)
  103.         planetB.applyForce(invertForce(f))
  104.  
  105.         planetA.update()
  106.         planetB.update()
  107.         planetC.update()
  108.    
  109.     pygame.draw.rect(screen, (0, 0, 0), pygame.Rect(0, 0, 600, 400))
  110.     planetA.draw(screen, (255, 0, 0), 5)
  111.     planetB.draw(screen, (0, 255, 0), 5)
  112.     planetC.draw(screen, (0, 0, 255), 3)
  113.  
  114.     clock.tick(15)
  115.     pygame.display.flip()
  116. print('Terminating')
Add Comment
Please, Sign In to add comment