# HG changeset patch # User Robert McIntyre # Date 1327046866 25200 # Node ID 947bef5d6670bdce0e1fe35e970fc5b419bee544 # Parent 247860e255365e93e05253dc42d902381b9ea608 working on eye-following camera diff -r 247860e25536 -r 947bef5d6670 assets/Models/creature1/try-again.blend Binary file assets/Models/creature1/try-again.blend has changed diff -r 247860e25536 -r 947bef5d6670 org/test-creature.org --- a/org/test-creature.org Thu Jan 19 22:26:16 2012 -0700 +++ b/org/test-creature.org Fri Jan 20 01:07:46 2012 -0700 @@ -180,8 +180,6 @@ "Return the two closest two objects to the joint object, ordered from bottom to top according to the joint's rotation." [#^Node parts #^Node joint] - ;;(println (meta-data joint "joint")) - (.getWorldRotation joint) (loop [radius (float 0.01)] (let [results (CollisionResults.)] (.collideWith @@ -347,14 +345,7 @@ joints)) pieces) -(defn blender-creature [blender-path] - (let [model (load-blender-model blender-path) - joints - (if-let [joint-node (.getChild model "joints")] - (seq (.getChildren joint-node)) - (do (println-repl "could not find joints node") - []))] - (assemble-creature model joints))) +(declare blender-creature) (def hand "Models/creature1/one.blend") @@ -650,18 +641,14 @@ (read-string eye-map)))) +(defn creature-eyes + "The eye nodes which are children of the \"eyes\" node in the + creature." + [#^Node creature] + (if-let [eye-node (.getChild creature "eyes")] + (seq (.getChildren eye-node)) + (do (println-repl "could not find eyes node") []))) -(defn enable-vision - - ;; need to create a camera based on uv image, - ;; update this camera every frame based on the position of this - ;; geometry. (maybe can get cam to follow the object) - - ;; use a stack for the continuation to grab the image. - - - [#^Geometry eye] - ;; Here's how vision will work. @@ -698,10 +685,62 @@ ;; eye, and will be moved by it's bound geometry. The dimensions of ;; the eye's camera are equal to the dimensions of the eye's "UV" ;; map. + +(defn eye-target + "The closest object in creature to eye." + [#^Node creature #^Node eye] + (loop [radius (float 0.01)] + (let [results (CollisionResults.)] + (.collideWith + creature + (BoundingBox. (.getWorldTranslation eye) + radius radius radius) + results) + (if-let [target (first results)] + (.getGeometry target) + (recur (float (* 2 radius))))))) + +(defn attach-eyes + "For each eye in the creature, attach a CameraNode to the appropiate + area and return the Camera." + [#^Node creature] + (for [eye (creature-eyes creature)] + (let [target (eye-target creature eye)] + (CameraNode +) + +(defn vision + + ;; need to create a camera based on uv image, + ;; update this camera every frame based on the position of this + ;; geometry. (maybe can get cam to follow the object) + + ;; use a stack for the continuation to grab the image. + + + [#^Geometry eye] + + ) + +(defn blender-creature + "Return a creature with all joints in place." + [blender-path] + (let [model (load-blender-model blender-path) + joints + (if-let [joint-node (.getChild model "joints")] + (seq (.getChildren joint-node)) + (do (println-repl "could not find joints node") []))] + (assemble-creature model joints))) + + + + + + (defn debug-window "creates function that offers a debug view of sensor data" [] @@ -823,9 +862,63 @@ )) + +;; the camera will stay in its initial position/rotation with relation +;; to the spatial. + +(defn bind-camera [#^Spatial obj #^Camera cam] + (let [cam-offset (.subtract (.getLocation cam) + (.getWorldTranslation obj)) + initial-cam-rotation (.getRotation cam) + base-anti-rotation (.inverse (.getWorldRotation obj))] + (.addControl + obj + (proxy [AbstractControl] [] + (controlUpdate [tpf] + (let [total-rotation + (.mult base-anti-rotation (.getWorldRotation obj))] + + (.setLocation cam + (.add + (.mult total-rotation cam-offset) + (.getWorldTranslation obj))) + (.setRotation cam + initial-cam-rotation) + ;;(.mult total-rotation initial-cam-rotation) + + )) + + (controlRender [_ _]))))) - - + + +(defn follow-test [] + (let [camera-pos (Vector3f. 0 30 0) + rock (box 1 1 1 :color ColorRGBA/Blue + :position (Vector3f. 0 10 0) + :mass 30 + ) + + table (box 3 1 10 :color ColorRGBA/Gray :mass 0 + :position (Vector3f. 0 -3 0))] + + (world + (nodify [rock table]) + standard-debug-controls + (fn [world] + (let + [cam (doto (.clone (.getCamera world)) + (.setLocation camera-pos) + (.lookAt Vector3f/ZERO + Vector3f/UNIT_X))] + (bind-camera rock cam) + + (.setTimer world (RatchetTimer. 60)) + (add-eye world cam (comp (view-image) BufferedImage!)) + (add-eye world (.getCamera world) no-op)) + ) + no-op))) + #+end_src #+results: body-1