Wednesday, May 25, 2016

Bouncing Ball inside Sphere


from visual import *
from random import uniform

display(center=(0,2,0),background=(1,1,1), autoscale=False, range=4.5,        
        width=600, height=600,  forward=(-.4,-.3,-1)) #arah kamera

distant_light(direction=(1,1,1), color=color.red)

Ball    = sphere(radius=2, length=4, opacity=.3)


bola        = sphere(color=color.green,radius=.2)
bola.y      = 1
bola.x      = -1
bola.z      = 1

v           = vector(2,1,0)
dt          = 1./16
r           = bola.pos
rc          = Ball.radius

def pantul():
    global r,v
    if mag(r)>=rc:
        r   = 1.99*norm(r)
        vp  = (dot(v,norm(r)))*norm(r)
        vr  = v-vp
        v   = vr - vp

def proses():
    global r,v
    a   = vector(0,0,0)
    v   += a*dt
    r   += v*dt

    bola.pos  = r

    pantul()

while 1:
    rate(37)
    proses()


.


Tuesday, May 24, 2016

Bouncing inside Cylinder




from visual import *
from random import uniform

display(center=(0,2,0),background=(1,1,1), autoscale=False, range=4.5,        
        width=600, height=600,  forward=(-.4,-.3,-1)) #arah kamera

distant_light(direction=(1,1,1), color=color.red)

silinder    = cylinder(radius=2, length=4, opacity=.3)
silinder.rotate(angle=pi/2, axis=(0,0,1),origin=(0,0,0))


bola        = sphere(color=color.green,radius=.2)
bola.y      = 0
bola.x      = 0
bola.z      = 1

v           = vector(2,0,0)
dt          = 1./16
r           = bola.pos
rc          = silinder.radius

def pantul():
    global r,v
    if mag(r)>=rc:
        r   = 1.9*norm(r)
        vp  = (dot(v,norm(r)))*norm(r)
        vr  = v-vp
        v   = vr - vp

def proses():
    global r,v
    a   = vector(0,0,0)
    v   += a*dt
    r   += v*dt

    bola.pos  = r

    pantul()

while 1:
    rate(7)
    proses()

.

Dot Product