Created
December 10, 2014 22:48
-
-
Save piotrmaslanka/31f8408bd1714521241e to your computer and use it in GitHub Desktop.
A 2D solar system gravity modeler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
from random import random | |
from Swiat import Swiat | |
import pygame | |
import math | |
def grawitacja(x,y,swiat): | |
if x.masa > 499999: | |
print 'Transport sighted: '+str(x.x)+ ' '+str(x.y) | |
if y.masa > 499999: | |
print 'Transport sighted: '+str(y.x)+ ' '+str(y.y) | |
if not x.aktywny: | |
return | |
if not y.aktywny: | |
return | |
kw_odleg = (x.x-y.x)*(x.x-y.x) + (x.y-y.y)*(x.y-y.y) | |
if kw_odleg<4: | |
x.kasowanie() | |
y.kasowanie() | |
print 'X mass '+str(x.masa) | |
print 'Y mass '+str(y.masa) | |
xz = x.x if x.masa>y.masa else y.x | |
yz = x.y if x.masa>y.masa else y.y | |
print 'X source area: '+str(x.x)+' '+str(x.y) | |
print 'Y source area: '+str(y.x)+' '+str(y.y) | |
print 'Target area: '+str(xz)+' '+str(yz) | |
swiat.dodajCialo(xz, yz, 0, 0, x.masa+y.masa, x.promien+y.promien) | |
return 0 | |
fg = 0.0003 * x.masa * y.masa / kw_odleg | |
x.deltaWektor(y.x, y.y, fg/x.masa) | |
y.deltaWektor(x.x, x.y, fg/y.masa) | |
class CialoNiebieskie(object): | |
swiat = Swiat() | |
def __init__(self, x, y, dx, dy, masa, promien): | |
self.x = x | |
self.y = y | |
self.ax = x | |
self.ay = y | |
self.dx = dx | |
self.dy = dy | |
self.masa = masa | |
self.promien = promien | |
self.aktywny = True | |
self.oczekujeKasacje = False | |
self.color = (random()*255, random()*255, random()*255) | |
def deltaWektor(self, tx, ty, sila): | |
dx = tx - self.x | |
dy = ty - self.y | |
vl = math.sqrt(math.pow(dx,2) + math.pow(dy,2)) | |
if vl==0: | |
return | |
self.dx = self.dx + dx/vl*sila | |
self.dy = self.dy + dy/vl*sila | |
def kasowanie(self): | |
self.aktywny = False | |
self.oczekujeKasacje = True | |
def ruch(self): | |
self.x = self.x + self.dx | |
self.y = self.y + self.dy | |
def czyKasowac(self): | |
return self.oczekujeKasacje | |
def renderuj(self): | |
if not self.aktywny: | |
return | |
pygame.draw.line(self.swiat.surface,self.color,(self.ax, self.ay), (self.x,self.y)); | |
pygame.draw.circle(self.swiat.surface,self.color,(self.x,self.y), self.promien) | |
self.ax = self.x | |
self.ay = self.y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
import CialaNiebieskie | |
import Swiat | |
import pygame | |
from pygame.locals import * | |
from random import random | |
from time import sleep | |
import threading | |
class WatekLiczacyFizyke(threading.Thread): | |
swiat = Swiat.Swiat() | |
czekaj = 0 | |
czekaj_licznik = 0 | |
zaakceptowano = False | |
maszSieZatrzymac = False | |
def run(self): | |
while True: | |
self.czekaj_licznik += 1 | |
if self.czekaj >= self.czekaj_licznik: | |
while not self.zaakceptowano: | |
pass | |
self.czekaj_licznik = 0 | |
self.zaakceptowano = False | |
swiat.usunSmiecie() | |
swiat.dlaPary(CialaNiebieskie.grawitacja) | |
swiat.rusz() | |
if self.maszSieZatrzymac: | |
break | |
swiat = Swiat.Swiat() | |
for x in xrange(1,100): | |
swiat.dodajCialo(random()*640,random()*480,random()-0.5,random()-0.5,500,0) | |
swiat.dodajCialo(300,300,0,0,500000,5) | |
w = WatekLiczacyFizyke() | |
w.start() | |
while True: | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
w.maszSieZatrzymac = True | |
exit() | |
if event.type == KEYDOWN: | |
swiat.surface.fill((0,0,0)) | |
swiat.renderuj() | |
pygame.display.flip() | |
w.zaakceptowano = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import CialaNiebieskie | |
class Swiat(object): | |
ciala = [] | |
surface = pygame.display.set_mode((1280,800)) | |
def usunSmiecie(self): | |
a = len(self.ciala) | |
self.ciala = filter(lambda x: x.aktywny, self.ciala) | |
def renderuj(self): | |
for cialo in self.ciala: | |
cialo.renderuj() | |
def rusz(self): | |
for cialo in self.ciala: | |
cialo.ruch() | |
def dlaPary(self, callback): | |
for x in xrange(0,len(self.ciala)-1): | |
for y in xrange(x+1, len(self.ciala)): | |
callback(self.ciala[x], self.ciala[y], self) | |
def dodajCialo(self, x, y, dx, dy, masa, promien): | |
self.ciala.append(CialaNiebieskie.CialoNiebieskie(x,y,dx,dy,masa,promien)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment