planet.py

from wireFrame import *
from math import *

class planet:
    def __init__(self):
        self.wf = wireFrame()
        self.wf.canvas.bind("<ButtonPress-1>",self.press1)
        self.vectors = {}
        self.timerTick = 100
        self.G = 1.0
        self.R = 4
        self.wf.window.after(self.timerTick,self.moveObjects)

    def press1(self,event):
        (x,y,z) = self.wf.cnvMap(event.x,event.y)
        (x,y,z) = self.wf.roll3DbySettingReverse(x,y,z)
        obj = self.wf.createObject(['sphere',x,y,z,self.R])
        self.vectors[obj['id']] = [0,0,0]

    def moveObjects(self):
        for id,obj in self.wf.objects.iteritems():
            itemType = obj['itemType']
            if itemType=='sphere':
                item,itemType,(x,y,z,r) = obj['id'],obj['itemType'],obj['coords']
                for oid,other in self.wf.objects.iteritems():
                    if other['id']==item or other['itemType']!='sphere':
                        continue
                    oid,oType,(ox,oy,oz,oR) = other['id'],other['itemType'],other['coords']
                    xDist,yDist,zDist = x-ox,y-oy,z-oz
                    d = pow(pow(xDist,2)+pow(yDist,2)+pow(zDist,2),1/2.0)
                    g = 1/pow(d,2)*self.G
                    vx,vy,vz = self.vectors[obj['id']]
                    vx,vy,vz = vx+(ox-x)*g,vy+(oy-y)*g,vz+(oz-z)*g
                    self.vectors[obj['id']] = [vx,vy,vz]
                vx,vy,vz = self.vectors[obj['id']]
                x,y,z = x+vx,y+vy,z+vz
                (rx,ry,rz) = self.wf.roll3DbySetting(x,y,z)
                (cx,cy) = self.wf.cnvCanvas(rx,ry,rz)
                self.wf.canvas.coords(item,cx-r,cy-r,cx+r,cy+r)
                obj['coords'] = (x,y,z,r)
        self.wf.window.after(self.timerTick,self.moveObjects)