Advertisement
Guest User

Gaze upon it and weep

a guest
Jan 31st, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.39 KB | None | 0 0
  1. import random
  2. import math
  3. import pygame
  4. G = 6.67384 * pow(10,-11)
  5. random.seed()
  6. '''Variables that need to be kept around long-term begin with upper case letters
  7. Variables which are used quickly begin with lower case letters'''
  8.  
  9. def StarCluster(ObjectCount):
  10.     global G
  11.     StarMassLow = 0.2 * pow(10, 30)
  12.     StarMassHigh = 2 * pow(10, 30)
  13.     ZoomLevel = 4.73 * pow(10, 15)
  14.     '''0.5 LY per pixel'''
  15.     Timestep = 10000 * 3.16 * pow(10, 7)
  16.     '''10000 Year per timestep'''
  17.     Spacing = 16 * 9.46 * pow(10, 15)
  18.     '''Number of square LY per star'''
  19.     GenDisplacement = int(pow(ObjectCount * pow(Spacing,2), 0.5))
  20.     '''Calculate max magnitude of X and Y'''
  21.     MinDistance = 4.73 * pow(10, 15)
  22.     '''Being closer than this distance won't increase acceleration of object'''
  23.     ObjectMass = []
  24.     ObjectX = []
  25.     ObjectY = []
  26.     ObjectVX = []
  27.     ObjectVY = []
  28.     for iteration in range (0, ObjectCount):
  29.         ObjectMass.append(random.randint(StarMassLow, StarMassHigh))
  30.         ObjectX.append(random.randint(0, GenDisplacement))
  31.         ObjectY.append(random.randint(0, GenDisplacement))
  32.         ObjectVX.append(0)
  33.         ObjectVY.append(0)
  34.         print('Created Object %s.' % (iteration + 1))
  35.     screen = pygame.display.set_mode((800,800))
  36.     while True:
  37.         for a in range (0, ObjectCount):
  38.             '''a is the object that will have acceleration changed'''
  39.             for b in range (0, ObjectCount):
  40.                 '''b is the object that will change it'''
  41.                 if a != b:
  42.                     distance = pow(pow(ObjectX[a]-ObjectX[b],2)+pow(ObjectY[a]-ObjectY[b],2), 0.5)
  43.                     if distance < MinDistance:
  44.                         distance = MinDistance
  45.                     dv = - ((G * ObjectMass[b]) / pow(distance, 2)) * Timestep
  46.                     if ObjectX[b] - ObjectX[a] >= 0:
  47.                         angle = math.atan((ObjectY[b] - ObjectY[a])/(ObjectX[b] - ObjectX[a]))
  48.                     if ObjectX[b] - ObjectX[a] < 0:
  49.                         angle = math.atan((ObjectY[b] - ObjectY[a])/(ObjectX[b] - ObjectX[a])) + math.pi
  50.                     ObjectVX[a] += dv * math.cos(angle)
  51.                     ObjectVY[a] += dv * math.sin(angle)
  52.                     '''if a == 0 and b == 1:
  53.                        print('Calculation object %s with object %s done, dv was %s and angle was %s' % (a+1, b+1, round(dv,8), round(math.degrees(angle),0))) #debugging line'''
  54.         for a in range (0, ObjectCount):
  55.             ObjectX[a] += ObjectVX[a]
  56.             ObjectY[a] += ObjectVY[a]
  57.             '''if a == 0:
  58.                print('Object %s at position %s , %s with velocity %s , %s' % (a+1, round(ObjectX[a],1), round(ObjectY[a],1), round(ObjectVX[a],2), round(ObjectVY[a],2))) #debugging line'''
  59.         for a in range (0, ObjectCount):
  60.             screen.set_at((int(ObjectX[a] / ZoomLevel), int(ObjectY[a] / ZoomLevel)), (255,255,255))
  61.             pygame.event.get()
  62.             print("Object %s at %s , %s" % (a+1, int(ObjectX[a] / ZoomLevel), int(ObjectY[a] / ZoomLevel)))
  63. print('Welcome to gravity simulator')
  64. print('Please select which mode you wish to use')
  65. print('1: Star Cluster Mode')
  66. print('2: Our Solar System Mode')
  67. print('3: Accretion Disk Mode')
  68. mode = int(input('Select Mode:'))
  69. if mode == 1:
  70.     print('Star Cluster Mode selected')
  71.     ObjectCount = int(input('Select number of stars:'))
  72.     StarCluster(ObjectCount)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement