VPython - example - 模拟球在封闭箱子内的碰撞

本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 Unported许可协议进行许可。允许非商业转载,但应注明作者及出处。


作者:liuyuan_jq

2011-04-10

From http://maths.anu.edu.au/comptlsci/Tutorial-Gas/bounce2.html

 

 

######################################### # Import the library(s) ######################################### from visual import * ########################################## # Create Wall(s) ########################################## side=4.0 thk=0.3 s2 = 2*side - thk s3 = 2*side + thk wallR = box (pos=( side, 0, 0), length=thk, height=s2, width=s3, color = color.red) wallL = box (pos=(-side, 0, 0), length=thk, height=s2, width=s3, color = color.red) wallB = box (pos=(0, -side, 0), length=s3, height=thk, width=s3, color = color.blue) wallT = box (pos=(0, side, 0), length=s3, height=thk, width=s3, color = color.blue) wallBK = box(pos=(0, 0, -side), length=s2, height=s2, width=thk, color = (0.7,0.7,0.7)) ########################################## # Create Ball(s) ########################################## ball_radius=1.0 maxpos=side-thk/2-ball_radius maxv=2.0 ball = sphere (color = color.green, radius = ball_radius) ball.velocity = vector (-1.5, -2.3, +2.7) ########################################## # Time loop for moving Ball(s) ########################################### timestep = 0.05 while (1==1): # Set number of times loop is repeated per second rate(100) # Move ball(s) ball.pos = ball.pos + ball.velocity*timestep #check for collisions with the walls #right wall if ball.pos.x > maxpos: ball.velocity.x = -ball.velocity.x #reflect velocity ball.pos.x=2*maxpos-ball.pos.x #reflect position #left wall if ball.pos.x < -maxpos: ball.velocity.x = -ball.velocity.x ball.pos.x=-2*maxpos-ball.pos.x # roof if ball.pos.y > maxpos: ball.velocity.y = -ball.velocity.y ball.pos.y=2*maxpos-ball.pos.y #floor if ball.pos.y < -maxpos: ball.velocity.y = -ball.velocity.y ball.pos.y=-2*maxpos-ball.pos.y #back wall if ball.pos.z > maxpos: ball.velocity.z = -ball.velocity.z ball.pos.z=2*maxpos-ball.pos.z #front wall if ball.pos.z < -maxpos: ball.velocity.z = -ball.velocity.z ball.pos.z=-2*maxpos-ball.pos.z

 

VPython - example - 模拟球在封闭箱子内的碰撞_第1张图片

 

你可能感兴趣的:(vector,velocity,import,library)