(save-name "cradle-0.0.1.scm")

(collisions 1)
(gravity (vector 0 -10 0))
(surface-params 0 0 1 0)

(define (build-pendulum origin)
  (let ((pendulum '()))
    (push)
      (translate (vadd origin (vector 0 2.6 0)))
      (scale (vector 0.1 0.1 0.1))
      (set! pendulum (assoc-set! pendulum "top" (build-sphere 20 20)))
      (active-sphere (assoc-ref pendulum "top"))
      (build-fixedjoint (assoc-ref pendulum "top"))
    (pop)
    (push)
      (translate (vadd origin (vector 0 0 0)))
      (scale (vector 0.1 5 0.1))
      (set! pendulum (assoc-set! pendulum "rod" (build-cube)))
      (active-box (assoc-ref pendulum "rod"))
      (set-mass (assoc-ref pendulum "rod") 1)
    (pop)
    (push)
      (translate (vadd origin (vector 0 -3.5 0)))
      (scale (vector 1 1 1))
      (set! pendulum (assoc-set! pendulum "weight" (build-sphere 16 16)))
      (active-sphere (assoc-ref pendulum "weight"))
      (set-mass (assoc-ref pendulum "weight") 10)
    (pop)
    (build-balljoint
      (assoc-ref pendulum "top")
      (assoc-ref pendulum "rod")
      (vadd origin (vector 0 2.5 0))
    )
    (build-balljoint
      (assoc-ref pendulum "rod")
      (assoc-ref pendulum "weight")
      (vadd origin (vector 0 -2.5 0))
    )
    pendulum
  )
)

(define (build-cradle origin n)
  (if (< n 1)
    '()
    (begin
      (colour (vector (flxrnd) (flxrnd) (flxrnd)))
      (cons
        (build-pendulum (vadd origin (vector (* n 2) 0 0)))
        (build-cradle origin (- n 1))
      )
    )
  )
)

(clear)

(define cradle '())

(push)
  (set! cradle (build-cradle (vector -9 0 0) 8))
(pop)

(kick (assoc-ref (car cradle) "weight") (vector 10 0 0))

