VPython - example - 模拟球在两板之间的碰撞

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


作者:liuyuan_jq

2011-04-10

######################################### # Import the library(s) ######################################### from visual import * ########################################## # Create Wall(s) ########################################## side=4.0 thk=0.3 wallR = box (pos=vector( side, 0, 0), length=thk, height=2*side, width=2*side, color = color.red) wallL = box (pos=vector(-side, 0, 0), length=thk, height=2*side, width=2*side, color = color.red) ########################################## # 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(2,0,0) ########################################## # Time loop for moving Ball(s) ########################################### timestep = 0.05 while True: # 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

 

VPython - example - 模拟球在两板之间的碰撞_第1张图片

 

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