changeset 61:7b44348af538

cleaning up body code
author Robert McIntyre <rlm@mit.edu>
date Mon, 28 Nov 2011 21:22:35 -0700
parents e5e627f50a3a
children 2b9d81017cb7
files org/body.org
diffstat 1 files changed, 356 insertions(+), 380 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/org/body.org	Mon Nov 28 02:54:48 2011 -0700
     1.2 +++ b/org/body.org	Mon Nov 28 21:22:35 2011 -0700
     1.3 @@ -16,24 +16,6 @@
     1.4  (cortex.import/mega-import-jme3)
     1.5  (rlm.rlm-commands/help)
     1.6  
     1.7 -;;(.loadModel 
     1.8 -;; (doto (asset-manager)
     1.9 -;;   (.registerLoader BlenderModelLoader (into-array String ["blend"])))
    1.10 -;;   "Models/person/person.blend")
    1.11 -
    1.12 -(defn view-model [^String model]
    1.13 -  (view 
    1.14 -   (.loadModel 
    1.15 -    (doto (asset-manager)
    1.16 -      (.registerLoader BlenderModelLoader (into-array String ["blend"])))
    1.17 -    model)))
    1.18 -
    1.19 -(defn load-blender-scene [^String model]
    1.20 -  (.loadModel
    1.21 -   (doto (asset-manager)
    1.22 -     (.registerLoader BlenderLoader (into-array String ["blend"])))
    1.23 -    model))
    1.24 -
    1.25  (defn load-blender-model 
    1.26    [^String model]
    1.27    (.loadModel
    1.28 @@ -41,23 +23,6 @@
    1.29       (.registerLoader BlenderModelLoader (into-array String ["blend"])))
    1.30     model))
    1.31  
    1.32 -(defn worm
    1.33 -  []
    1.34 -  (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
    1.35 -
    1.36 -(defn oto
    1.37 -  []
    1.38 -  (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
    1.39 -
    1.40 -(defn sinbad
    1.41 -  []
    1.42 -  (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
    1.43 -
    1.44 -(defn worm-blender
    1.45 -  []
    1.46 -  (first (seq (.getChildren (load-blender-model
    1.47 -                             "Models/anim2/simple-worm.blend")))))
    1.48 -
    1.49  (defn skel [node]
    1.50    (doto
    1.51        (.getSkeleton
    1.52 @@ -105,26 +70,6 @@
    1.53      (println "...geo...")))
    1.54  
    1.55  
    1.56 -
    1.57 -
    1.58 -(defn body
    1.59 -  "given a node with a SkeletonControl, will produce a body sutiable
    1.60 -  for AI control with movement and proprioception."
    1.61 -  [node]
    1.62 -  (let [skeleton-control (.getControl node SkeletonControl)
    1.63 -        krc (KinematicRagdollControl.)]
    1.64 -    (comment
    1.65 -    (dorun
    1.66 -     (map #(.addBoneName krc %)
    1.67 -          ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
    1.68 -           ;;"mid2" "mid3" "tail" "head"]
    1.69 -        )))
    1.70 -    (.addControl node krc)
    1.71 -    (.setRagdollMode krc)
    1.72 -    )
    1.73 -  node
    1.74 -  )
    1.75 -
    1.76  (defn green-x-ray []
    1.77    (doto (Material. (asset-manager)
    1.78                     "Common/MatDefs/Misc/Unshaded.j3md")
    1.79 @@ -132,241 +77,11 @@
    1.80      (-> (.getAdditionalRenderState)
    1.81          (.setDepthTest false))))
    1.82  
    1.83 -(defn show-skeleton [node]
    1.84 -  (let [sd
    1.85  
    1.86 -        (doto
    1.87 -            (SkeletonDebugger. "aurellem-skel-debug"
    1.88 -                               (skel node))
    1.89 -          (.setMaterial (green-x-ray)))]
    1.90 -    (.attachChild node sd)
    1.91 -    node))
    1.92 -
    1.93 -  
    1.94 -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    1.95 -
    1.96 -;; this could be a good way to give objects special properties like
    1.97 -;; being eyes and the like
    1.98 -
    1.99 -(.getUserData 
   1.100 - (.getChild
   1.101 -  (load-blender-model "Models/property/test.blend") 0)
   1.102 - "properties")
   1.103 -
   1.104 -;; the properties are saved along with the blender file.
   1.105 -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   1.106 -
   1.107 -
   1.108 -
   1.109 -(defn init-debug-skel-node
   1.110 -  [f debug-node skeleton]
   1.111 -  (let [bones 
   1.112 -        (map #(.getBone skeleton %)
   1.113 -             (range (.getBoneCount skeleton)))]
   1.114 -  (dorun (map #(.setUserControl % true) bones)) 
   1.115 -  (dorun (map (fn [b]
   1.116 -                (println (.getName b)
   1.117 -                         " -- " (f b)))
   1.118 -              bones))
   1.119 -  (dorun
   1.120 -   (map #(.attachChild
   1.121 -          debug-node 
   1.122 -          (doto 
   1.123 -              (sphere 0.1
   1.124 -                      :position (f %)
   1.125 -                      :physical? false)
   1.126 -          (.setMaterial (green-x-ray))))
   1.127 -        bones)))
   1.128 -    debug-node)
   1.129 -
   1.130 -(import jme3test.bullet.PhysicsTestHelper)
   1.131 -
   1.132 -
   1.133 -(defn test-zzz [the-worm world value]
   1.134 -  (if (not value)
   1.135 -    (let [skeleton (skel the-worm)]
   1.136 -      (println-repl "enabling bones")
   1.137 -      (dorun
   1.138 -       (map
   1.139 -        #(.setUserControl (.getBone skeleton %) true)
   1.140 -        (range (.getBoneCount skeleton))))
   1.141 -
   1.142 -
   1.143 -      (let [b (.getBone skeleton 2)]
   1.144 -        (println-repl "moving " (.getName b))
   1.145 -        (println-repl (.getLocalPosition b))
   1.146 -        (.setUserTransforms b
   1.147 -                            Vector3f/UNIT_X
   1.148 -                            Quaternion/IDENTITY
   1.149 -                            ;;(doto (Quaternion.)
   1.150 -                             ;; (.fromAngles (/ Math/PI 2)
   1.151 -                             ;;              0
   1.152 -                             ;;              0
   1.153 -                                          
   1.154 -                            (Vector3f. 1 1 1))
   1.155 -        )
   1.156 -      
   1.157 -      (println-repl "hi! <3"))))
   1.158 -
   1.159 -
   1.160 -(defn test-ragdoll []
   1.161 -  
   1.162 -  (let [the-worm
   1.163 -        
   1.164 -         ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
   1.165 -         (doto (show-skeleton (worm-blender))
   1.166 -           (.setLocalTranslation (Vector3f. 0 10 0))
   1.167 -         ;;(worm)
   1.168 -         ;;(oto)
   1.169 -         ;;(sinbad)
   1.170 -        )
   1.171 -    ]
   1.172 -
   1.173 -
   1.174 -    (.start 
   1.175 -     (world
   1.176 -      (doto (Node.)
   1.177 -        (.attachChild the-worm))
   1.178 -      {"key-return" (fire-cannon-ball)
   1.179 -       "key-space" (partial test-zzz the-worm)
   1.180 -       }
   1.181 -      (fn [world]
   1.182 -        (light-up-everything world)
   1.183 -        (PhysicsTestHelper/createPhysicsTestWorld
   1.184 -         (.getRootNode world)
   1.185 -         (asset-manager)
   1.186 -         (.getPhysicsSpace
   1.187 -          (.getState (.getStateManager world) BulletAppState)))
   1.188 -        (set-gravity world Vector3f/ZERO)
   1.189 -        ;;(.setTimer world (NanoTimer.))
   1.190 -        ;;(org.lwjgl.input.Mouse/setGrabbed false)
   1.191 -        )
   1.192 -      no-op
   1.193 -      )
   1.194 -  
   1.195 -     
   1.196 -     )))
   1.197 -    
   1.198 -       
   1.199 -
   1.200 -
   1.201 -
   1.202 -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   1.203 -;;; here is the ragdoll stuff
   1.204 -
   1.205 -(def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
   1.206 -(def mesh worm-mesh)
   1.207 -
   1.208 -(.getFloatBuffer mesh VertexBuffer$Type/Position)
   1.209 -(.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
   1.210 -(.getData  (.getBuffer mesh VertexBuffer$Type/BoneIndex))
   1.211 -
   1.212 -
   1.213 -(defn position [index]
   1.214 -  (.get
   1.215 -   (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
   1.216 -     index))
   1.217 -
   1.218 -(defn bones [index]
   1.219 -  (.get
   1.220 -   (.getData  (.getBuffer mesh VertexBuffer$Type/BoneIndex))
   1.221 -   index))
   1.222 -
   1.223 -(defn bone-weights [index]
   1.224 -  (.get
   1.225 -   (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
   1.226 -   index))
   1.227 -
   1.228 -
   1.229 -
   1.230 -(defn vertex-bones [vertex]
   1.231 -  (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
   1.232 -
   1.233 -(defn vertex-weights [vertex]
   1.234 -  (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
   1.235 -
   1.236 -(defn vertex-position [index]
   1.237 -  (let [offset (* index 3)]
   1.238 -    (Vector3f. (position offset)
   1.239 -               (position (inc offset))
   1.240 -               (position (inc(inc offset))))))
   1.241 -
   1.242 -(def vertex-info (juxt vertex-position vertex-bones vertex-weights))
   1.243 -
   1.244 -(defn bone-control-color [index]
   1.245 -  (get {[1 0 0 0] ColorRGBA/Red
   1.246 -        [1 2 0 0] ColorRGBA/Magenta
   1.247 -        [2 0 0 0] ColorRGBA/Blue}
   1.248 -       (vertex-bones index)
   1.249 -       ColorRGBA/White))
   1.250 -
   1.251 -(defn influence-color [index bone-num]
   1.252 -  (get
   1.253 -   {(float 0)   ColorRGBA/Blue
   1.254 -    (float 0.5) ColorRGBA/Green
   1.255 -    (float 1)   ColorRGBA/Red}
   1.256 -     ;; find the weight of the desired bone
   1.257 -   ((zipmap (vertex-bones index)(vertex-weights index))
   1.258 -    bone-num)
   1.259 -   ColorRGBA/Blue))
   1.260 -         
   1.261 -(def worm-vertices (set (map vertex-info (range 60))))
   1.262 -
   1.263 -
   1.264 -(defn test-info []
   1.265 -  (let [points (Node.)]
   1.266 -    (dorun
   1.267 -     (map #(.attachChild points %)
   1.268 -          (map #(sphere 0.01
   1.269 -                        :position (vertex-position %)
   1.270 -                        :color (influence-color % 1)
   1.271 -                        :physical? false)
   1.272 -               (range 60))))
   1.273 -    (view points)))
   1.274 -  
   1.275  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   1.276  
   1.277  ;;;;;;;;;;;;  eve-style bodies  ;;;;;;;;
   1.278  
   1.279 -
   1.280 -(defrecord JointControl [joint physics-space]
   1.281 -  PhysicsControl
   1.282 -  (setPhysicsSpace [this space]
   1.283 -    (dosync
   1.284 -     (ref-set (:physics-space this) space))
   1.285 -    (.addJoint space (:joint this)))
   1.286 -  (update [this tpf])
   1.287 -  (setSpatial [this spatial])
   1.288 -  (render [this rm vp])
   1.289 -  (getPhysicsSpace [this] (deref (:physics-space this)))
   1.290 -  (isEnabled [this] true)
   1.291 -  (setEnabled [this state]))
   1.292 -  
   1.293 -(defn add-joint
   1.294 -  "Add a joint to a particular object. When the object is added to the
   1.295 -  PhysicsSpace of a simulation, the joint will also be added"
   1.296 -  [object joint]
   1.297 -  (let [control (JointControl. joint (ref nil))]
   1.298 -    (.addControl object control))
   1.299 -  object)
   1.300 -
   1.301 -(defn hinge-world
   1.302 -  []
   1.303 -  (let [sphere1 (sphere)
   1.304 -        sphere2 (sphere 1 :position (Vector3f. 3 3 3))
   1.305 -        joint (Point2PointJoint.
   1.306 -               (.getControl sphere1 RigidBodyControl)
   1.307 -               (.getControl sphere2 RigidBodyControl) 
   1.308 -               Vector3f/ZERO (Vector3f. 3 3 3))]
   1.309 -    (add-joint sphere1 joint)
   1.310 -    (doto (Node. "hinge-world")
   1.311 -      (.attachChild sphere1)
   1.312 -      (.attachChild sphere2))))
   1.313 -
   1.314 -(defn test-joint []
   1.315 -  (view (hinge-world)))
   1.316 -
   1.317 -
   1.318  (defn worm [segment-length num-segments interstitial-space radius]
   1.319    (letfn [(nth-segment
   1.320              [n]
   1.321 @@ -387,7 +102,6 @@
   1.322         node))
   1.323    ([children] (nodify "" children)))
   1.324  
   1.325 -
   1.326  (defn connect-at-midpoint
   1.327    [segmentA segmentB]
   1.328    (let [centerA (.getWorldTranslation segmentA)
   1.329 @@ -395,23 +109,19 @@
   1.330          midpoint (.mult (.add centerA centerB) (float 0.5))
   1.331          pivotA (.subtract midpoint centerA)
   1.332          pivotB (.subtract midpoint centerB)
   1.333 -
   1.334          joint (Point2PointJoint.
   1.335                 (.getControl segmentA RigidBodyControl)
   1.336                 (.getControl segmentB RigidBodyControl)
   1.337                 pivotA
   1.338                 pivotB)]
   1.339 -    (add-joint segmentA joint)
   1.340    segmentB))
   1.341  
   1.342 -
   1.343  (defn point-worm []
   1.344    (let [segments (worm 0.2 5 0.1 0.1)]
   1.345      (dorun (map (partial apply connect-at-midpoint)
   1.346                  (partition 2 1 segments)))
   1.347      (nodify "worm"  segments)))
   1.348  
   1.349 -
   1.350  (defn test-worm []
   1.351    (.start
   1.352     (world
   1.353 @@ -445,7 +155,7 @@
   1.354  ;; correspond to some sort of jet or rocket propulseion)
   1.355  
   1.356  (defn node-seq
   1.357 -  "take a node and return a seq of all its children
   1.358 +  "Take a node and return a seq of all its children
   1.359     recursively. There will be no nodes left in the resulting
   1.360     structure"
   1.361    [#^Node node]
   1.362 @@ -478,8 +188,6 @@
   1.363          body-control  (partial map #(%1 %2) limb-controls)]
   1.364      body-control))
   1.365  
   1.366 -
   1.367 -
   1.368  (defn test-motor-map
   1.369    "see how torque works."
   1.370    []
   1.371 @@ -498,8 +206,6 @@
   1.372       (fn [_ _]
   1.373         (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))
   1.374  
   1.375 -
   1.376 -
   1.377  (defn test-torque
   1.378    "see how torque works."
   1.379    []
   1.380 @@ -527,7 +233,6 @@
   1.381         (if @move-right?
   1.382           (.applyTorque control (Vector3f. 0 0 1)))))))
   1.383  
   1.384 -
   1.385  (defn worm-pattern [time]
   1.386    [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   1.387  
   1.388 @@ -541,37 +246,8 @@
   1.389  
   1.390     ])
   1.391  
   1.392 -
   1.393 -;; (defn copier-gen []
   1.394 -;;   (let [count (atom 0)]
   1.395 -;;     (fn [in]
   1.396 -;;       (swap! count inc)
   1.397 -;;       (clojure.contrib.duck-streams/copy
   1.398 -;;        in (File. (str "/home/r/tmp/mao-test/clojure-images/"
   1.399 -;;                      ;;/home/r/tmp/mao-test/clojure-images
   1.400 -;;                (format "%08d.png" @count)))))))
   1.401 -;; (defn decrease-framerate []
   1.402 -;;   (map
   1.403 -;;    (copier-gen)
   1.404 -;;    (sort
   1.405 -;;     (map first
   1.406 -;;          (partition
   1.407 -;;           4
   1.408 -;;           (filter #(re-matches #".*.png$" (.getCanonicalPath %))
   1.409 -;;                   (file-seq
   1.410 -;;                    (file-str
   1.411 -;;                     "/home/r/media/anime/mao-temp/images"))))))))
   1.412 -
   1.413 -
   1.414 -
   1.415 -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   1.416 -
   1.417 -
   1.418 -
   1.419 -
   1.420  ;;;;;;;;;;;;;;;;;;  Proprioception  ;;;;;;;;;;;;;;;;;;;;;;;;
   1.421  
   1.422 -
   1.423  ;; this is not used as just getting the rotation would be simpler.
   1.424  (defn proprioception-senses 
   1.425    "given a control , create a sequence of thunks that will report the
   1.426 @@ -608,8 +284,6 @@
   1.427          rotated (.mult q basis-1)
   1.428          alpha (.dot basis-1 (.project rotated basis-1))
   1.429          beta  (.dot basis-2 (.project rotated basis-2))]
   1.430 -    (println-repl alpha)
   1.431 -    (println-repl beta)
   1.432      (Math/atan2 beta alpha)))
   1.433  
   1.434  
   1.435 @@ -638,10 +312,6 @@
   1.436            (.subtract 
   1.437             (.localToWorld object-b (.getPivotB joint) nil)
   1.438             (.getWorldTranslation object-b))))
   1.439 -        rotate-b
   1.440 -        (doto (Matrix3f.)
   1.441 -          (.fromStartEndVectors arm-b Vector3f/UNIT_X))
   1.442 -        
   1.443          pitch
   1.444          (.angleBetween
   1.445           (.normalize (Vector2f. (.getX arm-b) (.getY arm-b)))
   1.446 @@ -652,10 +322,12 @@
   1.447           (Vector2f. 1 0))
   1.448                       
   1.449          roll
   1.450 -        (.mult 
   1.451 -         (.getLocalRotation object-b)
   1.452 -         (doto (Quaternion.)
   1.453 -           (.fromRotationMatrix rotate-a)))
   1.454 +        (rot-about-axis
   1.455 +         (.mult 
   1.456 +          (.getLocalRotation object-b)
   1.457 +          (doto (Quaternion.)
   1.458 +            (.fromRotationMatrix rotate-a)))
   1.459 +         arm-b)
   1.460           ]
   1.461           
   1.462                  
   1.463 @@ -673,26 +345,6 @@
   1.464      
   1.465          
   1.466    
   1.467 -(defn proprioception
   1.468 -  "Create a proprioception map that reports the rotations of the
   1.469 -  various limbs of the creature's body"
   1.470 -  [creature]
   1.471 -  [#^Node creature]
   1.472 -  (let [
   1.473 -        nodes (node-seq creature)
   1.474 -        joints
   1.475 -        (map
   1.476 -         :joint 
   1.477 -         (filter
   1.478 -          #(isa? (class %) JointControl)
   1.479 -          (reduce
   1.480 -           concat
   1.481 -           (map (fn [node]
   1.482 -                  (map (fn [num] (.getControl node num))
   1.483 -                       (range (.getNumControls node))))
   1.484 -                nodes))))]
   1.485 -    (fn []
   1.486 -      (reduce concat (map relative-positions (list (first joints)))))))
   1.487          
   1.488  
   1.489  (defn test-worm-control
   1.490 @@ -700,7 +352,7 @@
   1.491    (let [worm (point-worm)
   1.492          time (atom 0)
   1.493          worm-motor-map (motor-map worm)
   1.494 -        body-map (proprioception worm)
   1.495 +        ;;body-map (proprioception worm)
   1.496          debug-segments
   1.497          (map
   1.498           #(doto
   1.499 @@ -740,7 +392,7 @@
   1.500           (drop 1 (node-seq worm))
   1.501           debug-segments))
   1.502         (swap! time inc)
   1.503 -       (println-repl (with-out-str (clojure.pprint/pprint (doall (body-map)))))
   1.504 +       ;;(println-repl (with-out-str (clojure.pprint/pprint (doall (body-map)))))
   1.505         (Thread/sleep 200)
   1.506         (dorun (worm-motor-map
   1.507                 (worm-pattern @time)))))))
   1.508 @@ -755,7 +407,7 @@
   1.509    (let [hand    (box 1 0.2 0.2 :position (Vector3f. 0 2 0)
   1.510                       :mass 0 :color ColorRGBA/Green)
   1.511          finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0)
   1.512 -                     :mass 1 :color (ColorRGBA. 0.20 0.40 0.99 1.0))
   1.513 +                    :mass 1 :color (ColorRGBA. 0.20 0.40 0.99 1.0))
   1.514          floor   (box 10 0.5 10 :position (Vector3f. 0 -5 0)
   1.515                       :mass 0 :color ColorRGBA/Gray)
   1.516  
   1.517 @@ -786,7 +438,7 @@
   1.518               "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
   1.519               "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})     
   1.520       (fn [world]
   1.521 -       (set-gravity world Vector3f/ZERO)
   1.522 +       (set-gravity world (Vector3f. 0 0 0))
   1.523         (.setMoveSpeed (.getFlyByCamera world) 50)
   1.524         (.setRotationSpeed (.getFlyByCamera world) 50)
   1.525         (light-up-everything world)
   1.526 @@ -796,48 +448,372 @@
   1.527         (if @move-up?
   1.528           (.applyTorque control
   1.529                         (.mult (.getPhysicsRotation control)
   1.530 -                       (Vector3f. 0 0 1))))
   1.531 +                       (Vector3f. 0 0 10))))
   1.532         (if @move-down?
   1.533           (.applyTorque control
   1.534                         (.mult (.getPhysicsRotation control)
   1.535 -                              (Vector3f. 0 0 -1))))
   1.536 +                              (Vector3f. 0 0 -10))))
   1.537         (if @move-left?
   1.538           (.applyTorque control
   1.539                         (.mult (.getPhysicsRotation control)
   1.540 -                              (Vector3f. 0 1 0))))
   1.541 +                              (Vector3f. 0 10 0))))
   1.542         (if @move-right?
   1.543           (.applyTorque control
   1.544                         (.mult (.getPhysicsRotation control)
   1.545 -                              (Vector3f. 0 -1 0))))
   1.546 +                              (Vector3f. 0 -10 0))))
   1.547         (if @roll-left?
   1.548           (.applyTorque control
   1.549                         (.mult (.getPhysicsRotation control)
   1.550 -                              (Vector3f. -0.1 0 0))))
   1.551 +                              (Vector3f. -1 0 0))))
   1.552         (if @roll-right?
   1.553           (.applyTorque control
   1.554                         (.mult (.getPhysicsRotation control)
   1.555 -                              (Vector3f. 0.1 0 0))))
   1.556 +                              (Vector3f. 1 0 0))))
   1.557  
   1.558         (if (= 0 (rem (swap! time inc) 2000))
   1.559 -         (do 
   1.560 -           ;;(println-repl (.getLocalRotation finger))
   1.561 -           (println-repl (nth (relative-positions joint) 2))))
   1.562 -
   1.563 -       ))))
   1.564 -
   1.565 -
   1.566 -
   1.567 -
   1.568 -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   1.569 -
   1.570 -
   1.571 -
   1.572 +         (do
   1.573 +           
   1.574 +           (apply
   1.575 +            (comp
   1.576 +             println-repl
   1.577 +             #(format "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n" %1 %2 %3))
   1.578 +            (relative-positions joint))))))))
   1.579  
   1.580  #+end_src
   1.581  
   1.582 -#+results: body-main
   1.583 -: #'cortex.body/test-prop
   1.584  
   1.585 +* COMMENT failed-clojure-code
   1.586 +#+begin_src clojure
   1.587 +;;(.loadModel 
   1.588 +;; (doto (asset-manager)
   1.589 +;;   (.registerLoader BlenderModelLoader (into-array String ["blend"])))
   1.590 +;;   "Models/person/person.blend")
   1.591 +
   1.592 +(defn view-model [^String model]
   1.593 +  (view 
   1.594 +   (.loadModel 
   1.595 +    (doto (asset-manager)
   1.596 +      (.registerLoader BlenderModelLoader (into-array String ["blend"])))
   1.597 +    model)))
   1.598 +
   1.599 +(defn load-blender-scene [^String model]
   1.600 +  (.loadModel
   1.601 +   (doto (asset-manager)
   1.602 +     (.registerLoader BlenderLoader (into-array String ["blend"])))
   1.603 +    model))
   1.604 +
   1.605 +(defn worm
   1.606 +  []
   1.607 +  (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
   1.608 +
   1.609 +(defn oto
   1.610 +  []
   1.611 +  (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
   1.612 +
   1.613 +(defn sinbad
   1.614 +  []
   1.615 +  (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
   1.616 +
   1.617 +(defn worm-blender
   1.618 +  []
   1.619 +  (first (seq (.getChildren (load-blender-model
   1.620 +                             "Models/anim2/simple-worm.blend")))))
   1.621 +
   1.622 +(defn body
   1.623 +  "given a node with a SkeletonControl, will produce a body sutiable
   1.624 +  for AI control with movement and proprioception."
   1.625 +  [node]
   1.626 +  (let [skeleton-control (.getControl node SkeletonControl)
   1.627 +        krc (KinematicRagdollControl.)]
   1.628 +    (comment
   1.629 +    (dorun
   1.630 +     (map #(.addBoneName krc %)
   1.631 +          ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
   1.632 +           ;;"mid2" "mid3" "tail" "head"]
   1.633 +        )))
   1.634 +    (.addControl node krc)
   1.635 +    (.setRagdollMode krc)
   1.636 +    )
   1.637 +  node
   1.638 +  )
   1.639 +(defn show-skeleton [node]
   1.640 +  (let [sd
   1.641 +
   1.642 +        (doto
   1.643 +            (SkeletonDebugger. "aurellem-skel-debug"
   1.644 +                               (skel node))
   1.645 +          (.setMaterial (green-x-ray)))]
   1.646 +    (.attachChild node sd)
   1.647 +    node))
   1.648 +
   1.649 +
   1.650 +  
   1.651 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   1.652 +
   1.653 +;; this could be a good way to give objects special properties like
   1.654 +;; being eyes and the like
   1.655 +
   1.656 +(.getUserData 
   1.657 + (.getChild
   1.658 +  (load-blender-model "Models/property/test.blend") 0)
   1.659 + "properties")
   1.660 +
   1.661 +;; the properties are saved along with the blender file.
   1.662 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   1.663 +
   1.664 +
   1.665 +
   1.666 +
   1.667 +(defn init-debug-skel-node
   1.668 +  [f debug-node skeleton]
   1.669 +  (let [bones 
   1.670 +        (map #(.getBone skeleton %)
   1.671 +             (range (.getBoneCount skeleton)))]
   1.672 +  (dorun (map #(.setUserControl % true) bones)) 
   1.673 +  (dorun (map (fn [b]
   1.674 +                (println (.getName b)
   1.675 +                         " -- " (f b)))
   1.676 +              bones))
   1.677 +  (dorun
   1.678 +   (map #(.attachChild
   1.679 +          debug-node 
   1.680 +          (doto 
   1.681 +              (sphere 0.1
   1.682 +                      :position (f %)
   1.683 +                      :physical? false)
   1.684 +          (.setMaterial (green-x-ray))))
   1.685 +        bones)))
   1.686 +    debug-node)
   1.687 +
   1.688 +(import jme3test.bullet.PhysicsTestHelper)
   1.689 +
   1.690 +
   1.691 +(defn test-zzz [the-worm world value]
   1.692 +  (if (not value)
   1.693 +    (let [skeleton (skel the-worm)]
   1.694 +      (println-repl "enabling bones")
   1.695 +      (dorun
   1.696 +       (map
   1.697 +        #(.setUserControl (.getBone skeleton %) true)
   1.698 +        (range (.getBoneCount skeleton))))
   1.699 +
   1.700 +
   1.701 +      (let [b (.getBone skeleton 2)]
   1.702 +        (println-repl "moving " (.getName b))
   1.703 +        (println-repl (.getLocalPosition b))
   1.704 +        (.setUserTransforms b
   1.705 +                            Vector3f/UNIT_X
   1.706 +                            Quaternion/IDENTITY
   1.707 +                            ;;(doto (Quaternion.)
   1.708 +                             ;; (.fromAngles (/ Math/PI 2)
   1.709 +                             ;;              0
   1.710 +                             ;;              0
   1.711 +                                          
   1.712 +                            (Vector3f. 1 1 1))
   1.713 +        )
   1.714 +      
   1.715 +      (println-repl "hi! <3"))))
   1.716 +
   1.717 +
   1.718 +(defn test-ragdoll []
   1.719 +  
   1.720 +  (let [the-worm
   1.721 +        
   1.722 +         ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
   1.723 +         (doto (show-skeleton (worm-blender))
   1.724 +           (.setLocalTranslation (Vector3f. 0 10 0))
   1.725 +         ;;(worm)
   1.726 +         ;;(oto)
   1.727 +         ;;(sinbad)
   1.728 +        )
   1.729 +    ]
   1.730 +
   1.731 +
   1.732 +    (.start 
   1.733 +     (world
   1.734 +      (doto (Node.)
   1.735 +        (.attachChild the-worm))
   1.736 +      {"key-return" (fire-cannon-ball)
   1.737 +       "key-space" (partial test-zzz the-worm)
   1.738 +       }
   1.739 +      (fn [world]
   1.740 +        (light-up-everything world)
   1.741 +        (PhysicsTestHelper/createPhysicsTestWorld
   1.742 +         (.getRootNode world)
   1.743 +         (asset-manager)
   1.744 +         (.getPhysicsSpace
   1.745 +          (.getState (.getStateManager world) BulletAppState)))
   1.746 +        (set-gravity world Vector3f/ZERO)
   1.747 +        ;;(.setTimer world (NanoTimer.))
   1.748 +        ;;(org.lwjgl.input.Mouse/setGrabbed false)
   1.749 +        )
   1.750 +      no-op
   1.751 +      )
   1.752 +  
   1.753 +     
   1.754 +     )))
   1.755 +
   1.756 +
   1.757 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   1.758 +;;; here is the ragdoll stuff
   1.759 +
   1.760 +(def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
   1.761 +(def mesh worm-mesh)
   1.762 +
   1.763 +(.getFloatBuffer mesh VertexBuffer$Type/Position)
   1.764 +(.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
   1.765 +(.getData  (.getBuffer mesh VertexBuffer$Type/BoneIndex))
   1.766 +
   1.767 +
   1.768 +(defn position [index]
   1.769 +  (.get
   1.770 +   (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
   1.771 +     index))
   1.772 +
   1.773 +(defn bones [index]
   1.774 +  (.get
   1.775 +   (.getData  (.getBuffer mesh VertexBuffer$Type/BoneIndex))
   1.776 +   index))
   1.777 +
   1.778 +(defn bone-weights [index]
   1.779 +  (.get
   1.780 +   (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
   1.781 +   index))
   1.782 +
   1.783 +
   1.784 +
   1.785 +(defn vertex-bones [vertex]
   1.786 +  (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
   1.787 +
   1.788 +(defn vertex-weights [vertex]
   1.789 +  (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
   1.790 +
   1.791 +(defn vertex-position [index]
   1.792 +  (let [offset (* index 3)]
   1.793 +    (Vector3f. (position offset)
   1.794 +               (position (inc offset))
   1.795 +               (position (inc(inc offset))))))
   1.796 +
   1.797 +(def vertex-info (juxt vertex-position vertex-bones vertex-weights))
   1.798 +
   1.799 +(defn bone-control-color [index]
   1.800 +  (get {[1 0 0 0] ColorRGBA/Red
   1.801 +        [1 2 0 0] ColorRGBA/Magenta
   1.802 +        [2 0 0 0] ColorRGBA/Blue}
   1.803 +       (vertex-bones index)
   1.804 +       ColorRGBA/White))
   1.805 +
   1.806 +(defn influence-color [index bone-num]
   1.807 +  (get
   1.808 +   {(float 0)   ColorRGBA/Blue
   1.809 +    (float 0.5) ColorRGBA/Green
   1.810 +    (float 1)   ColorRGBA/Red}
   1.811 +     ;; find the weight of the desired bone
   1.812 +   ((zipmap (vertex-bones index)(vertex-weights index))
   1.813 +    bone-num)
   1.814 +   ColorRGBA/Blue))
   1.815 +         
   1.816 +(def worm-vertices (set (map vertex-info (range 60))))
   1.817 +
   1.818 +
   1.819 +(defn test-info []
   1.820 +  (let [points (Node.)]
   1.821 +    (dorun
   1.822 +     (map #(.attachChild points %)
   1.823 +          (map #(sphere 0.01
   1.824 +                        :position (vertex-position %)
   1.825 +                        :color (influence-color % 1)
   1.826 +                        :physical? false)
   1.827 +               (range 60))))
   1.828 +    (view points)))
   1.829 +  
   1.830 +
   1.831 +(defrecord JointControl [joint physics-space]
   1.832 +  PhysicsControl
   1.833 +  (setPhysicsSpace [this space]
   1.834 +    (dosync
   1.835 +     (ref-set (:physics-space this) space))
   1.836 +    (.addJoint space (:joint this)))
   1.837 +  (update [this tpf])
   1.838 +  (setSpatial [this spatial])
   1.839 +  (render [this rm vp])
   1.840 +  (getPhysicsSpace [this] (deref (:physics-space this)))
   1.841 +  (isEnabled [this] true)
   1.842 +  (setEnabled [this state]))
   1.843 +  
   1.844 +(defn add-joint
   1.845 +  "Add a joint to a particular object. When the object is added to the
   1.846 +  PhysicsSpace of a simulation, the joint will also be added"
   1.847 +  [object joint]
   1.848 +  (let [control (JointControl. joint (ref nil))]
   1.849 +    (.addControl object control))
   1.850 +  object)
   1.851 +
   1.852 +
   1.853 +(defn hinge-world
   1.854 +  []
   1.855 +  (let [sphere1 (sphere)
   1.856 +        sphere2 (sphere 1 :position (Vector3f. 3 3 3))
   1.857 +        joint (Point2PointJoint.
   1.858 +               (.getControl sphere1 RigidBodyControl)
   1.859 +               (.getControl sphere2 RigidBodyControl) 
   1.860 +               Vector3f/ZERO (Vector3f. 3 3 3))]
   1.861 +    (add-joint sphere1 joint)
   1.862 +    (doto (Node. "hinge-world")
   1.863 +      (.attachChild sphere1)
   1.864 +      (.attachChild sphere2))))
   1.865 +
   1.866 +
   1.867 +(defn test-joint []
   1.868 +  (view (hinge-world)))
   1.869 +
   1.870 +;; (defn copier-gen []
   1.871 +;;   (let [count (atom 0)]
   1.872 +;;     (fn [in]
   1.873 +;;       (swap! count inc)
   1.874 +;;       (clojure.contrib.duck-streams/copy
   1.875 +;;        in (File. (str "/home/r/tmp/mao-test/clojure-images/"
   1.876 +;;                      ;;/home/r/tmp/mao-test/clojure-images
   1.877 +;;                (format "%08d.png" @count)))))))
   1.878 +;; (defn decrease-framerate []
   1.879 +;;   (map
   1.880 +;;    (copier-gen)
   1.881 +;;    (sort
   1.882 +;;     (map first
   1.883 +;;          (partition
   1.884 +;;           4
   1.885 +;;           (filter #(re-matches #".*.png$" (.getCanonicalPath %))
   1.886 +;;                   (file-seq
   1.887 +;;                    (file-str
   1.888 +;;                     "/home/r/media/anime/mao-temp/images"))))))))
   1.889 +
   1.890 +
   1.891 +
   1.892 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   1.893 +
   1.894 +(defn proprioception
   1.895 +  "Create a proprioception map that reports the rotations of the
   1.896 +  various limbs of the creature's body"
   1.897 +  [creature]
   1.898 +  [#^Node creature]
   1.899 +  (let [
   1.900 +        nodes (node-seq creature)
   1.901 +        joints
   1.902 +        (map
   1.903 +         :joint 
   1.904 +         (filter
   1.905 +          #(isa? (class %) JointControl)
   1.906 +          (reduce
   1.907 +           concat
   1.908 +           (map (fn [node]
   1.909 +                  (map (fn [num] (.getControl node num))
   1.910 +                       (range (.getNumControls node))))
   1.911 +                nodes))))]
   1.912 +    (fn []
   1.913 +      (reduce concat (map relative-positions (list (first joints)))))))
   1.914 +
   1.915 +
   1.916 +#+end_src
   1.917  
   1.918  
   1.919