changeset 116:947bef5d6670

working on eye-following camera
author Robert McIntyre <rlm@mit.edu>
date Fri, 20 Jan 2012 01:07:46 -0700
parents 247860e25536
children 94c005f7f9dd
files assets/Models/creature1/try-again.blend org/test-creature.org
diffstat 2 files changed, 116 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
     1.1 Binary file assets/Models/creature1/try-again.blend has changed
     2.1 --- a/org/test-creature.org	Thu Jan 19 22:26:16 2012 -0700
     2.2 +++ b/org/test-creature.org	Fri Jan 20 01:07:46 2012 -0700
     2.3 @@ -180,8 +180,6 @@
     2.4    "Return the two closest two objects to the joint object, ordered
     2.5    from bottom to top according to the joint's rotation."
     2.6    [#^Node parts #^Node joint]
     2.7 -  ;;(println (meta-data joint "joint"))
     2.8 -  (.getWorldRotation joint)
     2.9    (loop [radius (float 0.01)]
    2.10      (let [results (CollisionResults.)]
    2.11        (.collideWith
    2.12 @@ -347,14 +345,7 @@
    2.13      joints))
    2.14    pieces)
    2.15  
    2.16 -(defn blender-creature [blender-path]
    2.17 -  (let [model (load-blender-model blender-path)
    2.18 -        joints
    2.19 -        (if-let [joint-node (.getChild model "joints")]
    2.20 -          (seq (.getChildren joint-node))
    2.21 -          (do (println-repl "could not find joints node")
    2.22 -              []))]
    2.23 -  (assemble-creature model joints)))
    2.24 +(declare blender-creature)
    2.25  
    2.26  (def hand "Models/creature1/one.blend")
    2.27  
    2.28 @@ -650,18 +641,14 @@
    2.29        (read-string
    2.30         eye-map))))
    2.31  
    2.32 +(defn creature-eyes
    2.33 +  "The eye nodes which are children of the \"eyes\" node in the
    2.34 +  creature."
    2.35 +  [#^Node creature]
    2.36 +  (if-let [eye-node (.getChild creature "eyes")]
    2.37 +    (seq (.getChildren eye-node))
    2.38 +    (do (println-repl "could not find eyes node") [])))
    2.39  
    2.40 -(defn enable-vision
    2.41 -
    2.42 -  ;; need to create a camera based on uv image,
    2.43 -  ;; update this camera every frame based on the position of this
    2.44 -  ;; geometry. (maybe can get cam to follow the object)
    2.45 -
    2.46 -  ;; use a stack for the continuation to grab the image.
    2.47 -  
    2.48 -
    2.49 -  [#^Geometry eye]
    2.50 -  
    2.51  
    2.52    ;; Here's how vision will work.
    2.53  
    2.54 @@ -698,10 +685,62 @@
    2.55    ;; eye, and will be moved by it's bound geometry. The dimensions of
    2.56    ;; the eye's camera are equal to the dimensions of the eye's "UV"
    2.57    ;; map. 
    2.58 +
    2.59 +(defn eye-target
    2.60 +  "The closest object in creature to eye."
    2.61 +  [#^Node creature #^Node eye]
    2.62 +  (loop [radius (float 0.01)]
    2.63 +    (let [results (CollisionResults.)]
    2.64 +      (.collideWith
    2.65 +       creature
    2.66 +       (BoundingBox. (.getWorldTranslation eye)
    2.67 +                     radius radius radius)
    2.68 +       results)
    2.69 +      (if-let [target (first results)]
    2.70 +        (.getGeometry target)
    2.71 +        (recur (float (* 2 radius)))))))
    2.72 +
    2.73 +(defn attach-eyes
    2.74 +  "For each eye in the creature, attach a CameraNode to the appropiate
    2.75 +   area and return the Camera."
    2.76 +  [#^Node creature]
    2.77 +  (for [eye (creature-eyes creature)]
    2.78 +    (let [target (eye-target creature eye)]
    2.79 +      (CameraNode
    2.80 +)
    2.81 +  
    2.82 +(defn vision
    2.83 +
    2.84 +  ;; need to create a camera based on uv image,
    2.85 +  ;; update this camera every frame based on the position of this
    2.86 +  ;; geometry. (maybe can get cam to follow the object)
    2.87 +
    2.88 +  ;; use a stack for the continuation to grab the image.
    2.89 +  
    2.90 +
    2.91 +  [#^Geometry eye]
    2.92 +  
    2.93 +
    2.94    
    2.95  
    2.96  )
    2.97  
    2.98 +
    2.99 +(defn blender-creature
   2.100 +  "Return a creature with all joints in place."
   2.101 +  [blender-path]
   2.102 +  (let [model (load-blender-model blender-path)
   2.103 +        joints
   2.104 +        (if-let [joint-node (.getChild model "joints")]
   2.105 +          (seq (.getChildren joint-node))
   2.106 +          (do (println-repl "could not find joints node") []))]
   2.107 +  (assemble-creature model joints)))
   2.108 +
   2.109 +
   2.110 +
   2.111 +
   2.112 +
   2.113 +
   2.114  (defn debug-window
   2.115    "creates function that offers a debug view of sensor data"
   2.116    []
   2.117 @@ -823,9 +862,63 @@
   2.118      
   2.119  ))
   2.120     
   2.121 +
   2.122 +;; the camera will stay in its initial position/rotation with relation
   2.123 +;; to the spatial.
   2.124 +
   2.125 +(defn bind-camera [#^Spatial obj #^Camera cam]
   2.126 +  (let [cam-offset (.subtract (.getLocation cam)
   2.127 +                              (.getWorldTranslation obj))
   2.128 +        initial-cam-rotation (.getRotation cam)
   2.129 +        base-anti-rotation (.inverse (.getWorldRotation obj))]
   2.130 +    (.addControl
   2.131 +     obj
   2.132 +     (proxy [AbstractControl] []
   2.133 +       (controlUpdate [tpf]
   2.134 +         (let [total-rotation
   2.135 +               (.mult base-anti-rotation (.getWorldRotation obj))]
   2.136 +                          
   2.137 +         (.setLocation cam
   2.138 +                       (.add
   2.139 +                        (.mult total-rotation cam-offset)
   2.140 +                        (.getWorldTranslation obj)))
   2.141 +         (.setRotation cam
   2.142 +                       initial-cam-rotation)
   2.143 +                       ;;(.mult total-rotation initial-cam-rotation)
   2.144 +                       
   2.145 +         ))
   2.146 +
   2.147 +       (controlRender [_ _])))))
   2.148      
   2.149 -                         
   2.150 -  
   2.151 +
   2.152 +
   2.153 +(defn follow-test []
   2.154 +  (let [camera-pos (Vector3f. 0 30 0)
   2.155 +        rock (box 1 1 1 :color ColorRGBA/Blue
   2.156 +                     :position (Vector3f. 0 10 0)
   2.157 +                     :mass 30
   2.158 +                     )
   2.159 +        
   2.160 +        table (box 3 1 10 :color ColorRGBA/Gray :mass 0
   2.161 +                   :position (Vector3f. 0 -3 0))]
   2.162 +
   2.163 +    (world
   2.164 +     (nodify [rock table])
   2.165 +             standard-debug-controls
   2.166 +             (fn [world]
   2.167 +               (let 
   2.168 +                   [cam (doto (.clone (.getCamera world))
   2.169 +                          (.setLocation camera-pos)
   2.170 +                          (.lookAt Vector3f/ZERO
   2.171 +                                   Vector3f/UNIT_X))]
   2.172 +                 (bind-camera rock cam)
   2.173 +                 
   2.174 +                 (.setTimer world (RatchetTimer. 60))
   2.175 +                 (add-eye world cam (comp (view-image) BufferedImage!))
   2.176 +                 (add-eye world (.getCamera world) no-op))
   2.177 +               )
   2.178 +             no-op)))
   2.179 +       
   2.180  #+end_src
   2.181  
   2.182  #+results: body-1