from random import random, randint
from visual import box, display, sphere, color, vector, rate, cylinder

limits = {'x': (0, 10),
          'y': (-2, 2),
	  'z': (-10, 10)}

display(title='Graph of position', width=600, height=200, center=(5,0,0), range=(10,10,10))

ball = sphere (pos=(randint(limits['x'][0], limits['x'][1]),
	            randint(limits['y'][0], limits['y'][1]),
                    randint(limits['z'][0], limits['z'][1])), radius=0.5, color=color.yellow)

box (pos=(5, limits['y'][0], 0), length=9, height=0.2, width=20, color=color.green)
box (pos=(5, limits['y'][1], 0), length=9, height=0.2, width=20, color=color.green)
rac1 = box (pos=(limits['x'][0], 0, 0), length=0.5, height=2, width=2, color=color.blue)
rac2 = box (pos=(limits['x'][1], 0, 0), length=0.5, height=2, width=2, color=color.blue)
shadow = (cylinder(pos=(ball.pos.x, limits['y'][0], ball.pos.z), axis=(0, 0.1, 0), radius=0.5, color=color.black),
	  cylinder(pos=(ball.pos.x, limits['y'][1], ball.pos.z), axis=(0, -0.1, 0), radius=0.5, color=color.black))

dt = (random()+0.001)*0.01
ball.velocity = vector((random()+0.1)*10-5, (random()+0.1)*10-5, (random()+0.1)*10-5)

while 1:
	rate(100)
	ball.pos = ball.pos + ball.velocity*dt
	for a in ('x', 'y', 'z') :
		if getattr(ball, a) < limits[a][0]+rac1.length or getattr(ball, a) > limits[a][1]-rac2.length:
			setattr(ball.velocity, a, -getattr(ball.velocity, a))
	for r in (rac1, rac2) :
		for a in ('y', 'z'):
			setattr(r, a, getattr(ball, a))
	for s in shadow :
		for a in ('x', 'z'):
			setattr(s, a, getattr(ball, a))

