I only compute the collision between bed and blue one. They're bola[1] and bola[2] respectively
from visual import *
from random import uniform,random
from visual.controls import *
def change():
global jalan
if b.value:
jalan = True
for ball in(bola):
ball.vx = uniform(-7,7)
ball.vy = uniform(-7,7)
ball.vz = uniform(-7,7)
else:
jalan = False
c = controls(title='Tempat Tombol',x=800, y=0, width=300, height=300, range=50)
b = toggle( pos=(0,0), width=20, height=20, text='Click me', action=lambda: change() )
display(center=(0,0,0),background=(1,1,1), #autoscale=False,
width=600, height=600, forward=(-0.4,-0.3,-1)) #arah kamera
g = -1.
dt = .1
e = 1.
b.value = True
jalan = True
l = 17.
dl = .01
n = 11
distant_light(direction=(1,1,1), color=color.red)
lantai = box(color=color.white, pos=(0,0,0),length=l,height=dl, width=l, opacity=.3)
dindingKiri = box(color=color.white, pos=(-l/2,l/2,0),length=dl,height=l, width=l, opacity=.3)
dindingKanan= box(color=color.white, pos=(l/2,l/2,0),length=dl,height=l, width=l, opacity=.3)
dBelakang = box(color=color.white, pos=(0,l/2,-l/2),length=l,height=l, width=dl, opacity=.3)
atap = box(color=color.white, pos=(0,l,0),length=l,height=dl, width=l, opacity=.3)
bola = []
for i in arange(n):
ball = sphere (pos=(uniform(1,7),0,uniform(-7,7)), radius=.3, color=color.green)
ball.v = vector(uniform(-7,7),uniform(-7,7),uniform(-7,7))
bola.append(ball)
bola[1].radius = 2.5
bola[2].radius = 2.5
bola[1].color = color.red
bola[2].color = color.blue
def proses():
for ball in (bola):
a = g
ball.v[1] += a*dt
ball.pos+= ball.v*dt
tumbukan()
tumbukanBola()
def tumbukan():
for ball in(bola):
if ball.y<0:
ball.y = 0.01
ball.v[1] *=-1.*e
elif ball.y>l:
ball.y = l-.01
ball.v[1] *= -1
if ball.x<-l/2:
ball.x=-l/2+.01
ball.v[0] *= -1*e
if ball.x>l/2:
ball.x=l/2-.01
ball.v[0] *= -1*e
if ball.z<-l/2:
ball.z=-l/2+.01
ball.v[2] *= -1*e
if ball.z>l/2:
ball.z=l/2-.01
ball.v[2] *= -1*e
def tumbukanBola():
#pass
jarak = mag(bola[2].pos-bola[1].pos)
if jarak<(bola[1].radius+bola[2].radius):
arah = norm(bola[2].pos-bola[1].pos)
v1 = dot(bola[1].v,arah)
v2 = dot(bola[2].v,arah)
dv = v2-v1
bola[1].v += dv*arah
bola[2].v -= dv*arah
while 1:
rate (51)
if jalan:
proses()
No comments:
Post a Comment