from KineticsKit import *
import visual
import sys

print """Try to click at a Mass to grip it, click again to release it.
Zooming / rotating of the scene is done by holding the left / right
mouse button down while moving the mouse around."""

rate = 30
count = 11 # number of masses

system = System(timestep=1./rate, gravity=2.0, viscosity=0.1)

# generate some masses and insert them into the system
dx = 2.0 / count
for x in range(count):
    system.insertMass(Mass(m=(count-x+1)*0.2, pos=(x, 0.7, 0.0)))

#system.insertMass(Mass(m=0.3, pos=(1.4, 0.8, 0.1)))

# an objects property can also be changed via the reference inserted into the system
system.masses[0].fixed = 1
# system.masses[-1].fixed = 1
print system.masses[0].sphere.pos
system.masses[0].sphere.pos = (0.0,0.0,0.0)
print system.masses[0].sphere.pos

# connect the masses with springs
for i in range(len(system.masses)-1):
    system.insertSpring(CylinderSpring(m0=system.masses[i], m1=system.masses[i+1], k=10., damping=0.0))

system.display.forward = (-0.5, -0.4, 0.2)

#start the mainloop (convenience function)
#system.mainloop()

system.display.autoscale=0
for tick in range(300):
    #system.step()
    if tick % 500 == 0:
        print tick
        system.masses[0].sphere.pos = (0.0,0.0,0.0)
    if tick % 500 == 100:
        system.masses[0].sphere.pos = (0.5,0.5,0.5)
    if tick % 500 == 200:
        system.masses[0].sphere.pos = (0.0,0.0,-0.5)
    if tick % 500 == 400:
        system.masses[0].sphere.pos = (0.5,-0.5,0.0)
    system.advance()
sys.exit()
