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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值