Skip to content

Instantly share code, notes, and snippets.

@jackrusher
Last active December 20, 2015 11:19
Show Gist options
  • Save jackrusher/6122522 to your computer and use it in GitHub Desktop.
Save jackrusher/6122522 to your computer and use it in GitHub Desktop.
Leap Motion controlled cube in Quil.
(ns leap-quil-cube
(:require [clojure-leap.core :as leap]
[clojure-leap.hand :as hand]
[clojure-leap.screen :as screen]
[clojure-leap.vector :as v]
[clojure-leap.pointable :as pointable :refer [tip-position]])
(:use quil.core))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Quil cube
(def cube-data [[0 1 1] [1 1 1] [1 0 1] [0 0 1] [1 1 1] [1 1 0] [1 0 0] [1 0 1] [1 1 0] [0 1 0] [0 0 0] [1 0 0] [0 1 0] [0 1 1] [0 0 1] [0 0 0] [0 1 0] [1 1 0] [1 1 1] [0 1 1] [0 0 0] [1 0 0] [1 0 1] [0 0 1]])
;; in pixels
(def sketch-width 1400.0)
(def sketch-height 800.0)
;; these atoms contain the position/rotation info from the Leap
(def x (atom (/ sketch-width 2)))
(def y (atom (/ sketch-height 2)))
(def z (atom 10.0))
(def rot-x (atom 0.0))
(def rot-y (atom 0.0))
(def rot-z (atom 0.0))
(defn setup []
(color-mode :rgb)
(fill 255 120 120)
(no-stroke)
(smooth))
(defn draw []
(background 230)
(lights)
(push-matrix)
(translate @x @y @z)
(rotate-x @rot-x)
(rotate-y @rot-y)
(rotate-z @rot-z)
(scale 50)
(begin-shape :quads)
(doseq [v cube-data]
(apply vertex v))
(end-shape)
(pop-matrix))
;; this will start the sketch
(defsketch leap-experiments
:title "LEAP CUBE"
:setup setup
:draw draw
:renderer :p3d
:size [sketch-width sketch-height])
;; stop the front end
;;(sketch-stop leap-experiments)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; LEAP interface
;; in mm
(def leap-depth 200.0)
(def leap-width 200.0)
(def leap-height 500.0)
(defn to-screen-x [x]
(let [c (/ sketch-width 2)]
(if (> x 0)
(lerp c sketch-width (/ x leap-width))
(lerp c 0.0 (/ (- x) leap-width)))))
(defn to-screen-y [y]
(lerp sketch-height 0.0 (/ y leap-height)))
(defn process-frame [frame screens]
(when-let [hand (and (leap/hands? frame) (first (leap/hands frame)))]
(let [palm (hand/palm hand)]
(reset! x (to-screen-x (.getX (:position palm))))
(reset! y (to-screen-y (.getY (:position palm))))
(reset! z (* 3 (.getZ (:position palm))))
(reset! rot-x (.roll (:direction palm)))
(reset! rot-y (.pitch (:direction palm)))
(reset! rot-z (.yaw (:direction palm))))))
(def listener
(leap/listener :frame #(process-frame (:frame %) (:screens %))
:default #(println "listener:" (:listener %) ", state: " (:state %))))
(def controller
(first (leap/controller listener)))
;; de-activate the Leap event stream
;;(leap/remove-listener! controller listener)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment