annotate org/body.org @ 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
rev   line source
rlm@0 1 #+title: The BODY!!!
rlm@0 2 #+author: Robert McIntyre
rlm@0 3 #+email: rlm@mit.edu
rlm@4 4 #+description: Simulating a body (movement, touch, propioception) in jMonkeyEngine3.
rlm@4 5 #+SETUPFILE: ../../aurellem/org/setup.org
rlm@4 6 #+INCLUDE: ../../aurellem/org/level-0.org
rlm@4 7
rlm@58 8 * Body
rlm@0 9
rlm@0 10 #+srcname: body-main
rlm@0 11 #+begin_src clojure
rlm@44 12 (ns cortex.body
rlm@44 13 (use (cortex world util import)))
rlm@44 14
rlm@0 15 (use 'clojure.contrib.def)
rlm@0 16 (cortex.import/mega-import-jme3)
rlm@0 17 (rlm.rlm-commands/help)
rlm@0 18
rlm@49 19 (defn load-blender-model
rlm@49 20 [^String model]
rlm@49 21 (.loadModel
rlm@49 22 (doto (asset-manager)
rlm@49 23 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@49 24 model))
rlm@49 25
rlm@52 26 (defn skel [node]
rlm@52 27 (doto
rlm@52 28 (.getSkeleton
rlm@52 29 (.getControl node SkeletonControl))
rlm@52 30 ;; this is necessary to force the skeleton to have accurate world
rlm@52 31 ;; transforms before it is rendered to the screen.
rlm@52 32 (.resetAndUpdate)))
rlm@52 33
rlm@50 34 (defprotocol Textual
rlm@50 35 (text [something]
rlm@50 36 "Display a detailed textual analysis of the given object."))
rlm@50 37
rlm@50 38 (extend-type com.jme3.scene.Node
rlm@50 39 Textual
rlm@50 40 (text [node]
rlm@50 41 (println "Total Vertexes: " (.getVertexCount node))
rlm@50 42 (println "Total Triangles: " (.getTriangleCount node))
rlm@50 43 (println "Controls :")
rlm@50 44 (dorun (map #(text (.getControl node %)) (range (.getNumControls node))))
rlm@50 45 (println "Has " (.getQuantity node) " Children:")
rlm@50 46 (doall (map text (.getChildren node)))))
rlm@50 47
rlm@50 48 (extend-type com.jme3.animation.AnimControl
rlm@50 49 Textual
rlm@50 50 (text [control]
rlm@50 51 (let [animations (.getAnimationNames control)]
rlm@52 52 (println "Animation Control with " (count animations) " animation(s):")
rlm@50 53 (dorun (map println animations)))))
rlm@50 54
rlm@50 55 (extend-type com.jme3.animation.SkeletonControl
rlm@50 56 Textual
rlm@50 57 (text [control]
rlm@50 58 (println "Skeleton Control with the following skeleton:")
rlm@50 59 (println (.getSkeleton control))))
rlm@50 60
rlm@52 61 (extend-type com.jme3.bullet.control.KinematicRagdollControl
rlm@52 62 Textual
rlm@52 63 (text [control]
rlm@52 64 (println "Ragdoll Control")))
rlm@52 65
rlm@52 66
rlm@50 67 (extend-type com.jme3.scene.Geometry
rlm@50 68 Textual
rlm@50 69 (text [control]
rlm@50 70 (println "...geo...")))
rlm@50 71
rlm@50 72
rlm@52 73 (defn green-x-ray []
rlm@51 74 (doto (Material. (asset-manager)
rlm@51 75 "Common/MatDefs/Misc/Unshaded.j3md")
rlm@51 76 (.setColor "Color" ColorRGBA/Green)
rlm@52 77 (-> (.getAdditionalRenderState)
rlm@52 78 (.setDepthTest false))))
rlm@51 79
rlm@51 80
rlm@58 81 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@58 82
rlm@60 83 ;;;;;;;;;;;; eve-style bodies ;;;;;;;;
rlm@58 84
rlm@58 85 (defn worm [segment-length num-segments interstitial-space radius]
rlm@58 86 (letfn [(nth-segment
rlm@58 87 [n]
rlm@58 88 (box segment-length radius radius :mass 0.1
rlm@58 89 :position
rlm@58 90 (Vector3f.
rlm@58 91 (* 2 n (+ interstitial-space segment-length)) 0 0)
rlm@58 92 :name (str "worm-segment" n)
rlm@58 93 :color (ColorRGBA/randomColor)))]
rlm@58 94 (map nth-segment (range num-segments))))
rlm@58 95
rlm@58 96 (defn nodify
rlm@58 97 "take a sequence of things that can be attached to a node and return
rlm@60 98 a node with all of them attached"
rlm@58 99 ([name children]
rlm@58 100 (let [node (Node. name)]
rlm@58 101 (dorun (map #(.attachChild node %) children))
rlm@58 102 node))
rlm@58 103 ([children] (nodify "" children)))
rlm@58 104
rlm@58 105 (defn connect-at-midpoint
rlm@58 106 [segmentA segmentB]
rlm@58 107 (let [centerA (.getWorldTranslation segmentA)
rlm@58 108 centerB (.getWorldTranslation segmentB)
rlm@58 109 midpoint (.mult (.add centerA centerB) (float 0.5))
rlm@58 110 pivotA (.subtract midpoint centerA)
rlm@58 111 pivotB (.subtract midpoint centerB)
rlm@58 112 joint (Point2PointJoint.
rlm@58 113 (.getControl segmentA RigidBodyControl)
rlm@58 114 (.getControl segmentB RigidBodyControl)
rlm@58 115 pivotA
rlm@58 116 pivotB)]
rlm@58 117 segmentB))
rlm@58 118
rlm@58 119 (defn point-worm []
rlm@58 120 (let [segments (worm 0.2 5 0.1 0.1)]
rlm@58 121 (dorun (map (partial apply connect-at-midpoint)
rlm@58 122 (partition 2 1 segments)))
rlm@58 123 (nodify "worm" segments)))
rlm@58 124
rlm@58 125 (defn test-worm []
rlm@58 126 (.start
rlm@58 127 (world
rlm@58 128 (doto (Node.)
rlm@58 129 ;;(.attachChild (point-worm))
rlm@58 130 (.attachChild (load-blender-model
rlm@58 131 "Models/anim2/joint-worm.blend"))
rlm@58 132
rlm@58 133 (.attachChild (box 10 1 10
rlm@58 134 :position (Vector3f. 0 -2 0) :mass 0
rlm@58 135 :color (ColorRGBA/Gray))))
rlm@58 136 {
rlm@58 137 "key-space" (fire-cannon-ball)
rlm@58 138 }
rlm@58 139 (fn [world]
rlm@58 140 (enable-debug world)
rlm@58 141 (light-up-everything world)
rlm@58 142 ;;(.setTimer world (NanoTimer.))
rlm@58 143 )
rlm@58 144 no-op)))
rlm@49 145
rlm@56 146 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@56 147
rlm@56 148
rlm@60 149 ;;;;;;;;; Mortor Control ;;;;;;;;;;;;;
rlm@56 150
rlm@56 151
rlm@60 152 ;; surprisingly ehough, terristerial creatures only move by using
rlm@60 153 ;; torque applied to their joints. There's not a single straight line
rlm@60 154 ;; of force in the human body at all! (a straight line of force would
rlm@60 155 ;; correspond to some sort of jet or rocket propulseion)
rlm@56 156
rlm@60 157 (defn node-seq
rlm@61 158 "Take a node and return a seq of all its children
rlm@60 159 recursively. There will be no nodes left in the resulting
rlm@60 160 structure"
rlm@60 161 [#^Node node]
rlm@60 162 (tree-seq #(isa? (class %) Node) #(.getChildren %) node))
rlm@60 163
rlm@60 164
rlm@60 165 (defn torque-controls [control]
rlm@60 166 (let [torques
rlm@60 167 (concat
rlm@60 168 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))
rlm@60 169 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))
rlm@60 170 [Vector3f/UNIT_X])]
rlm@60 171 (map (fn [torque-axis]
rlm@60 172 (fn [torque]
rlm@60 173 (.applyTorque
rlm@60 174 control
rlm@60 175 (.mult (.mult (.getPhysicsRotation control)
rlm@60 176 torque-axis)
rlm@60 177 (float
rlm@60 178 (* (.getMass control) torque))))))
rlm@60 179 torques)))
rlm@56 180
rlm@60 181 (defn motor-map
rlm@60 182 "Take a creature and generate a function that will enable fine
rlm@60 183 grained control over all the creature's limbs."
rlm@60 184 [#^Node creature]
rlm@60 185 (let [controls (keep #(.getControl % RigidBodyControl)
rlm@60 186 (node-seq creature))
rlm@60 187 limb-controls (reduce concat (map torque-controls controls))
rlm@60 188 body-control (partial map #(%1 %2) limb-controls)]
rlm@60 189 body-control))
rlm@56 190
rlm@60 191 (defn test-motor-map
rlm@60 192 "see how torque works."
rlm@60 193 []
rlm@60 194 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
rlm@60 195 :mass 1 :color ColorRGBA/Green)
rlm@60 196 motor-map (motor-map finger)]
rlm@60 197 (world
rlm@60 198 (nodify [finger
rlm@60 199 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
rlm@60 200 :color ColorRGBA/Gray)])
rlm@60 201 standard-debug-controls
rlm@60 202 (fn [world]
rlm@60 203 (set-gravity world Vector3f/ZERO)
rlm@60 204 (light-up-everything world)
rlm@60 205 (.setTimer world (NanoTimer.)))
rlm@60 206 (fn [_ _]
rlm@60 207 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))
rlm@60 208
rlm@60 209 (defn test-torque
rlm@60 210 "see how torque works."
rlm@60 211 []
rlm@60 212 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
rlm@60 213 :mass 1 :color ColorRGBA/Green)
rlm@60 214 move-left? (atom false)
rlm@60 215 move-right? (atom false)
rlm@60 216 control (.getControl finger RigidBodyControl)]
rlm@60 217 (world
rlm@60 218 (nodify [finger
rlm@60 219 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
rlm@60 220 :color ColorRGBA/Gray)])
rlm@60 221 (merge standard-debug-controls
rlm@60 222 {"key-k" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@60 223 "key-l" (fn [_ pressed?] (reset! move-right? pressed?))})
rlm@60 224 (fn [world]
rlm@60 225 (set-gravity world Vector3f/ZERO)
rlm@60 226 (light-up-everything world)
rlm@60 227 (.setTimer world (NanoTimer.)))
rlm@60 228 (fn [_ _]
rlm@60 229 (if @move-left?
rlm@60 230 (.applyTorque control
rlm@60 231 (.mult (.getPhysicsRotation control)
rlm@60 232 (Vector3f. -3 20 0))))
rlm@60 233 (if @move-right?
rlm@60 234 (.applyTorque control (Vector3f. 0 0 1)))))))
rlm@60 235
rlm@60 236 (defn worm-pattern [time]
rlm@60 237 [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rlm@60 238
rlm@60 239 0 0 0 0 0 0 0 0 0 0 0
rlm@60 240
rlm@60 241 (* 20 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))
rlm@60 242
rlm@60 243 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rlm@60 244 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rlm@60 245 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rlm@60 246
rlm@60 247 ])
rlm@60 248
rlm@60 249 ;;;;;;;;;;;;;;;;;; Proprioception ;;;;;;;;;;;;;;;;;;;;;;;;
rlm@60 250
rlm@60 251 ;; this is not used as just getting the rotation would be simpler.
rlm@60 252 (defn proprioception-senses
rlm@60 253 "given a control , create a sequence of thunks that will report the
rlm@60 254 rotation of the control's object along the same axes as the motor-control map."
rlm@60 255 [control]
rlm@60 256 (let [torques
rlm@60 257 (concat
rlm@60 258 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))
rlm@60 259 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))
rlm@60 260 [Vector3f/UNIT_X])]
rlm@60 261 (map (fn [torque-axis]
rlm@60 262 (fn []
rlm@60 263 (.getPhysicsRotation control)))
rlm@60 264 torques)))
rlm@60 265
rlm@60 266 (defn orthogonal-vect
rlm@60 267 "Return a vector orthogonal to the current one"
rlm@60 268 [vector]
rlm@60 269 (let [x (.getX vector)
rlm@60 270 y (.getY vector)
rlm@60 271 z (.getZ vector)]
rlm@60 272 (cond
rlm@60 273 (not= x (float 0)) (Vector3f. (- z) 0 x)
rlm@60 274 (not= y (float 0)) (Vector3f. 0 (- z) y)
rlm@60 275 (not= z (float 0)) (Vector3f. 0 (- z) y)
rlm@60 276 true Vector3f/ZERO)))
rlm@60 277
rlm@60 278 ;; from
rlm@60 279 ;; http://stackoverflow.com/questions/3684269/ \\
rlm@60 280 ;; component-of-a-quaternion-rotation-around-an-axis
rlm@60 281 (defn rot-about-axis [#^Quaternion q #^Vector3f axis]
rlm@60 282 (let [basis-1 (orthogonal-vect axis)
rlm@60 283 basis-2 (.cross axis basis-1)
rlm@60 284 rotated (.mult q basis-1)
rlm@60 285 alpha (.dot basis-1 (.project rotated basis-1))
rlm@60 286 beta (.dot basis-2 (.project rotated basis-2))]
rlm@60 287 (Math/atan2 beta alpha)))
rlm@60 288
rlm@60 289
rlm@60 290 (defn check-rot [a]
rlm@60 291 (rot-about-axis
rlm@60 292 (doto (Quaternion.)
rlm@60 293 (.fromAngleAxis
rlm@60 294 (float a)
rlm@60 295 (Vector3f. 1 0 0))) (Vector3f. 1 0 0)))
rlm@60 296
rlm@60 297 (defn relative-positions [joint]
rlm@60 298 (let [object-a (.getUserObject (.getBodyA joint))
rlm@60 299 object-b (.getUserObject (.getBodyB joint))
rlm@60 300 arm-a
rlm@60 301 (.normalize
rlm@60 302 (.subtract
rlm@60 303 (.localToWorld object-a (.getPivotA joint) nil)
rlm@60 304 (.getWorldTranslation object-a)))
rlm@60 305 rotate-a
rlm@60 306 (doto (Matrix3f.)
rlm@60 307 (.fromStartEndVectors arm-a Vector3f/UNIT_X))
rlm@60 308 arm-b
rlm@60 309 (.mult
rlm@60 310 rotate-a
rlm@60 311 (.normalize
rlm@60 312 (.subtract
rlm@60 313 (.localToWorld object-b (.getPivotB joint) nil)
rlm@60 314 (.getWorldTranslation object-b))))
rlm@60 315 pitch
rlm@60 316 (.angleBetween
rlm@60 317 (.normalize (Vector2f. (.getX arm-b) (.getY arm-b)))
rlm@60 318 (Vector2f. 1 0))
rlm@60 319 yaw
rlm@60 320 (.angleBetween
rlm@60 321 (.normalize (Vector2f. (.getX arm-b) (.getZ arm-b)))
rlm@60 322 (Vector2f. 1 0))
rlm@60 323
rlm@60 324 roll
rlm@61 325 (rot-about-axis
rlm@61 326 (.mult
rlm@61 327 (.getLocalRotation object-b)
rlm@61 328 (doto (Quaternion.)
rlm@61 329 (.fromRotationMatrix rotate-a)))
rlm@61 330 arm-b)
rlm@60 331 ]
rlm@60 332
rlm@60 333
rlm@60 334
rlm@60 335 ;;(println-repl
rlm@60 336 ;; "arm-b is " arm-b)
rlm@60 337 ;;(println-repl
rlm@60 338 ;; "pivot-b is " (.getPivotB joint))
rlm@60 339 ;;(println-repl
rlm@60 340 ;; (format "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n"
rlm@60 341 ;; pitch yaw roll))
rlm@60 342 [pitch yaw roll]))
rlm@60 343
rlm@60 344
rlm@60 345
rlm@60 346
rlm@60 347
rlm@60 348
rlm@60 349
rlm@60 350 (defn test-worm-control
rlm@60 351 []
rlm@60 352 (let [worm (point-worm)
rlm@60 353 time (atom 0)
rlm@60 354 worm-motor-map (motor-map worm)
rlm@61 355 ;;body-map (proprioception worm)
rlm@60 356 debug-segments
rlm@60 357 (map
rlm@60 358 #(doto
rlm@60 359 (make-shape
rlm@60 360 (assoc base-shape
rlm@60 361 :name "debug-line"
rlm@60 362 :physical? false
rlm@60 363 :shape
rlm@60 364 (com.jme3.scene.shape.Line.
rlm@60 365 (.add (.getWorldTranslation %)
rlm@60 366 (Vector3f. -0.2 0 0 ))
rlm@60 367 (.add (.getWorldTranslation %)
rlm@60 368 (Vector3f. 0.2 0 0)))))
rlm@60 369 (.setMaterial (green-x-ray)))
rlm@60 370 (drop 1 (node-seq worm)))]
rlm@60 371 (world
rlm@60 372 (nodify [worm
rlm@60 373 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
rlm@60 374 :color ColorRGBA/Gray)])
rlm@60 375 standard-debug-controls
rlm@60 376 (fn [world]
rlm@60 377 (.attachChild (.getRootNode world) (nodify debug-segments))
rlm@60 378 (enable-debug world)
rlm@60 379 (light-up-everything world)
rlm@60 380 (com.aurellem.capture.Capture/captureVideo
rlm@60 381 world
rlm@60 382 (file-str "/home/r/proj/cortex/tmp/moving-worm")))
rlm@60 383
rlm@60 384 (fn [_ _]
rlm@60 385 (dorun
rlm@60 386 (map
rlm@60 387 (fn [worm-segment
rlm@60 388 debug-segment]
rlm@60 389 (.rotate
rlm@60 390 debug-segment
rlm@60 391 (Quaternion. (float 0) (float 0.05) (float 0) (float 1))))
rlm@60 392 (drop 1 (node-seq worm))
rlm@60 393 debug-segments))
rlm@60 394 (swap! time inc)
rlm@61 395 ;;(println-repl (with-out-str (clojure.pprint/pprint (doall (body-map)))))
rlm@60 396 (Thread/sleep 200)
rlm@60 397 (dorun (worm-motor-map
rlm@60 398 (worm-pattern @time)))))))
rlm@60 399
rlm@60 400
rlm@60 401
rlm@60 402
rlm@60 403
rlm@60 404 (defn test-prop
rlm@60 405 "see how torque works."
rlm@60 406 []
rlm@60 407 (let [hand (box 1 0.2 0.2 :position (Vector3f. 0 2 0)
rlm@60 408 :mass 0 :color ColorRGBA/Green)
rlm@60 409 finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0)
rlm@61 410 :mass 1 :color (ColorRGBA. 0.20 0.40 0.99 1.0))
rlm@60 411 floor (box 10 0.5 10 :position (Vector3f. 0 -5 0)
rlm@60 412 :mass 0 :color ColorRGBA/Gray)
rlm@60 413
rlm@60 414 move-up? (atom false)
rlm@60 415 move-down? (atom false)
rlm@60 416 move-left? (atom false)
rlm@60 417 move-right? (atom false)
rlm@60 418 roll-left? (atom false)
rlm@60 419 roll-right? (atom false)
rlm@60 420 control (.getControl finger RigidBodyControl)
rlm@60 421 joint
rlm@60 422 (doto
rlm@60 423 (Point2PointJoint.
rlm@60 424 (.getControl hand RigidBodyControl)
rlm@60 425 control
rlm@60 426 (Vector3f. 1.2 0 0)
rlm@60 427 (Vector3f. -1.2 0 0 ))
rlm@60 428 (.setCollisionBetweenLinkedBodys false))
rlm@60 429 time (atom 0)
rlm@60 430 ]
rlm@60 431 (world
rlm@60 432 (nodify [hand finger floor])
rlm@60 433 (merge standard-debug-controls
rlm@60 434 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@60 435 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@60 436 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@60 437 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@60 438 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@60 439 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@60 440 (fn [world]
rlm@61 441 (set-gravity world (Vector3f. 0 0 0))
rlm@60 442 (.setMoveSpeed (.getFlyByCamera world) 50)
rlm@60 443 (.setRotationSpeed (.getFlyByCamera world) 50)
rlm@60 444 (light-up-everything world)
rlm@60 445 (.setTimer world (NanoTimer.))
rlm@60 446 )
rlm@60 447 (fn [_ _]
rlm@60 448 (if @move-up?
rlm@60 449 (.applyTorque control
rlm@60 450 (.mult (.getPhysicsRotation control)
rlm@61 451 (Vector3f. 0 0 10))))
rlm@60 452 (if @move-down?
rlm@60 453 (.applyTorque control
rlm@60 454 (.mult (.getPhysicsRotation control)
rlm@61 455 (Vector3f. 0 0 -10))))
rlm@60 456 (if @move-left?
rlm@60 457 (.applyTorque control
rlm@60 458 (.mult (.getPhysicsRotation control)
rlm@61 459 (Vector3f. 0 10 0))))
rlm@60 460 (if @move-right?
rlm@60 461 (.applyTorque control
rlm@60 462 (.mult (.getPhysicsRotation control)
rlm@61 463 (Vector3f. 0 -10 0))))
rlm@60 464 (if @roll-left?
rlm@60 465 (.applyTorque control
rlm@60 466 (.mult (.getPhysicsRotation control)
rlm@61 467 (Vector3f. -1 0 0))))
rlm@60 468 (if @roll-right?
rlm@60 469 (.applyTorque control
rlm@60 470 (.mult (.getPhysicsRotation control)
rlm@61 471 (Vector3f. 1 0 0))))
rlm@60 472
rlm@60 473 (if (= 0 (rem (swap! time inc) 2000))
rlm@61 474 (do
rlm@61 475
rlm@61 476 (apply
rlm@61 477 (comp
rlm@61 478 println-repl
rlm@61 479 #(format "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n" %1 %2 %3))
rlm@61 480 (relative-positions joint))))))))
rlm@56 481
rlm@0 482 #+end_src
rlm@0 483
rlm@60 484
rlm@61 485 * COMMENT failed-clojure-code
rlm@61 486 #+begin_src clojure
rlm@61 487 ;;(.loadModel
rlm@61 488 ;; (doto (asset-manager)
rlm@61 489 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@61 490 ;; "Models/person/person.blend")
rlm@61 491
rlm@61 492 (defn view-model [^String model]
rlm@61 493 (view
rlm@61 494 (.loadModel
rlm@61 495 (doto (asset-manager)
rlm@61 496 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@61 497 model)))
rlm@61 498
rlm@61 499 (defn load-blender-scene [^String model]
rlm@61 500 (.loadModel
rlm@61 501 (doto (asset-manager)
rlm@61 502 (.registerLoader BlenderLoader (into-array String ["blend"])))
rlm@61 503 model))
rlm@61 504
rlm@61 505 (defn worm
rlm@61 506 []
rlm@61 507 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
rlm@61 508
rlm@61 509 (defn oto
rlm@61 510 []
rlm@61 511 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
rlm@61 512
rlm@61 513 (defn sinbad
rlm@61 514 []
rlm@61 515 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
rlm@61 516
rlm@61 517 (defn worm-blender
rlm@61 518 []
rlm@61 519 (first (seq (.getChildren (load-blender-model
rlm@61 520 "Models/anim2/simple-worm.blend")))))
rlm@61 521
rlm@61 522 (defn body
rlm@61 523 "given a node with a SkeletonControl, will produce a body sutiable
rlm@61 524 for AI control with movement and proprioception."
rlm@61 525 [node]
rlm@61 526 (let [skeleton-control (.getControl node SkeletonControl)
rlm@61 527 krc (KinematicRagdollControl.)]
rlm@61 528 (comment
rlm@61 529 (dorun
rlm@61 530 (map #(.addBoneName krc %)
rlm@61 531 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
rlm@61 532 ;;"mid2" "mid3" "tail" "head"]
rlm@61 533 )))
rlm@61 534 (.addControl node krc)
rlm@61 535 (.setRagdollMode krc)
rlm@61 536 )
rlm@61 537 node
rlm@61 538 )
rlm@61 539 (defn show-skeleton [node]
rlm@61 540 (let [sd
rlm@61 541
rlm@61 542 (doto
rlm@61 543 (SkeletonDebugger. "aurellem-skel-debug"
rlm@61 544 (skel node))
rlm@61 545 (.setMaterial (green-x-ray)))]
rlm@61 546 (.attachChild node sd)
rlm@61 547 node))
rlm@61 548
rlm@61 549
rlm@61 550
rlm@61 551 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@61 552
rlm@61 553 ;; this could be a good way to give objects special properties like
rlm@61 554 ;; being eyes and the like
rlm@61 555
rlm@61 556 (.getUserData
rlm@61 557 (.getChild
rlm@61 558 (load-blender-model "Models/property/test.blend") 0)
rlm@61 559 "properties")
rlm@61 560
rlm@61 561 ;; the properties are saved along with the blender file.
rlm@61 562 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@61 563
rlm@61 564
rlm@61 565
rlm@61 566
rlm@61 567 (defn init-debug-skel-node
rlm@61 568 [f debug-node skeleton]
rlm@61 569 (let [bones
rlm@61 570 (map #(.getBone skeleton %)
rlm@61 571 (range (.getBoneCount skeleton)))]
rlm@61 572 (dorun (map #(.setUserControl % true) bones))
rlm@61 573 (dorun (map (fn [b]
rlm@61 574 (println (.getName b)
rlm@61 575 " -- " (f b)))
rlm@61 576 bones))
rlm@61 577 (dorun
rlm@61 578 (map #(.attachChild
rlm@61 579 debug-node
rlm@61 580 (doto
rlm@61 581 (sphere 0.1
rlm@61 582 :position (f %)
rlm@61 583 :physical? false)
rlm@61 584 (.setMaterial (green-x-ray))))
rlm@61 585 bones)))
rlm@61 586 debug-node)
rlm@61 587
rlm@61 588 (import jme3test.bullet.PhysicsTestHelper)
rlm@61 589
rlm@61 590
rlm@61 591 (defn test-zzz [the-worm world value]
rlm@61 592 (if (not value)
rlm@61 593 (let [skeleton (skel the-worm)]
rlm@61 594 (println-repl "enabling bones")
rlm@61 595 (dorun
rlm@61 596 (map
rlm@61 597 #(.setUserControl (.getBone skeleton %) true)
rlm@61 598 (range (.getBoneCount skeleton))))
rlm@61 599
rlm@61 600
rlm@61 601 (let [b (.getBone skeleton 2)]
rlm@61 602 (println-repl "moving " (.getName b))
rlm@61 603 (println-repl (.getLocalPosition b))
rlm@61 604 (.setUserTransforms b
rlm@61 605 Vector3f/UNIT_X
rlm@61 606 Quaternion/IDENTITY
rlm@61 607 ;;(doto (Quaternion.)
rlm@61 608 ;; (.fromAngles (/ Math/PI 2)
rlm@61 609 ;; 0
rlm@61 610 ;; 0
rlm@61 611
rlm@61 612 (Vector3f. 1 1 1))
rlm@61 613 )
rlm@61 614
rlm@61 615 (println-repl "hi! <3"))))
rlm@61 616
rlm@61 617
rlm@61 618 (defn test-ragdoll []
rlm@61 619
rlm@61 620 (let [the-worm
rlm@61 621
rlm@61 622 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
rlm@61 623 (doto (show-skeleton (worm-blender))
rlm@61 624 (.setLocalTranslation (Vector3f. 0 10 0))
rlm@61 625 ;;(worm)
rlm@61 626 ;;(oto)
rlm@61 627 ;;(sinbad)
rlm@61 628 )
rlm@61 629 ]
rlm@61 630
rlm@61 631
rlm@61 632 (.start
rlm@61 633 (world
rlm@61 634 (doto (Node.)
rlm@61 635 (.attachChild the-worm))
rlm@61 636 {"key-return" (fire-cannon-ball)
rlm@61 637 "key-space" (partial test-zzz the-worm)
rlm@61 638 }
rlm@61 639 (fn [world]
rlm@61 640 (light-up-everything world)
rlm@61 641 (PhysicsTestHelper/createPhysicsTestWorld
rlm@61 642 (.getRootNode world)
rlm@61 643 (asset-manager)
rlm@61 644 (.getPhysicsSpace
rlm@61 645 (.getState (.getStateManager world) BulletAppState)))
rlm@61 646 (set-gravity world Vector3f/ZERO)
rlm@61 647 ;;(.setTimer world (NanoTimer.))
rlm@61 648 ;;(org.lwjgl.input.Mouse/setGrabbed false)
rlm@61 649 )
rlm@61 650 no-op
rlm@61 651 )
rlm@61 652
rlm@61 653
rlm@61 654 )))
rlm@61 655
rlm@61 656
rlm@61 657 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@61 658 ;;; here is the ragdoll stuff
rlm@61 659
rlm@61 660 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
rlm@61 661 (def mesh worm-mesh)
rlm@61 662
rlm@61 663 (.getFloatBuffer mesh VertexBuffer$Type/Position)
rlm@61 664 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
rlm@61 665 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
rlm@61 666
rlm@61 667
rlm@61 668 (defn position [index]
rlm@61 669 (.get
rlm@61 670 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
rlm@61 671 index))
rlm@61 672
rlm@61 673 (defn bones [index]
rlm@61 674 (.get
rlm@61 675 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
rlm@61 676 index))
rlm@61 677
rlm@61 678 (defn bone-weights [index]
rlm@61 679 (.get
rlm@61 680 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
rlm@61 681 index))
rlm@61 682
rlm@61 683
rlm@61 684
rlm@61 685 (defn vertex-bones [vertex]
rlm@61 686 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
rlm@61 687
rlm@61 688 (defn vertex-weights [vertex]
rlm@61 689 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
rlm@61 690
rlm@61 691 (defn vertex-position [index]
rlm@61 692 (let [offset (* index 3)]
rlm@61 693 (Vector3f. (position offset)
rlm@61 694 (position (inc offset))
rlm@61 695 (position (inc(inc offset))))))
rlm@61 696
rlm@61 697 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))
rlm@61 698
rlm@61 699 (defn bone-control-color [index]
rlm@61 700 (get {[1 0 0 0] ColorRGBA/Red
rlm@61 701 [1 2 0 0] ColorRGBA/Magenta
rlm@61 702 [2 0 0 0] ColorRGBA/Blue}
rlm@61 703 (vertex-bones index)
rlm@61 704 ColorRGBA/White))
rlm@61 705
rlm@61 706 (defn influence-color [index bone-num]
rlm@61 707 (get
rlm@61 708 {(float 0) ColorRGBA/Blue
rlm@61 709 (float 0.5) ColorRGBA/Green
rlm@61 710 (float 1) ColorRGBA/Red}
rlm@61 711 ;; find the weight of the desired bone
rlm@61 712 ((zipmap (vertex-bones index)(vertex-weights index))
rlm@61 713 bone-num)
rlm@61 714 ColorRGBA/Blue))
rlm@61 715
rlm@61 716 (def worm-vertices (set (map vertex-info (range 60))))
rlm@61 717
rlm@61 718
rlm@61 719 (defn test-info []
rlm@61 720 (let [points (Node.)]
rlm@61 721 (dorun
rlm@61 722 (map #(.attachChild points %)
rlm@61 723 (map #(sphere 0.01
rlm@61 724 :position (vertex-position %)
rlm@61 725 :color (influence-color % 1)
rlm@61 726 :physical? false)
rlm@61 727 (range 60))))
rlm@61 728 (view points)))
rlm@61 729
rlm@61 730
rlm@61 731 (defrecord JointControl [joint physics-space]
rlm@61 732 PhysicsControl
rlm@61 733 (setPhysicsSpace [this space]
rlm@61 734 (dosync
rlm@61 735 (ref-set (:physics-space this) space))
rlm@61 736 (.addJoint space (:joint this)))
rlm@61 737 (update [this tpf])
rlm@61 738 (setSpatial [this spatial])
rlm@61 739 (render [this rm vp])
rlm@61 740 (getPhysicsSpace [this] (deref (:physics-space this)))
rlm@61 741 (isEnabled [this] true)
rlm@61 742 (setEnabled [this state]))
rlm@61 743
rlm@61 744 (defn add-joint
rlm@61 745 "Add a joint to a particular object. When the object is added to the
rlm@61 746 PhysicsSpace of a simulation, the joint will also be added"
rlm@61 747 [object joint]
rlm@61 748 (let [control (JointControl. joint (ref nil))]
rlm@61 749 (.addControl object control))
rlm@61 750 object)
rlm@61 751
rlm@61 752
rlm@61 753 (defn hinge-world
rlm@61 754 []
rlm@61 755 (let [sphere1 (sphere)
rlm@61 756 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
rlm@61 757 joint (Point2PointJoint.
rlm@61 758 (.getControl sphere1 RigidBodyControl)
rlm@61 759 (.getControl sphere2 RigidBodyControl)
rlm@61 760 Vector3f/ZERO (Vector3f. 3 3 3))]
rlm@61 761 (add-joint sphere1 joint)
rlm@61 762 (doto (Node. "hinge-world")
rlm@61 763 (.attachChild sphere1)
rlm@61 764 (.attachChild sphere2))))
rlm@61 765
rlm@61 766
rlm@61 767 (defn test-joint []
rlm@61 768 (view (hinge-world)))
rlm@61 769
rlm@61 770 ;; (defn copier-gen []
rlm@61 771 ;; (let [count (atom 0)]
rlm@61 772 ;; (fn [in]
rlm@61 773 ;; (swap! count inc)
rlm@61 774 ;; (clojure.contrib.duck-streams/copy
rlm@61 775 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"
rlm@61 776 ;; ;;/home/r/tmp/mao-test/clojure-images
rlm@61 777 ;; (format "%08d.png" @count)))))))
rlm@61 778 ;; (defn decrease-framerate []
rlm@61 779 ;; (map
rlm@61 780 ;; (copier-gen)
rlm@61 781 ;; (sort
rlm@61 782 ;; (map first
rlm@61 783 ;; (partition
rlm@61 784 ;; 4
rlm@61 785 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))
rlm@61 786 ;; (file-seq
rlm@61 787 ;; (file-str
rlm@61 788 ;; "/home/r/media/anime/mao-temp/images"))))))))
rlm@61 789
rlm@61 790
rlm@61 791
rlm@61 792 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@61 793
rlm@61 794 (defn proprioception
rlm@61 795 "Create a proprioception map that reports the rotations of the
rlm@61 796 various limbs of the creature's body"
rlm@61 797 [creature]
rlm@61 798 [#^Node creature]
rlm@61 799 (let [
rlm@61 800 nodes (node-seq creature)
rlm@61 801 joints
rlm@61 802 (map
rlm@61 803 :joint
rlm@61 804 (filter
rlm@61 805 #(isa? (class %) JointControl)
rlm@61 806 (reduce
rlm@61 807 concat
rlm@61 808 (map (fn [node]
rlm@61 809 (map (fn [num] (.getControl node num))
rlm@61 810 (range (.getNumControls node))))
rlm@61 811 nodes))))]
rlm@61 812 (fn []
rlm@61 813 (reduce concat (map relative-positions (list (first joints)))))))
rlm@61 814
rlm@61 815
rlm@61 816 #+end_src
rlm@0 817
rlm@0 818
rlm@0 819
rlm@0 820
rlm@0 821
rlm@0 822
rlm@0 823
rlm@0 824 * COMMENT generate Source.
rlm@44 825 #+begin_src clojure :tangle ../src/cortex/body.clj
rlm@0 826 <<body-main>>
rlm@0 827 #+end_src
rlm@0 828
rlm@60 829