from visual import *

print """
Right button drag to rotate "camera" to view scene.
Middle button to drag up or down to zoom in or out.
  On a two-button mouse, middle is left + right.
  On a one-button mouse, middle is CTRL + mouse.
"""

# build the box
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 the frame/object
f = frame()
cylinder(frame=f, pos=(0,-1.2,0), axis=(0,1,0), radius=0.5, length=2.2, color=color.cyan)
sphere(frame=f, pos=(0.5,1,0), radius=0.7, color=color.red)
cone(frame=f, pos=(0.5,1,0.28), radius=0.4, color=color.yellow)
cone(frame=f, pos=(0.5,1,-0.28), radius=0.4, color=color.yellow)
box(frame=f, pos=(-0.5,1,0), height=0.2, length=2, width=1, color=color.blue)
box(frame=f, pos=(-0.5,-1,0), height=0.2, length=2, width=1, color=color.blue)
f.axis = (0,1,0)
f.pos = (0,0,0)
f.mass = 1
f.p = vector (-0.15, -0.23, +0.27)

# set up distance from the outer sides of the box that the object should bounce from
side = side - thk*0.5 - 1.6

dt = 0.5
t=0.0

# set the initial speed rate for the loop
loopRate= 100

while 1: #loop inifinately
  rate(loopRate) #ensure that the speed of the loop will be the same across all pc's
  t = t + dt
  #draw object in its new posion
  f.pos = f.pos + f.p*(dt/f.mass)
  #give the object some rotation
  f.rotate(angle=0.0628, axis=( -0.16, -0.21, +0.25))
  #check for a key press
  if scene.kb.keys:
      key = scene.kb.getkey()
      if key == 'f': #if f has been pressed
          print "speeding up"
          loopRate = loopRate + 10
      elif key == 's': #if s has been pressed
          if loopRate > 19:
              loopRate = loopRate - 10
              print "slowing down"
  #check if object is hitting any sides and if so change its direction otherwise continue to it in its given path
  if not (side > f.x > -side):
    f.p.x = -f.p.x
  if not (side > f.y > -side):
    f.p.y = -f.p.y
  if not (side > f.z > -side):
    f.p.z = -f.p.z   



