@Guozhongzhi
2016-11-20T14:46:24.000000Z
字数 1209
阅读 734
计算物理
#Sinai billiardfrom __future__ import division, print_functionfrom visual import *r=0.6rb2=(2.+0.5*r)**2L=8side = 8.0thk = 0.3s2 = 2*side - thks3 = 2*side + thkwallR = box (pos=( L, 0, 0), size=(thk, s2, s3), color = (0.7, 0.7, 0.7))wallL = box (pos=(-L, 0, 0), size=(thk, s2, s3), color = (0.7, 0.7, 0.7))wallB = box (pos=(0, -L, 0), size=(s3, thk, s3), color = (0.7, 0.7, 0.7))wallT = box (pos=(0, L, 0), size=(s3, thk, s3), color = (0.7, 0.7, 0.7))wallBK = box(pos=(0, 0, -0.5), size=(s2, s2, thk), color = color.green)inner=sphere(pos=(0,0,0), radius=2, color=color.cyan) #Fixed obstacleball=sphere(pos=(2,3,0),radius=r,color=color.red,make_trail=True,retain=200) # Moving ballball.trail_object.radius = 0.05v0=vector(1,-1.01,0)ball.v=v0trail=curve()vel=sqrt(ball.v.x**2+ball.v.y**2)dt=0.05for i in arange(15000):rate(1000)ball.pos=ball.pos+ball.v*dt # update positionif mod(i,20)==0:trail.append(pos=ball.pos) #sample position, sample time= 20*dtif not L>ball.x > -L: #bounce off wallsball.v.x=-ball.v.xif not L>ball.y >- L:ball.v.y=-ball.v.yrball2=ball.x**2+ball.y**2phinew=atan2(ball.v.y,ball.v.x)if rball2 < rb2: #bounce off ball in middletheta=atan2(ball.y,ball.x)phi=atan2(ball.v.y,ball.v.x)alpha=phi-thetaphinew=theta-alphaball.v=vector(-vel*cos(phinew),-vel*sin(phinew),0)phinew=atan2(ball.v.y,ball.v.x)