diff org/test-creature.org @ 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
line wrap: on
line diff
     1.1 --- a/org/test-creature.org	Thu Jan 19 22:26:16 2012 -0700
     1.2 +++ b/org/test-creature.org	Fri Jan 20 01:07:46 2012 -0700
     1.3 @@ -180,8 +180,6 @@
     1.4    "Return the two closest two objects to the joint object, ordered
     1.5    from bottom to top according to the joint's rotation."
     1.6    [#^Node parts #^Node joint]
     1.7 -  ;;(println (meta-data joint "joint"))
     1.8 -  (.getWorldRotation joint)
     1.9    (loop [radius (float 0.01)]
    1.10      (let [results (CollisionResults.)]
    1.11        (.collideWith
    1.12 @@ -347,14 +345,7 @@
    1.13      joints))
    1.14    pieces)
    1.15  
    1.16 -(defn blender-creature [blender-path]
    1.17 -  (let [model (load-blender-model blender-path)
    1.18 -        joints
    1.19 -        (if-let [joint-node (.getChild model "joints")]
    1.20 -          (seq (.getChildren joint-node))
    1.21 -          (do (println-repl "could not find joints node")
    1.22 -              []))]
    1.23 -  (assemble-creature model joints)))
    1.24 +(declare blender-creature)
    1.25  
    1.26  (def hand "Models/creature1/one.blend")
    1.27  
    1.28 @@ -650,18 +641,14 @@
    1.29        (read-string
    1.30         eye-map))))
    1.31  
    1.32 +(defn creature-eyes
    1.33 +  "The eye nodes which are children of the \"eyes\" node in the
    1.34 +  creature."
    1.35 +  [#^Node creature]
    1.36 +  (if-let [eye-node (.getChild creature "eyes")]
    1.37 +    (seq (.getChildren eye-node))
    1.38 +    (do (println-repl "could not find eyes node") [])))
    1.39  
    1.40 -(defn enable-vision
    1.41 -
    1.42 -  ;; need to create a camera based on uv image,
    1.43 -  ;; update this camera every frame based on the position of this
    1.44 -  ;; geometry. (maybe can get cam to follow the object)
    1.45 -
    1.46 -  ;; use a stack for the continuation to grab the image.
    1.47 -  
    1.48 -
    1.49 -  [#^Geometry eye]
    1.50 -  
    1.51  
    1.52    ;; Here's how vision will work.
    1.53  
    1.54 @@ -698,10 +685,62 @@
    1.55    ;; eye, and will be moved by it's bound geometry. The dimensions of
    1.56    ;; the eye's camera are equal to the dimensions of the eye's "UV"
    1.57    ;; map. 
    1.58 +
    1.59 +(defn eye-target
    1.60 +  "The closest object in creature to eye."
    1.61 +  [#^Node creature #^Node eye]
    1.62 +  (loop [radius (float 0.01)]
    1.63 +    (let [results (CollisionResults.)]
    1.64 +      (.collideWith
    1.65 +       creature
    1.66 +       (BoundingBox. (.getWorldTranslation eye)
    1.67 +                     radius radius radius)
    1.68 +       results)
    1.69 +      (if-let [target (first results)]
    1.70 +        (.getGeometry target)
    1.71 +        (recur (float (* 2 radius)))))))
    1.72 +
    1.73 +(defn attach-eyes
    1.74 +  "For each eye in the creature, attach a CameraNode to the appropiate
    1.75 +   area and return the Camera."
    1.76 +  [#^Node creature]
    1.77 +  (for [eye (creature-eyes creature)]
    1.78 +    (let [target (eye-target creature eye)]
    1.79 +      (CameraNode
    1.80 +)
    1.81 +  
    1.82 +(defn vision
    1.83 +
    1.84 +  ;; need to create a camera based on uv image,
    1.85 +  ;; update this camera every frame based on the position of this
    1.86 +  ;; geometry. (maybe can get cam to follow the object)
    1.87 +
    1.88 +  ;; use a stack for the continuation to grab the image.
    1.89 +  
    1.90 +
    1.91 +  [#^Geometry eye]
    1.92 +  
    1.93 +
    1.94    
    1.95  
    1.96  )
    1.97  
    1.98 +
    1.99 +(defn blender-creature
   1.100 +  "Return a creature with all joints in place."
   1.101 +  [blender-path]
   1.102 +  (let [model (load-blender-model blender-path)
   1.103 +        joints
   1.104 +        (if-let [joint-node (.getChild model "joints")]
   1.105 +          (seq (.getChildren joint-node))
   1.106 +          (do (println-repl "could not find joints node") []))]
   1.107 +  (assemble-creature model joints)))
   1.108 +
   1.109 +
   1.110 +
   1.111 +
   1.112 +
   1.113 +
   1.114  (defn debug-window
   1.115    "creates function that offers a debug view of sensor data"
   1.116    []
   1.117 @@ -823,9 +862,63 @@
   1.118      
   1.119  ))
   1.120     
   1.121 +
   1.122 +;; the camera will stay in its initial position/rotation with relation
   1.123 +;; to the spatial.
   1.124 +
   1.125 +(defn bind-camera [#^Spatial obj #^Camera cam]
   1.126 +  (let [cam-offset (.subtract (.getLocation cam)
   1.127 +                              (.getWorldTranslation obj))
   1.128 +        initial-cam-rotation (.getRotation cam)
   1.129 +        base-anti-rotation (.inverse (.getWorldRotation obj))]
   1.130 +    (.addControl
   1.131 +     obj
   1.132 +     (proxy [AbstractControl] []
   1.133 +       (controlUpdate [tpf]
   1.134 +         (let [total-rotation
   1.135 +               (.mult base-anti-rotation (.getWorldRotation obj))]
   1.136 +                          
   1.137 +         (.setLocation cam
   1.138 +                       (.add
   1.139 +                        (.mult total-rotation cam-offset)
   1.140 +                        (.getWorldTranslation obj)))
   1.141 +         (.setRotation cam
   1.142 +                       initial-cam-rotation)
   1.143 +                       ;;(.mult total-rotation initial-cam-rotation)
   1.144 +                       
   1.145 +         ))
   1.146 +
   1.147 +       (controlRender [_ _])))))
   1.148      
   1.149 -                         
   1.150 -  
   1.151 +
   1.152 +
   1.153 +(defn follow-test []
   1.154 +  (let [camera-pos (Vector3f. 0 30 0)
   1.155 +        rock (box 1 1 1 :color ColorRGBA/Blue
   1.156 +                     :position (Vector3f. 0 10 0)
   1.157 +                     :mass 30
   1.158 +                     )
   1.159 +        
   1.160 +        table (box 3 1 10 :color ColorRGBA/Gray :mass 0
   1.161 +                   :position (Vector3f. 0 -3 0))]
   1.162 +
   1.163 +    (world
   1.164 +     (nodify [rock table])
   1.165 +             standard-debug-controls
   1.166 +             (fn [world]
   1.167 +               (let 
   1.168 +                   [cam (doto (.clone (.getCamera world))
   1.169 +                          (.setLocation camera-pos)
   1.170 +                          (.lookAt Vector3f/ZERO
   1.171 +                                   Vector3f/UNIT_X))]
   1.172 +                 (bind-camera rock cam)
   1.173 +                 
   1.174 +                 (.setTimer world (RatchetTimer. 60))
   1.175 +                 (add-eye world cam (comp (view-image) BufferedImage!))
   1.176 +                 (add-eye world (.getCamera world) no-op))
   1.177 +               )
   1.178 +             no-op)))
   1.179 +       
   1.180  #+end_src
   1.181  
   1.182  #+results: body-1