Skip Navigation | ANU Home | Search ANU
The Australian
National University
Computational Science Education Outreach and Training (EOT)
Printer Friendly Version of this Document

#########################################
# 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