Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 4665
Kernel: Python 2 (Ubuntu Linux)

The following example comes from the VPython documentation at http://vpython.org/contents/docs/VPython_Intro.pdf. It demonstrates how to create a box with walls that a single ball can bounce around inside of.

Take a look at the code and see if you can figure out what each part does.

from vpython import * L = 5.0 thickness=0.2 wall_Top = box (pos=vector(0, L, 0), size=vector(2*L, thickness, 2*L), color = color.blue) wall_Bottom = box (pos=vector(0, -L, 0), size=vector(2*L, thickness, 2*L), color = color.blue) wall_Left = box (pos=vector(-L, 0, 0), size=vector(thickness, 2*L, 2*L), color = color.red) wall_Right = box (pos=vector( L, 0, 0), size=vector(thickness, 2*L, 2*L), color = color.red) wall_Back = box(pos=vector(0, 0, -L), size=vector(2*L, 2*L, thickness), color = color.red) ball = sphere (color = color.green, radius = 0.5, make_trail=True, retain=50, trail_color=color.green) ball.mass = 1.0 ball.p = vector(0.2, .3, .4) L=L-ball.radius dt = 0.5 t = 0.0 tf = 300.0 while t<tf: rate(40) ball.pos = ball.pos + ball.p/ball.mass*dt ### Detect wall collisions if ball.pos.x>L or ball.pos.x<-L: ball.p.x = -ball.p.x if ball.pos.y>L or ball.pos.y<-L: ball.p.y = -ball.p.y if ball.pos.z>L or ball.pos.z<-L: ball.p.z = -ball.p.z t = t + dt #Update time