In this lesson we show how to model a moving marble in vPython. We show how to create an animation where the marble bounces off the left and righty walls of our virtual room. For your convenience, we include the code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | from vpython import * roomX=12 roomY=10 roomZ=16 wallT=.5 wallColor=vector(1,1,1) wallOpacity=.8 frontOpacity=.1 marbleR=.5 ballColor=vector(0,0,1) myFloor=box(size=vector(roomX,wallT,roomZ),pos=vector(0,-roomY/2,0),color=wallColor,opacity=wallOpacity) myCeiling=box(size=vector(roomX,wallT,roomZ),pos=vector(0,roomY/2,0),color=wallColor,opacity=wallOpacity) leftWall=box(size=vector(wallT,roomY,roomZ),pos=vector(-roomX/2,0,0),color=wallColor,opacity=wallOpacity) rightWall=box(size=vector(wallT,roomY,roomZ),pos=vector(roomX/2,0,0),color=wallColor,opacity=wallOpacity) backWall=box(size=vector(roomX,roomY,wallT),pos=vector(0,0,-roomZ/2),color=wallColor,opacity=wallOpacity) frontWall=box(size=vector(roomX,roomY,wallT),pos=vector(0,0,roomZ/2),color=wallColor,opacity=frontOpacity) marble=sphere(color=ballColor,radius=marbleR) marbleX=0 deltaX=.1 while True: rate(20) marbleX=marbleX+deltaX if marbleX+marbleR>(roomX/2-wallT/2) or marbleX-marbleR<(-roomX/2+wallT/2): deltaX=deltaX*(-1) marbleX=marbleX+deltaX marble.pos=vector(marbleX,0,0) |