In [ ]:
import math
from visual import*

G=6.67428e-11
AU=(149.6e6*1000)
SCALE = 250 / AU

scene = display(title='Posevni met',
     x=0, y=0, width=800, height=800,
     center=(0,0,0), background=(0,0,0))
scene.lights = []
local_light(pos=(0,0,0), color=color.white)

class telo():

    ime='telo'
    masa=None
    vx=vy=0.0
    px=py=0.0
    

    def gravitacija(self,other):
        #Napaka 1.
        if self is other:
            raise ValueError("Izracun privlaka telesa %r s samim seboj"%self.ime)
        #Razdalje med drugimi planeti in soncem
        sx, sy = self.px, self.py
        ox, oy = other.px, other.py
        dx = (ox-sx)
        dy = (oy-sy)
        d = math.sqrt(dx**2 + dy**2)
        #Napaka 2.
        if d == 0:
            raise ValueError("Trk med telesoma %r and %r"% (self.ime, other.ime))

        f = G * self.masa * other.masa / (d**2)

        theta = math.atan2(dy, dx)
        fx = math.cos(theta) * f
        fy = math.sin(theta) * f
        return fx, fy

def loop(telesa,ani):
    timestep = 50 # sekund
    step = 1
    ii=0
    while True:
        step += 1
        force = {}
        for telo in telesa:
            total_fx = total_fy = 0.0
            for other in telesa:
                if telo is other:
                    continue
                fx, fy = telo.gravitacija(other)
                total_fx += fx
                total_fy += fy
            force[telo] = (total_fx, total_fy)

        # Posodobi hitrosti.
        for telo in telesa:
            fx, fy = force[telo]
            telo.vx += fx / telo.masa * timestep
            telo.vy += fy / telo.masa * timestep

            telo.px += telo.vx * timestep
            telo.py += telo.vy * timestep
            if step%3600==0:
                ani[telo.ime].pos=(telo.px/AU,telo.py/AU,0.0)



ani={}
telesa=['sonce','merkur','venera','zemlja','mars','jupiter','astro']
for i in telesa:
    ani[i]=0.0

sonce=telo()
sonce.ime='sonce'
sonce.masa=1.99*10**30
ani['sonce']=sphere(pos=(sonce.px/AU,sonce.py/AU,0),radius=0.1,material=materials.emissive,color=color.yellow)

merkur=telo()
merkur.ime='merkur'
merkur.masa = 0.3301 * 10**24
merkur.px = 0.387 * AU
merkur.vy = -47.36 * 1000
ani['merkur']=sphere(pos=(merkur.px/AU,merkur.py/AU,0),radius=0.01,make_trail=True, trail_type="points",interval=1, retain=500)

venera = telo()
venera.ime = 'venera'
venera.masa = 4.8685 * 10**24
venera.px = 0.723 * AU
venera.vy = -35.02 * 1000
ani['venera']=sphere(pos=(venera.px/AU,venera.py/AU,0),radius=0.02,color=color.orange,make_trail=True, trail_type="points",interval=1, retain=500)
ani['venera'].trail_object.color=color.orange

zemlja = telo()
zemlja.ime = 'zemlja'
zemlja.masa = 5.9742 * 10**29
zemlja.px = -1*AU
zemlja.vy = 29.783 * 1000
ani['zemlja']=sphere(pos=(zemlja.px/AU,zemlja.py/AU,0),radius=0.02,material=materials.earth,make_trail=True, trail_type="points",interval=1, retain=500)
ani['zemlja'].trail_object.color=color.blue

mars = telo()
mars.ime = 'mars'
mars.masa = 0.64174 * 10**24
mars.px = -1.52366231*AU
mars.vy = 26.50 * 1000
ani['mars']=sphere(pos=(mars.px/AU,mars.py/AU,0),radius=0.01,color=color.red,make_trail=True, trail_type="points",interval=1, retain=500)
ani['mars'].trail_object.color=color.red

jupiter = telo()
jupiter.ime = 'jupiter'
jupiter.masa = 1898.3*10**24
jupiter.px = 5.20336*AU
jupiter.vy = -13.72* 1000
ani['jupiter']=sphere(pos=(jupiter.px/AU,jupiter.py/AU,0),radius=0.1,materials=materials.marble,make_trail=True,interval=1, retain=500)

astro = telo()
astro.ime = 'astro'
astro.masa = 0.1*10**3
astro.px = 2.20336*AU
astro.vx = -1.72* 1000
astro.vy = -10.72* 1000
ani['astro']=sphere(pos=(astro.px/AU,astro.py/AU,0),radius=0.01,color=color.green,make_trail=True,interval=1, retain=500)
                    

loop([sonce,merkur,venera,zemlja,mars,jupiter,astro],ani)
In [ ]: