annotate org/test-creature.org @ 185:cfb71209ddc6

moved touch-debug view to touch.org
author Robert McIntyre <rlm@mit.edu>
date Sat, 04 Feb 2012 09:33:13 -0700
parents f1b078375484
children 22548d48cc85
rev   line source
rlm@73 1 #+title: First attempt at a creature!
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@129 9
rlm@129 10
rlm@99 11
rlm@73 12 * Intro
rlm@73 13 So far, I've made the following senses --
rlm@73 14 - Vision
rlm@73 15 - Hearing
rlm@73 16 - Touch
rlm@73 17 - Proprioception
rlm@73 18
rlm@73 19 And one effector:
rlm@73 20 - Movement
rlm@73 21
rlm@73 22 However, the code so far has only enabled these senses, but has not
rlm@73 23 actually implemented them. For example, there is still a lot of work
rlm@73 24 to be done for vision. I need to be able to create an /eyeball/ in
rlm@73 25 simulation that can be moved around and see the world from different
rlm@73 26 angles. I also need to determine weather to use log-polar or cartesian
rlm@73 27 for the visual input, and I need to determine how/wether to
rlm@73 28 disceritise the visual input.
rlm@73 29
rlm@73 30 I also want to be able to visualize both the sensors and the
rlm@104 31 effectors in pretty pictures. This semi-retarted creature will be my
rlm@73 32 first attempt at bringing everything together.
rlm@73 33
rlm@73 34 * The creature's body
rlm@73 35
rlm@73 36 Still going to do an eve-like body in blender, but due to problems
rlm@104 37 importing the joints, etc into jMonkeyEngine3, I'm going to do all
rlm@73 38 the connecting here in clojure code, using the names of the individual
rlm@73 39 components and trial and error. Later, I'll maybe make some sort of
rlm@73 40 creature-building modifications to blender that support whatever
rlm@158 41 discritized senses I'm going to make.
rlm@73 42
rlm@73 43 #+name: body-1
rlm@73 44 #+begin_src clojure
rlm@73 45 (ns cortex.silly
rlm@73 46 "let's play!"
rlm@73 47 {:author "Robert McIntyre"})
rlm@73 48
rlm@73 49 ;; TODO remove this!
rlm@73 50 (require 'cortex.import)
rlm@73 51 (cortex.import/mega-import-jme3)
rlm@158 52 (use '(cortex world util body hearing touch vision sense
rlm@162 53 proprioception movement))
rlm@73 54
rlm@73 55 (rlm.rlm-commands/help)
rlm@99 56 (import java.awt.image.BufferedImage)
rlm@99 57 (import javax.swing.JPanel)
rlm@99 58 (import javax.swing.SwingUtilities)
rlm@99 59 (import java.awt.Dimension)
rlm@99 60 (import javax.swing.JFrame)
rlm@99 61 (import java.awt.Dimension)
rlm@106 62 (import com.aurellem.capture.RatchetTimer)
rlm@161 63
rlm@108 64 (use 'clojure.contrib.def)
rlm@73 65
rlm@73 66 (defn load-blender-model
rlm@73 67 "Load a .blend file using an asset folder relative path."
rlm@73 68 [^String model]
rlm@73 69 (.loadModel
rlm@73 70 (doto (asset-manager)
rlm@73 71 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@73 72 model))
rlm@73 73
rlm@78 74 (def hand "Models/creature1/one.blend")
rlm@74 75
rlm@78 76 (def worm "Models/creature1/try-again.blend")
rlm@78 77
rlm@90 78 (defn worm-model [] (load-blender-model worm))
rlm@90 79
rlm@80 80 (defn x-ray [#^ColorRGBA color]
rlm@80 81 (doto (Material. (asset-manager)
rlm@80 82 "Common/MatDefs/Misc/Unshaded.j3md")
rlm@80 83 (.setColor "Color" color)
rlm@80 84 (-> (.getAdditionalRenderState)
rlm@80 85 (.setDepthTest false))))
rlm@80 86
rlm@91 87 (defn colorful []
rlm@91 88 (.getChild (worm-model) "worm-21"))
rlm@90 89
rlm@90 90 (import jme3tools.converters.ImageToAwt)
rlm@90 91
rlm@90 92 (import ij.ImagePlus)
rlm@90 93
rlm@94 94
rlm@109 95
rlm@111 96 (defn test-eye []
rlm@117 97 (.getChild
rlm@117 98 (.getChild (worm-model) "eyes")
rlm@117 99 "eye"))
rlm@111 100
rlm@111 101
rlm@123 102
rlm@128 103 ;; lower level --- nodes
rlm@128 104 ;; closest-node "parse/compile-x" -> makes organ, which is spatial, fn pair
rlm@128 105
rlm@128 106 ;; higher level -- organs
rlm@128 107 ;;
rlm@128 108
rlm@128 109 ;; higher level --- sense/effector
rlm@128 110 ;; these are the functions that provide world i/o, chinese-room style
rlm@128 111
rlm@128 112
rlm@118 113 (defn debug-vision-window
rlm@118 114 "creates function that offers a debug view of sensor data"
rlm@118 115 []
rlm@118 116 (let [vi (view-image)]
rlm@118 117 (fn
rlm@118 118 [[coords sensor-data]]
rlm@118 119 (let [image (points->image coords)]
rlm@118 120 (dorun
rlm@118 121 (for [i (range (count coords))]
rlm@118 122 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@118 123 (sensor-data i))))
rlm@118 124 (vi image)))))
rlm@118 125
rlm@123 126 (defn debug-hearing-window
rlm@123 127 "view audio data"
rlm@123 128 [height]
rlm@123 129 (let [vi (view-image)]
rlm@123 130 (fn [[coords sensor-data]]
rlm@123 131 (let [image (BufferedImage. (count coords) height
rlm@123 132 BufferedImage/TYPE_INT_RGB)]
rlm@123 133 (dorun
rlm@123 134 (for [x (range (count coords))]
rlm@123 135 (dorun
rlm@123 136 (for [y (range height)]
rlm@123 137 (let [raw-sensor (sensor-data x)]
rlm@126 138 (.setRGB image x y (gray-scale raw-sensor)))))))
rlm@126 139
rlm@123 140 (vi image)))))
rlm@123 141
rlm@106 142 (defn test-creature [thing]
rlm@106 143 (let [x-axis
rlm@106 144 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
rlm@106 145 y-axis
rlm@106 146 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
rlm@106 147 z-axis
rlm@106 148 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
rlm@175 149 creature (doto
rlm@175 150 (load-blender-model thing)
rlm@175 151 (body!))
rlm@185 152 touch (touch! creature)
rlm@185 153 touch-view (view-touch)
rlm@170 154 vision-data (vision! creature)
rlm@121 155 vision-debug (map (fn [_] (debug-vision-window)) vision-data)
rlm@118 156 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
rlm@164 157 hearing-senses (hearing! creature)
rlm@123 158 hearing-windows (map (fn [_] (debug-hearing-window 50))
rlm@123 159 hearing-senses)
rlm@124 160 bell (AudioNode. (asset-manager)
rlm@128 161 "Sounds/pure.wav" false)
rlm@173 162 prop (proprioception! creature)
rlm@135 163 prop-debug (proprioception-debug-window)
rlm@148 164
rlm@180 165 muscle-fns (movement! creature)
rlm@123 166 ;; dream
rlm@170 167 fix-display (runonce
rlm@170 168 (fn [world] (add-camera! world (.getCamera world) no-op)))
rlm@106 169 ]
rlm@143 170
rlm@143 171
rlm@143 172 (apply
rlm@143 173 world
rlm@143 174 (with-movement
rlm@143 175 (.getChild creature "worm-21")
rlm@143 176 ["key-r" "key-t"
rlm@143 177 "key-f" "key-g"
rlm@143 178 "key-v" "key-b"]
rlm@143 179 [10 10 10 10 1 1]
rlm@143 180 [(nodify [creature
rlm@143 181 (box 10 2 10 :position (Vector3f. 0 -9 0)
rlm@143 182 :color ColorRGBA/Gray :mass 0)
rlm@143 183 x-axis y-axis z-axis
rlm@143 184 me
rlm@143 185 ])
rlm@143 186 (merge standard-debug-controls
rlm@143 187 {"key-return"
rlm@143 188 (fn [_ value]
rlm@143 189 (if value
rlm@143 190 (do
rlm@143 191 (println-repl "play-sound")
rlm@148 192 (.play bell))))
rlm@148 193 "key-h"
rlm@148 194 (fn [_ value]
rlm@148 195 (if value
rlm@148 196 (do
rlm@148 197 (println-repl "muscle activating!")
rlm@148 198 ((first muscle-fns) 199))))
rlm@148 199
rlm@148 200 })
rlm@143 201 (fn [world]
rlm@143 202 (light-up-everything world)
rlm@143 203 (enable-debug world)
rlm@170 204 ;;(dorun (map #(% world) init-vision-fns))
rlm@164 205 ;;(dorun (map #(% world) init-hearing-fns))
rlm@143 206
rlm@169 207 (add-camera! world
rlm@169 208 (add-eye! creature (test-eye))
rlm@143 209 (comp (view-image) BufferedImage!))
rlm@143 210
rlm@170 211
rlm@145 212 ;;(set-gravity world (Vector3f. 0 0 0))
rlm@143 213 ;;(com.aurellem.capture.Capture/captureVideo
rlm@143 214 ;; world (file-str "/home/r/proj/ai-videos/hand"))
rlm@143 215 ;;(.setTimer world (RatchetTimer. 60))
rlm@143 216 (speed-up world)
rlm@148 217 (set-gravity world (Vector3f. 0 0 0))
rlm@143 218 )
rlm@143 219 (fn [world tpf]
rlm@143 220 ;;(dorun
rlm@143 221 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
rlm@143 222
rlm@143 223 (prop-debug (prop))
rlm@143 224
rlm@185 225 (touch-view (map #(% (.getRootNode world)) touch))
rlm@143 226
rlm@143 227 (dorun
rlm@170 228 (map #(%1 (%2 world))
rlm@143 229 vision-debug vision-data))
rlm@143 230 (dorun
rlm@164 231 (map #(%1 (%2 world)) hearing-windows hearing-senses))
rlm@143 232
rlm@143 233
rlm@143 234 ;;(println-repl (vision-data))
rlm@143 235 (.setLocalTranslation me (.getLocation (.getCamera world)))
rlm@170 236 (fix-display world)
rlm@143 237
rlm@143 238 )]
rlm@106 239 ;;(let [timer (atom 0)]
rlm@106 240 ;; (fn [_ _]
rlm@106 241 ;; (swap! timer inc)
rlm@106 242 ;; (if (= (rem @timer 60) 0)
rlm@106 243 ;; (println-repl (float (/ @timer 60))))))
rlm@143 244 ))))
rlm@83 245
rlm@109 246
rlm@116 247
rlm@116 248 ;; the camera will stay in its initial position/rotation with relation
rlm@116 249 ;; to the spatial.
rlm@116 250
rlm@116 251
rlm@162 252 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli,
rlm@162 253 ;;anchor-qualia, augment-organ, with-organ
rlm@162 254
rlm@117 255 (defn follow-test
rlm@117 256 "show a camera that stays in the same relative position to a blue cube."
rlm@117 257 []
rlm@116 258 (let [camera-pos (Vector3f. 0 30 0)
rlm@116 259 rock (box 1 1 1 :color ColorRGBA/Blue
rlm@116 260 :position (Vector3f. 0 10 0)
rlm@116 261 :mass 30
rlm@116 262 )
rlm@118 263 rot (.getWorldRotation rock)
rlm@116 264
rlm@116 265 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
rlm@116 266 :position (Vector3f. 0 -3 0))]
rlm@116 267
rlm@116 268 (world
rlm@116 269 (nodify [rock table])
rlm@116 270 standard-debug-controls
rlm@116 271 (fn [world]
rlm@116 272 (let
rlm@116 273 [cam (doto (.clone (.getCamera world))
rlm@116 274 (.setLocation camera-pos)
rlm@116 275 (.lookAt Vector3f/ZERO
rlm@116 276 Vector3f/UNIT_X))]
rlm@123 277 (bind-sense rock cam)
rlm@116 278
rlm@116 279 (.setTimer world (RatchetTimer. 60))
rlm@169 280 (add-camera! world cam (comp (view-image) BufferedImage!))
rlm@169 281 (add-camera! world (.getCamera world) no-op))
rlm@116 282 )
rlm@118 283 (fn [_ _] (println-repl rot)))))
rlm@116 284
rlm@118 285
rlm@123 286
rlm@87 287 #+end_src
rlm@83 288
rlm@87 289 #+results: body-1
rlm@133 290 : #'cortex.silly/follow-test
rlm@78 291
rlm@78 292
rlm@78 293 * COMMENT purgatory
rlm@78 294 #+begin_src clojure
rlm@74 295
rlm@77 296 (defn bullet-trans* []
rlm@77 297 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
rlm@77 298 :position (Vector3f. 5 0 0)
rlm@77 299 :mass 90)
rlm@77 300 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 301 :position (Vector3f. -5 0 0)
rlm@77 302 :mass 0)
rlm@77 303 control-a (.getControl obj-a RigidBodyControl)
rlm@77 304 control-b (.getControl obj-b RigidBodyControl)
rlm@77 305 move-up? (atom nil)
rlm@77 306 move-down? (atom nil)
rlm@77 307 move-left? (atom nil)
rlm@77 308 move-right? (atom nil)
rlm@77 309 roll-left? (atom nil)
rlm@77 310 roll-right? (atom nil)
rlm@77 311 force 100
rlm@77 312 swivel
rlm@77 313 (.toRotationMatrix
rlm@77 314 (doto (Quaternion.)
rlm@77 315 (.fromAngleAxis (/ Math/PI 2)
rlm@77 316 Vector3f/UNIT_X)))
rlm@77 317 x-move
rlm@77 318 (doto (Matrix3f.)
rlm@77 319 (.fromStartEndVectors Vector3f/UNIT_X
rlm@77 320 (.normalize (Vector3f. 1 1 0))))
rlm@77 321
rlm@77 322 timer (atom 0)]
rlm@77 323 (doto
rlm@77 324 (ConeJoint.
rlm@77 325 control-a control-b
rlm@77 326 (Vector3f. -8 0 0)
rlm@77 327 (Vector3f. 2 0 0)
rlm@77 328 ;;swivel swivel
rlm@77 329 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
rlm@77 330 x-move Matrix3f/IDENTITY
rlm@77 331 )
rlm@77 332 (.setCollisionBetweenLinkedBodys false)
rlm@77 333 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
rlm@77 334 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
rlm@77 335 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
rlm@77 336 (world (nodify
rlm@77 337 [obj-a obj-b])
rlm@77 338 (merge standard-debug-controls
rlm@77 339 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@77 340 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@77 341 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@77 342 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@77 343 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@77 344 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@77 345
rlm@77 346 (fn [world]
rlm@77 347 (enable-debug world)
rlm@77 348 (set-gravity world Vector3f/ZERO)
rlm@77 349 )
rlm@77 350
rlm@77 351 (fn [world _]
rlm@77 352
rlm@77 353 (if @move-up?
rlm@77 354 (.applyForce control-a
rlm@77 355 (Vector3f. force 0 0)
rlm@77 356 (Vector3f. 0 0 0)))
rlm@77 357 (if @move-down?
rlm@77 358 (.applyForce control-a
rlm@77 359 (Vector3f. (- force) 0 0)
rlm@77 360 (Vector3f. 0 0 0)))
rlm@77 361 (if @move-left?
rlm@77 362 (.applyForce control-a
rlm@77 363 (Vector3f. 0 force 0)
rlm@77 364 (Vector3f. 0 0 0)))
rlm@77 365 (if @move-right?
rlm@77 366 (.applyForce control-a
rlm@77 367 (Vector3f. 0 (- force) 0)
rlm@77 368 (Vector3f. 0 0 0)))
rlm@77 369
rlm@77 370 (if @roll-left?
rlm@77 371 (.applyForce control-a
rlm@77 372 (Vector3f. 0 0 force)
rlm@77 373 (Vector3f. 0 0 0)))
rlm@77 374 (if @roll-right?
rlm@77 375 (.applyForce control-a
rlm@77 376 (Vector3f. 0 0 (- force))
rlm@77 377 (Vector3f. 0 0 0)))
rlm@77 378
rlm@77 379 (if (zero? (rem (swap! timer inc) 100))
rlm@77 380 (.attachChild
rlm@77 381 (.getRootNode world)
rlm@77 382 (sphere 0.05 :color ColorRGBA/Yellow
rlm@77 383 :physical? false :position
rlm@77 384 (.getWorldTranslation obj-a)))))
rlm@77 385 )
rlm@77 386 ))
rlm@77 387
rlm@106 388 (defn test-joint [joint]
rlm@106 389 (let [[origin top bottom floor] (world-setup joint)
rlm@106 390 control (.getControl top RigidBodyControl)
rlm@106 391 move-up? (atom false)
rlm@106 392 move-down? (atom false)
rlm@106 393 move-left? (atom false)
rlm@106 394 move-right? (atom false)
rlm@106 395 roll-left? (atom false)
rlm@106 396 roll-right? (atom false)
rlm@106 397 timer (atom 0)]
rlm@106 398
rlm@106 399 (world
rlm@106 400 (nodify [top bottom floor origin])
rlm@106 401 (merge standard-debug-controls
rlm@106 402 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@106 403 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@106 404 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@106 405 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@106 406 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@106 407 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@106 408
rlm@106 409 (fn [world]
rlm@106 410 (light-up-everything world)
rlm@106 411 (enable-debug world)
rlm@106 412 (set-gravity world (Vector3f. 0 0 0))
rlm@106 413 )
rlm@106 414
rlm@106 415 (fn [world _]
rlm@106 416 (if (zero? (rem (swap! timer inc) 100))
rlm@106 417 (do
rlm@106 418 ;; (println-repl @timer)
rlm@106 419 (.attachChild (.getRootNode world)
rlm@106 420 (sphere 0.05 :color ColorRGBA/Yellow
rlm@106 421 :position (.getWorldTranslation top)
rlm@106 422 :physical? false))
rlm@106 423 (.attachChild (.getRootNode world)
rlm@106 424 (sphere 0.05 :color ColorRGBA/LightGray
rlm@106 425 :position (.getWorldTranslation bottom)
rlm@106 426 :physical? false))))
rlm@106 427
rlm@106 428 (if @move-up?
rlm@106 429 (.applyTorque control
rlm@106 430 (.mult (.getPhysicsRotation control)
rlm@106 431 (Vector3f. 0 0 10))))
rlm@106 432 (if @move-down?
rlm@106 433 (.applyTorque control
rlm@106 434 (.mult (.getPhysicsRotation control)
rlm@106 435 (Vector3f. 0 0 -10))))
rlm@106 436 (if @move-left?
rlm@106 437 (.applyTorque control
rlm@106 438 (.mult (.getPhysicsRotation control)
rlm@106 439 (Vector3f. 0 10 0))))
rlm@106 440 (if @move-right?
rlm@106 441 (.applyTorque control
rlm@106 442 (.mult (.getPhysicsRotation control)
rlm@106 443 (Vector3f. 0 -10 0))))
rlm@106 444 (if @roll-left?
rlm@106 445 (.applyTorque control
rlm@106 446 (.mult (.getPhysicsRotation control)
rlm@106 447 (Vector3f. -1 0 0))))
rlm@106 448 (if @roll-right?
rlm@106 449 (.applyTorque control
rlm@106 450 (.mult (.getPhysicsRotation control)
rlm@106 451 (Vector3f. 1 0 0))))))))
rlm@99 452 #+end_src
rlm@99 453
rlm@99 454
rlm@99 455 * COMMENT generate source
rlm@99 456 #+begin_src clojure :tangle ../src/cortex/silly.clj
rlm@99 457 <<body-1>>
rlm@99 458 #+end_src
rlm@99 459
rlm@99 460
rlm@94 461
rlm@94 462