Thursday, April 14, 2016

Playing with Central Force in Pyton with Visual Module


 I don't use n body calculation, every sphere only attracted by force from origin, dependent upon its position.

from visual import *
from random import uniform,random

l       = 17.
dl      = .01
display(center=(0,0,0),background=(1,1,1),autoscale=False, width=600, height=600,forward=(-0.4,-0.3,-1))
distant_light(direction=(1,1,1), color=color.red)
box(color=color.white, pos=(0,0,0),length=l,height=dl, width=l, opacity=0.3)
box(color=color.white, pos=(0,0,0),length=dl,height=l, width=l, opacity=0.3)
box(color=color.white, pos=(0,0,0),length=l,height=l, width=dl, opacity=0.3)

bola    = [] 
n       =   11

G       = 31.
for i in range(n):
    ball            = sphere (pos=(uniform(1,7),0,uniform(-7,7)), radius=.3, color=(random(),random(),random()))
    ball.v          = vector(0,uniform(1,3),0)
    
    bola.append(ball)


dt = 1./32.
bola[0].pos=(1,1,1)
while 1:
    rate (100)
    for i in arange(n):
        r           = bola[i].pos
        v           = bola[i].v
        jarak       = mag(bola[i].pos)
        arah        = -norm(bola[i].pos)
        gaya        = G*1./(pow(jarak,2)+.1)*arah
        a           = gaya
        v           += a*dt
        r           += v*dt

        bola[i].pos = r
        

.