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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值