from visual import * ##establishes what value the x,y, and z will have positionNum = 20 ##size of each side is twice the position size = 2*positionNum rad = positionNum/20 ##velocity should be kept at <(1/20)*position because any higher and numerous problems may occur with bounces ##weird bounces will still occur occasionally regardless of velocity xVelocity = .5 yVelocity = .2 zVelocity = .3 ##creates the walls using the positions and size xzBottomWall = box (pos=(0,-positionNum,0), length=size, height=0.01, width=size, color=color.blue) xzTopWall = box (pos=(0,positionNum,0), length=size, height=0.01, width=size, color=color.blue) yzRightWall = box (pos=(positionNum,0,0), length=.01, height=size, width=size, color=color.red) yzLeftWall = box (pos=(-positionNum,0,0), length=.01, height=size, width=size, color=color.red) ##creates front wall as invisible wall so user can see inside xyFrontWall = box (pos=(0,0,positionNum), length=size, height=size, width=0.01, color=color.white, opacity=0) xyBackWall = box (pos=(0,0,-positionNum), length=size, height=size, width=0.01, color=color.white, opacity=0.2) ##creates ball and establishes velocity ball = sphere (pos=(0,0,0), radius=rad, color=color.green) ball.velocity = vector(xVelocity,yVelocity,zVelocity) while True: ##halts computations briefly rate(100) ##changes ball position by adding velocity to position ##this is why a big velocity causes problems, makes position beyond borders ball.pos = ball.pos + ball.velocity ##conditionals check to see if the ball's x,y or z position is beyond the wall ##if one of them is, it flips that component of the velocity if ball.y - ball.radius < -positionNum: ball.velocity.y = -ball.velocity.y if ball.y + ball.radius> positionNum: ball.velocity.y = -ball.velocity.y if ball.x - ball.radius< -positionNum: ball.velocity.x = -ball.velocity.x if ball.x + ball.radius> positionNum: ball.velocity.x = -ball.velocity.x if ball.z - ball.radius< -positionNum: ball.velocity.z = -ball.velocity.z if ball.z + ball.radius> positionNum: ball.velocity.z = -ball.velocity.z