annotate org/integration.org @ 297:d1206b11ae2d

creating final video
author Robert McIntyre <rlm@mit.edu>
date Thu, 16 Feb 2012 12:48:51 -0700
parents 1eed471e2ebf
children 85f3ff3e3f26
rev   line source
rlm@281 1 #+title:
rlm@73 2 #+author: Robert McIntyre
rlm@73 3 #+email: rlm@mit.edu
rlm@73 4 #+description:
rlm@73 5 #+keywords: simulation, jMonkeyEngine3, clojure
rlm@73 6 #+SETUPFILE: ../../aurellem/org/setup.org
rlm@73 7 #+INCLUDE: ../../aurellem/org/level-0.org
rlm@73 8
rlm@281 9 * Intro
rlm@129 10
rlm@281 11 This is the ultimate test which features all of the senses that I've
rlm@281 12 made so far. The blender file for the creature serves as an example of
rlm@281 13 a fully equipped creature in terms of senses. You can find it [[../assets/Models/test-creature/hand.blend][here]].
rlm@73 14
rlm@192 15 #+name: integration
rlm@73 16 #+begin_src clojure
rlm@192 17 (ns cortex.integration
rlm@73 18 "let's play!"
rlm@192 19 {:author "Robert McIntyre"}
rlm@281 20 (:use (cortex world util body sense
rlm@281 21 hearing touch vision proprioception movement))
rlm@192 22 (:import (com.jme3.math ColorRGBA Vector3f))
rlm@281 23 (:import java.io.File)
rlm@192 24 (:import com.jme3.audio.AudioNode)
rlm@192 25 (:import com.aurellem.capture.RatchetTimer))
rlm@73 26
rlm@281 27 (dorun (cortex.import/mega-import-jme3))
rlm@281 28 (rlm.rlm-commands/help)
rlm@74 29
rlm@281 30 (def hand "Models/test-creature/hand.blend")
rlm@78 31
rlm@281 32 (def output-base (File. "/home/r/proj/cortex/render/hand"))
rlm@189 33
rlm@297 34
rlm@297 35 ;; Let the hand fall palm-up
rlm@297 36
rlm@297 37 ;; it curls its phalanges, starting with the pinky.
rlm@297 38
rlm@297 39 ;; it lets these fall back down?
rlm@297 40
rlm@297 41 ;; block falls down onto the hand, accompanied by a sound. The block
rlm@297 42 ;; can be seen by the hand's eye.
rlm@297 43
rlm@297 44 ;; hand FORCEFULLY catapults the block so that it hits the camera.
rlm@297 45
rlm@297 46
rlm@296 47 (defn motor-control-program
rlm@296 48 "Create a function which will execute the motor script"
rlm@296 49 [muscle-positions
rlm@296 50 script]
rlm@296 51 (let [current-frame (atom -1)
rlm@296 52 keyed-script (group-by first script)
rlm@296 53 current-forces (atom {}) ]
rlm@296 54 (fn [effectors]
rlm@296 55 (let [indexed-effectors (vec effectors)]
rlm@296 56 (dorun
rlm@296 57 (for [[_ part force] (keyed-script (swap! current-frame inc))]
rlm@296 58 (swap! current-forces (fn [m] (assoc m part force)))))
rlm@296 59 (doall (map (fn [effector power]
rlm@296 60 (effector (int power)))
rlm@296 61 effectors
rlm@296 62 (map #(@current-forces % 0) muscle-positions)))))))
rlm@296 63
rlm@296 64 (def muscle-positions
rlm@296 65 [:pointer-2-e
rlm@296 66 :pointer-2-f
rlm@296 67 :thumb-1
rlm@296 68 :thumb-1
rlm@296 69 :pointer-1-e
rlm@296 70 :pointer-1-f
rlm@296 71 :thumb-2-e
rlm@296 72 :thumb-2-f
rlm@296 73 :middle-1-e
rlm@296 74 :middle-1-f
rlm@296 75 :pointer-3-f
rlm@296 76 :pointer-3-e
rlm@296 77 :middle-2-e
rlm@296 78 :middle-2-f
rlm@296 79 :middle-3-f
rlm@296 80 :middle-3-e
rlm@296 81 :pinky-2-e
rlm@296 82 :pinky-2-f
rlm@296 83 :pinky-3-f
rlm@296 84 :pinky-3-e
rlm@296 85 :ring-3-e
rlm@296 86 :ring-3-f
rlm@296 87 :ring-2-f
rlm@296 88 :ring-2-e
rlm@296 89 :ring-1-e
rlm@296 90 :ring-1-f
rlm@296 91 :thumb-1-e
rlm@296 92 :thumb-1-f
rlm@296 93 :pinky-1-f
rlm@296 94 :pinky-1-e])
rlm@296 95
rlm@296 96 (def full 9001)
rlm@296 97 ;; the systax here is [keyframe body-part force]
rlm@296 98 (def wiggle-each-finger-tip
rlm@297 99 [[300 :pointer-3-f 50]
rlm@297 100
rlm@296 101
rlm@296 102 [400 :middle-3-f full]
rlm@297 103 [401 :middle-3-f 50]
rlm@296 104
rlm@296 105 [600 :ring-3-f full]
rlm@296 106 [601 :ring-3-f 40]
rlm@296 107
rlm@296 108 [800 :pinky-3-f full]
rlm@297 109 [801 :pinky-3-f 50]
rlm@296 110
rlm@297 111 [1300 :pointer-1-f full]
rlm@297 112 [1300 :pointer-2-f full]
rlm@297 113 [1300 :pointer-3-f 0]
rlm@296 114
rlm@297 115 [1289 :middle-1-f full]
rlm@297 116 [1289 :middle-2-f full]
rlm@297 117 [1289 :middle-3-f 0]
rlm@297 118
rlm@297 119 [1310 :pointer-1-f 0]
rlm@297 120 [1310 :pointer-2-f 0]
rlm@297 121 [1310 :pointer-3-f 0]
rlm@296 122
rlm@297 123 [1310 :middle-1-f 0]
rlm@297 124 [1310 :middle-2-f 0]
rlm@297 125 [1310 :middle-3-f 0]
rlm@297 126
rlm@297 127
rlm@297 128
rlm@297 129 ])
rlm@297 130
rlm@297 131 (defn gen-summon-ball []
rlm@297 132 (let [wait (atom 20)]
rlm@297 133 (fn [world]
rlm@297 134 (if (= 0 (swap! wait dec))
rlm@297 135 (let [brick
rlm@297 136 (box 0.8 0.8 0.8 :mass 0.05
rlm@297 137 :position (Vector3f. 0 0 0.5)
rlm@297 138 :color (ColorRGBA/Red))]
rlm@297 139
rlm@297 140 (.addControl
rlm@297 141 brick
rlm@297 142 (proxy [AbstractControl] []
rlm@297 143 (controlUpdate [tpf]
rlm@297 144 (println-repl (.getWorldTranslation brick)))
rlm@297 145 (controlRender [_ _])))
rlm@297 146 (add-element world brick))))))
rlm@297 147
rlm@296 148
rlm@295 149 (def control-list
rlm@295 150 [
rlm@295 151 0 ;;pointer-21 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@296 152 0 ;;pointer-21 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 153 0 ;;thumb-11 #<Vector3f (-0.8802276, -0.39781287, -0.25873658)>
rlm@295 154 0 ;;thumb-11 #<Vector3f (0.8485723, 0.46149826, 0.2587364)>
rlm@295 155 0 ;;pointer-11 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 156 0 ;;pointer-11 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 157 0 ;;thumb-2.0011 #<Vector3f (-0.71705645, -0.44753736, -0.5343599)>
rlm@295 158 0 ;;thumb-2.0011 #<Vector3f (-0.10567085, 0.83862597, 0.53435963)>
rlm@295 159 0 ;;middle-11 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 160 0 ;;middle-11 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@296 161 1 ;;pointer-31 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 162 0 ;;pointer-31 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 163 0 ;;middle-21 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 164 0 ;;middle-21 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 165 0 ;;middle-31 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 166 0 ;;middle-31 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 167 0 ;;pinky-21 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 168 0 ;;pinky-21 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@296 169 0 ;;pinky-31 #<Vector3f (-1, 0.0, 0.0)>
rlm@296 170 0 ;;pinky-31 #<Vector3f (1.0, 0.0, 0.0)>
rlm@295 171 0 ;;ring-31 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 172 0 ;;ring-31 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 173 0 ;;ring-21 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 174 0 ;;ring-21 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 175 0 ;;ring-11 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 176 0 ;;ring-11 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 177 0 ;;thumb-11 #<Vector3f (-0.43154645, 0.7302033, 0.5296894)>
rlm@295 178 0 ;;thumb-11 #<Vector3f (-0.8032993, -0.2722854, -0.5296895)>
rlm@295 179 0 ;;pinky-11 #<Vector3f (-0.99999994, 0.0, 0.0)>
rlm@295 180 0 ;;pinky-11 #<Vector3f (0.99999994, 0.0, 0.0)>
rlm@295 181 ])
rlm@295 182
rlm@281 183 (defn test-everything!
rlm@281 184 ([] (test-everything! false))
rlm@281 185 ([record?]
rlm@281 186 (let [me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
rlm@281 187
rlm@192 188 bell (AudioNode. (asset-manager)
rlm@192 189 "Sounds/pure.wav" false)
rlm@281 190 creature (doto (load-blender-model hand)
rlm@281 191 (body!))
rlm@297 192
rlm@297 193 summon-ball (gen-summon-ball)
rlm@192 194 ;;;;;;;;;;;; Sensors/Effectors ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@294 195 ;;touch (touch! creature)
rlm@294 196 ;;touch-display (view-touch)
rlm@189 197
rlm@294 198 ;;vision (vision! creature)
rlm@294 199 ;;vision-display (view-vision)
rlm@189 200
rlm@294 201 ;;hearing (hearing! creature)
rlm@294 202 ;;hearing-display (view-hearing)
rlm@189 203
rlm@173 204 prop (proprioception! creature)
rlm@190 205 prop-display (view-proprioception)
rlm@148 206
rlm@296 207 control-script (motor-control-program
rlm@296 208 muscle-positions wiggle-each-finger-tip)
rlm@191 209 muscles (movement! creature)
rlm@281 210 muscle-display (view-movement)
rlm@281 211 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@189 212
rlm@281 213 fix-display (gen-fix-display)]
rlm@285 214 (world
rlm@285 215 (nodify [creature
rlm@285 216 (box 10 2 10 :position (Vector3f. 0 -9 0)
rlm@285 217 :color ColorRGBA/Gray :mass 0)
rlm@285 218 me])
rlm@285 219 (merge standard-debug-controls
rlm@285 220 {"key-return"
rlm@285 221 (fn [_ value]
rlm@285 222 (if value
rlm@296 223 (.play bell)))})
rlm@296 224
rlm@285 225 (fn [world]
rlm@285 226 (.setTimer world (RatchetTimer. 60))
rlm@297 227 (position-camera
rlm@297 228 world (Vector3f. 7.7101235, 6.3628435, -14.15047)
rlm@297 229 (Quaternion. 0.3644328, -0.2558489, 0.104854375, 0.8892332))
rlm@285 230 (light-up-everything world)
rlm@285 231 (enable-debug world)
rlm@285 232 (add-camera! world
rlm@285 233 (add-eye! creature
rlm@285 234 (.getChild
rlm@285 235 (.getChild creature "eyes") "eye"))
rlm@285 236 (comp (view-image) BufferedImage!))
rlm@297 237 (speed-up world)
rlm@297 238 )
rlm@285 239 (fn [world tpf]
rlm@294 240 ;;(prop-display (prop))
rlm@294 241 ;;(touch-display (map #(% (.getRootNode world)) touch))
rlm@294 242 ;;(vision-display (map #(% world) vision))
rlm@294 243 ;;(hearing-display (map #(% world) hearing))
rlm@297 244 (summon-ball world)
rlm@296 245 ;;(muscle-display
rlm@296 246 (control-script muscles)
rlm@296 247 ;;)
rlm@295 248
rlm@285 249 (.setLocalTranslation me (.getLocation (.getCamera world)))
rlm@285 250 (fix-display world))))))
rlm@87 251 #+end_src
rlm@83 252
rlm@282 253 #+results: integration
rlm@282 254 : #'cortex.integration/test-everything!
rlm@282 255
rlm@78 256 * COMMENT purgatory
rlm@78 257 #+begin_src clojure
rlm@77 258 (defn bullet-trans* []
rlm@77 259 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
rlm@77 260 :position (Vector3f. 5 0 0)
rlm@77 261 :mass 90)
rlm@77 262 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 263 :position (Vector3f. -5 0 0)
rlm@77 264 :mass 0)
rlm@77 265 control-a (.getControl obj-a RigidBodyControl)
rlm@77 266 control-b (.getControl obj-b RigidBodyControl)
rlm@77 267 move-up? (atom nil)
rlm@77 268 move-down? (atom nil)
rlm@77 269 move-left? (atom nil)
rlm@77 270 move-right? (atom nil)
rlm@77 271 roll-left? (atom nil)
rlm@77 272 roll-right? (atom nil)
rlm@77 273 force 100
rlm@77 274 swivel
rlm@77 275 (.toRotationMatrix
rlm@77 276 (doto (Quaternion.)
rlm@77 277 (.fromAngleAxis (/ Math/PI 2)
rlm@77 278 Vector3f/UNIT_X)))
rlm@77 279 x-move
rlm@77 280 (doto (Matrix3f.)
rlm@77 281 (.fromStartEndVectors Vector3f/UNIT_X
rlm@77 282 (.normalize (Vector3f. 1 1 0))))
rlm@77 283
rlm@77 284 timer (atom 0)]
rlm@77 285 (doto
rlm@77 286 (ConeJoint.
rlm@77 287 control-a control-b
rlm@77 288 (Vector3f. -8 0 0)
rlm@77 289 (Vector3f. 2 0 0)
rlm@77 290 ;;swivel swivel
rlm@77 291 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
rlm@77 292 x-move Matrix3f/IDENTITY
rlm@77 293 )
rlm@77 294 (.setCollisionBetweenLinkedBodys false)
rlm@77 295 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
rlm@77 296 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
rlm@77 297 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
rlm@77 298 (world (nodify
rlm@77 299 [obj-a obj-b])
rlm@77 300 (merge standard-debug-controls
rlm@77 301 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@77 302 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@77 303 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@77 304 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@77 305 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@77 306 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@77 307
rlm@77 308 (fn [world]
rlm@77 309 (enable-debug world)
rlm@77 310 (set-gravity world Vector3f/ZERO)
rlm@77 311 )
rlm@77 312
rlm@77 313 (fn [world _]
rlm@77 314
rlm@77 315 (if @move-up?
rlm@77 316 (.applyForce control-a
rlm@77 317 (Vector3f. force 0 0)
rlm@77 318 (Vector3f. 0 0 0)))
rlm@77 319 (if @move-down?
rlm@77 320 (.applyForce control-a
rlm@77 321 (Vector3f. (- force) 0 0)
rlm@77 322 (Vector3f. 0 0 0)))
rlm@77 323 (if @move-left?
rlm@77 324 (.applyForce control-a
rlm@77 325 (Vector3f. 0 force 0)
rlm@77 326 (Vector3f. 0 0 0)))
rlm@77 327 (if @move-right?
rlm@77 328 (.applyForce control-a
rlm@77 329 (Vector3f. 0 (- force) 0)
rlm@77 330 (Vector3f. 0 0 0)))
rlm@77 331
rlm@77 332 (if @roll-left?
rlm@77 333 (.applyForce control-a
rlm@77 334 (Vector3f. 0 0 force)
rlm@77 335 (Vector3f. 0 0 0)))
rlm@77 336 (if @roll-right?
rlm@77 337 (.applyForce control-a
rlm@77 338 (Vector3f. 0 0 (- force))
rlm@77 339 (Vector3f. 0 0 0)))
rlm@77 340
rlm@77 341 (if (zero? (rem (swap! timer inc) 100))
rlm@77 342 (.attachChild
rlm@77 343 (.getRootNode world)
rlm@77 344 (sphere 0.05 :color ColorRGBA/Yellow
rlm@77 345 :physical? false :position
rlm@77 346 (.getWorldTranslation obj-a)))))
rlm@77 347 )
rlm@77 348 ))
rlm@77 349
rlm@106 350 (defn test-joint [joint]
rlm@106 351 (let [[origin top bottom floor] (world-setup joint)
rlm@106 352 control (.getControl top RigidBodyControl)
rlm@106 353 move-up? (atom false)
rlm@106 354 move-down? (atom false)
rlm@106 355 move-left? (atom false)
rlm@106 356 move-right? (atom false)
rlm@106 357 roll-left? (atom false)
rlm@106 358 roll-right? (atom false)
rlm@106 359 timer (atom 0)]
rlm@106 360
rlm@106 361 (world
rlm@106 362 (nodify [top bottom floor origin])
rlm@106 363 (merge standard-debug-controls
rlm@106 364 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@106 365 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@106 366 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@106 367 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@106 368 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@106 369 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@106 370
rlm@106 371 (fn [world]
rlm@106 372 (light-up-everything world)
rlm@106 373 (enable-debug world)
rlm@106 374 (set-gravity world (Vector3f. 0 0 0))
rlm@106 375 )
rlm@106 376
rlm@106 377 (fn [world _]
rlm@106 378 (if (zero? (rem (swap! timer inc) 100))
rlm@106 379 (do
rlm@106 380 ;; (println-repl @timer)
rlm@106 381 (.attachChild (.getRootNode world)
rlm@106 382 (sphere 0.05 :color ColorRGBA/Yellow
rlm@106 383 :position (.getWorldTranslation top)
rlm@106 384 :physical? false))
rlm@106 385 (.attachChild (.getRootNode world)
rlm@106 386 (sphere 0.05 :color ColorRGBA/LightGray
rlm@106 387 :position (.getWorldTranslation bottom)
rlm@106 388 :physical? false))))
rlm@106 389
rlm@106 390 (if @move-up?
rlm@106 391 (.applyTorque control
rlm@106 392 (.mult (.getPhysicsRotation control)
rlm@106 393 (Vector3f. 0 0 10))))
rlm@106 394 (if @move-down?
rlm@106 395 (.applyTorque control
rlm@106 396 (.mult (.getPhysicsRotation control)
rlm@106 397 (Vector3f. 0 0 -10))))
rlm@106 398 (if @move-left?
rlm@106 399 (.applyTorque control
rlm@106 400 (.mult (.getPhysicsRotation control)
rlm@106 401 (Vector3f. 0 10 0))))
rlm@106 402 (if @move-right?
rlm@106 403 (.applyTorque control
rlm@106 404 (.mult (.getPhysicsRotation control)
rlm@106 405 (Vector3f. 0 -10 0))))
rlm@106 406 (if @roll-left?
rlm@106 407 (.applyTorque control
rlm@106 408 (.mult (.getPhysicsRotation control)
rlm@106 409 (Vector3f. -1 0 0))))
rlm@106 410 (if @roll-right?
rlm@106 411 (.applyTorque control
rlm@106 412 (.mult (.getPhysicsRotation control)
rlm@106 413 (Vector3f. 1 0 0))))))))
rlm@99 414 #+end_src
rlm@192 415
rlm@99 416
rlm@99 417 * COMMENT generate source
rlm@192 418 #+begin_src clojure :tangle ../src/cortex/integration.clj
rlm@192 419 <<integration>>
rlm@99 420 #+end_src
rlm@99 421
rlm@99 422
rlm@94 423
rlm@94 424