Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import math
- import pygame
- G = 6.67384 * pow(10,-11)
- random.seed()
- '''Variables that need to be kept around long-term begin with upper case letters
- Variables which are used quickly begin with lower case letters'''
- def StarCluster(ObjectCount):
- global G
- StarMassLow = 0.2 * pow(10, 30)
- StarMassHigh = 2 * pow(10, 30)
- ZoomLevel = 4.73 * pow(10, 15)
- '''0.5 LY per pixel'''
- Timestep = 10000 * 3.16 * pow(10, 7)
- '''10000 Year per timestep'''
- Spacing = 16 * 9.46 * pow(10, 15)
- '''Number of square LY per star'''
- GenDisplacement = int(pow(ObjectCount * pow(Spacing,2), 0.5))
- '''Calculate max magnitude of X and Y'''
- MinDistance = 4.73 * pow(10, 15)
- '''Being closer than this distance won't increase acceleration of object'''
- ObjectMass = []
- ObjectX = []
- ObjectY = []
- ObjectVX = []
- ObjectVY = []
- for iteration in range (0, ObjectCount):
- ObjectMass.append(random.randint(StarMassLow, StarMassHigh))
- ObjectX.append(random.randint(0, GenDisplacement))
- ObjectY.append(random.randint(0, GenDisplacement))
- ObjectVX.append(0)
- ObjectVY.append(0)
- print('Created Object %s.' % (iteration + 1))
- screen = pygame.display.set_mode((800,800))
- while True:
- for a in range (0, ObjectCount):
- '''a is the object that will have acceleration changed'''
- for b in range (0, ObjectCount):
- '''b is the object that will change it'''
- if a != b:
- distance = pow(pow(ObjectX[a]-ObjectX[b],2)+pow(ObjectY[a]-ObjectY[b],2), 0.5)
- if distance < MinDistance:
- distance = MinDistance
- dv = - ((G * ObjectMass[b]) / pow(distance, 2)) * Timestep
- if ObjectX[b] - ObjectX[a] >= 0:
- angle = math.atan((ObjectY[b] - ObjectY[a])/(ObjectX[b] - ObjectX[a]))
- if ObjectX[b] - ObjectX[a] < 0:
- angle = math.atan((ObjectY[b] - ObjectY[a])/(ObjectX[b] - ObjectX[a])) + math.pi
- ObjectVX[a] += dv * math.cos(angle)
- ObjectVY[a] += dv * math.sin(angle)
- '''if a == 0 and b == 1:
- 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'''
- for a in range (0, ObjectCount):
- ObjectX[a] += ObjectVX[a]
- ObjectY[a] += ObjectVY[a]
- '''if a == 0:
- 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'''
- for a in range (0, ObjectCount):
- screen.set_at((int(ObjectX[a] / ZoomLevel), int(ObjectY[a] / ZoomLevel)), (255,255,255))
- pygame.event.get()
- print("Object %s at %s , %s" % (a+1, int(ObjectX[a] / ZoomLevel), int(ObjectY[a] / ZoomLevel)))
- print('Welcome to gravity simulator')
- print('Please select which mode you wish to use')
- print('1: Star Cluster Mode')
- print('2: Our Solar System Mode')
- print('3: Accretion Disk Mode')
- mode = int(input('Select Mode:'))
- if mode == 1:
- print('Star Cluster Mode selected')
- ObjectCount = int(input('Select number of stars:'))
- StarCluster(ObjectCount)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement