Mercurial > cortex
view org/test-creature.org @ 188:22548d48cc85
added debug for vision
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 04 Feb 2012 10:00:37 -0700 |
parents | cfb71209ddc6 |
children | facc2ef3fe5c |
line wrap: on
line source
1 #+title: First attempt at a creature!2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description:5 #+keywords: simulation, jMonkeyEngine3, clojure6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org12 * Intro13 So far, I've made the following senses --14 - Vision15 - Hearing16 - Touch17 - Proprioception19 And one effector:20 - Movement22 However, the code so far has only enabled these senses, but has not23 actually implemented them. For example, there is still a lot of work24 to be done for vision. I need to be able to create an /eyeball/ in25 simulation that can be moved around and see the world from different26 angles. I also need to determine weather to use log-polar or cartesian27 for the visual input, and I need to determine how/wether to28 disceritise the visual input.30 I also want to be able to visualize both the sensors and the31 effectors in pretty pictures. This semi-retarted creature will be my32 first attempt at bringing everything together.34 * The creature's body36 Still going to do an eve-like body in blender, but due to problems37 importing the joints, etc into jMonkeyEngine3, I'm going to do all38 the connecting here in clojure code, using the names of the individual39 components and trial and error. Later, I'll maybe make some sort of40 creature-building modifications to blender that support whatever41 discritized senses I'm going to make.43 #+name: body-144 #+begin_src clojure45 (ns cortex.silly46 "let's play!"47 {:author "Robert McIntyre"})49 ;; TODO remove this!50 (require 'cortex.import)51 (cortex.import/mega-import-jme3)52 (use '(cortex world util body hearing touch vision sense53 proprioception movement))55 (rlm.rlm-commands/help)56 (import java.awt.image.BufferedImage)57 (import javax.swing.JPanel)58 (import javax.swing.SwingUtilities)59 (import java.awt.Dimension)60 (import javax.swing.JFrame)61 (import java.awt.Dimension)62 (import com.aurellem.capture.RatchetTimer)64 (use 'clojure.contrib.def)66 (defn load-blender-model67 "Load a .blend file using an asset folder relative path."68 [^String model]69 (.loadModel70 (doto (asset-manager)71 (.registerLoader BlenderModelLoader (into-array String ["blend"])))72 model))74 (def hand "Models/creature1/one.blend")76 (def worm "Models/creature1/try-again.blend")78 (defn worm-model [] (load-blender-model worm))80 (defn x-ray [#^ColorRGBA color]81 (doto (Material. (asset-manager)82 "Common/MatDefs/Misc/Unshaded.j3md")83 (.setColor "Color" color)84 (-> (.getAdditionalRenderState)85 (.setDepthTest false))))87 (defn colorful []88 (.getChild (worm-model) "worm-21"))90 (import jme3tools.converters.ImageToAwt)92 (import ij.ImagePlus)96 (defn test-eye []97 (.getChild98 (.getChild (worm-model) "eyes")99 "eye"))103 ;; lower level --- nodes104 ;; closest-node "parse/compile-x" -> makes organ, which is spatial, fn pair106 ;; higher level -- organs107 ;;109 ;; higher level --- sense/effector110 ;; these are the functions that provide world i/o, chinese-room style112 (defn debug-hearing-window113 "view audio data"114 [height]115 (let [vi (view-image)]116 (fn [[coords sensor-data]]117 (let [image (BufferedImage. (count coords) height118 BufferedImage/TYPE_INT_RGB)]119 (dorun120 (for [x (range (count coords))]121 (dorun122 (for [y (range height)]123 (let [raw-sensor (sensor-data x)]124 (.setRGB image x y (gray raw-sensor)))))))126 (vi image)))))128 (defn test-creature [thing]129 (let [x-axis130 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)131 y-axis132 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)133 z-axis134 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)135 creature (doto136 (load-blender-model thing)137 (body!))138 touch (touch! creature)139 touch-display (view-touch)140 vision (vision! creature)141 vision-display (view-vision)142 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)143 hearing-senses (hearing! creature)144 hearing-windows (map (fn [_] (debug-hearing-window 50))145 hearing-senses)146 bell (AudioNode. (asset-manager)147 "Sounds/pure.wav" false)148 prop (proprioception! creature)149 prop-debug (proprioception-debug-window)151 muscle-fns (movement! creature)152 ;; dream153 fix-display (runonce154 (fn [world] (add-camera! world (.getCamera world) no-op)))155 ]158 (apply159 world160 (with-movement161 (.getChild creature "worm-21")162 ["key-r" "key-t"163 "key-f" "key-g"164 "key-v" "key-b"]165 [10 10 10 10 1 1]166 [(nodify [creature167 (box 10 2 10 :position (Vector3f. 0 -9 0)168 :color ColorRGBA/Gray :mass 0)169 x-axis y-axis z-axis170 me171 ])172 (merge standard-debug-controls173 {"key-return"174 (fn [_ value]175 (if value176 (do177 (println-repl "play-sound")178 (.play bell))))179 "key-h"180 (fn [_ value]181 (if value182 (do183 (println-repl "muscle activating!")184 ((first muscle-fns) 199))))186 })187 (fn [world]188 (light-up-everything world)189 (enable-debug world)190 ;;(dorun (map #(% world) init-vision-fns))191 ;;(dorun (map #(% world) init-hearing-fns))193 (add-camera! world194 (add-eye! creature (test-eye))195 (comp (view-image) BufferedImage!))198 ;;(set-gravity world (Vector3f. 0 0 0))199 ;;(com.aurellem.capture.Capture/captureVideo200 ;; world (file-str "/home/r/proj/ai-videos/hand"))201 ;;(.setTimer world (RatchetTimer. 60))202 (speed-up world)203 (set-gravity world (Vector3f. 0 0 0))204 )205 (fn [world tpf]206 ;;(dorun207 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))209 (prop-debug (prop))211 (touch-display (map #(% (.getRootNode world)) touch))213 (vision-display (map #(% world) vision))215 (dorun216 (map #(%1 (%2 world)) hearing-windows hearing-senses))219 ;;(println-repl (vision-data))220 (.setLocalTranslation me (.getLocation (.getCamera world)))221 (fix-display world)223 )]224 ;;(let [timer (atom 0)]225 ;; (fn [_ _]226 ;; (swap! timer inc)227 ;; (if (= (rem @timer 60) 0)228 ;; (println-repl (float (/ @timer 60))))))229 ))))233 ;; the camera will stay in its initial position/rotation with relation234 ;; to the spatial.237 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli,238 ;;anchor-qualia, augment-organ, with-organ240 (defn follow-test241 "show a camera that stays in the same relative position to a blue cube."242 []243 (let [camera-pos (Vector3f. 0 30 0)244 rock (box 1 1 1 :color ColorRGBA/Blue245 :position (Vector3f. 0 10 0)246 :mass 30247 )248 rot (.getWorldRotation rock)250 table (box 3 1 10 :color ColorRGBA/Gray :mass 0251 :position (Vector3f. 0 -3 0))]253 (world254 (nodify [rock table])255 standard-debug-controls256 (fn [world]257 (let258 [cam (doto (.clone (.getCamera world))259 (.setLocation camera-pos)260 (.lookAt Vector3f/ZERO261 Vector3f/UNIT_X))]262 (bind-sense rock cam)264 (.setTimer world (RatchetTimer. 60))265 (add-camera! world cam (comp (view-image) BufferedImage!))266 (add-camera! world (.getCamera world) no-op))267 )268 (fn [_ _] (println-repl rot)))))272 #+end_src274 #+results: body-1275 : #'cortex.silly/follow-test278 * COMMENT purgatory279 #+begin_src clojure281 (defn bullet-trans* []282 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red283 :position (Vector3f. 5 0 0)284 :mass 90)285 obj-b (sphere 0.5 :color ColorRGBA/Blue286 :position (Vector3f. -5 0 0)287 :mass 0)288 control-a (.getControl obj-a RigidBodyControl)289 control-b (.getControl obj-b RigidBodyControl)290 move-up? (atom nil)291 move-down? (atom nil)292 move-left? (atom nil)293 move-right? (atom nil)294 roll-left? (atom nil)295 roll-right? (atom nil)296 force 100297 swivel298 (.toRotationMatrix299 (doto (Quaternion.)300 (.fromAngleAxis (/ Math/PI 2)301 Vector3f/UNIT_X)))302 x-move303 (doto (Matrix3f.)304 (.fromStartEndVectors Vector3f/UNIT_X305 (.normalize (Vector3f. 1 1 0))))307 timer (atom 0)]308 (doto309 (ConeJoint.310 control-a control-b311 (Vector3f. -8 0 0)312 (Vector3f. 2 0 0)313 ;;swivel swivel314 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY315 x-move Matrix3f/IDENTITY316 )317 (.setCollisionBetweenLinkedBodys false)318 (.setLimit (* 1 (/ Math/PI 4)) ;; twist319 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane320 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane321 (world (nodify322 [obj-a obj-b])323 (merge standard-debug-controls324 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))325 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))326 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))327 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))328 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))329 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})331 (fn [world]332 (enable-debug world)333 (set-gravity world Vector3f/ZERO)334 )336 (fn [world _]338 (if @move-up?339 (.applyForce control-a340 (Vector3f. force 0 0)341 (Vector3f. 0 0 0)))342 (if @move-down?343 (.applyForce control-a344 (Vector3f. (- force) 0 0)345 (Vector3f. 0 0 0)))346 (if @move-left?347 (.applyForce control-a348 (Vector3f. 0 force 0)349 (Vector3f. 0 0 0)))350 (if @move-right?351 (.applyForce control-a352 (Vector3f. 0 (- force) 0)353 (Vector3f. 0 0 0)))355 (if @roll-left?356 (.applyForce control-a357 (Vector3f. 0 0 force)358 (Vector3f. 0 0 0)))359 (if @roll-right?360 (.applyForce control-a361 (Vector3f. 0 0 (- force))362 (Vector3f. 0 0 0)))364 (if (zero? (rem (swap! timer inc) 100))365 (.attachChild366 (.getRootNode world)367 (sphere 0.05 :color ColorRGBA/Yellow368 :physical? false :position369 (.getWorldTranslation obj-a)))))370 )371 ))373 (defn test-joint [joint]374 (let [[origin top bottom floor] (world-setup joint)375 control (.getControl top RigidBodyControl)376 move-up? (atom false)377 move-down? (atom false)378 move-left? (atom false)379 move-right? (atom false)380 roll-left? (atom false)381 roll-right? (atom false)382 timer (atom 0)]384 (world385 (nodify [top bottom floor origin])386 (merge standard-debug-controls387 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))388 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))389 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))390 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))391 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))392 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})394 (fn [world]395 (light-up-everything world)396 (enable-debug world)397 (set-gravity world (Vector3f. 0 0 0))398 )400 (fn [world _]401 (if (zero? (rem (swap! timer inc) 100))402 (do403 ;; (println-repl @timer)404 (.attachChild (.getRootNode world)405 (sphere 0.05 :color ColorRGBA/Yellow406 :position (.getWorldTranslation top)407 :physical? false))408 (.attachChild (.getRootNode world)409 (sphere 0.05 :color ColorRGBA/LightGray410 :position (.getWorldTranslation bottom)411 :physical? false))))413 (if @move-up?414 (.applyTorque control415 (.mult (.getPhysicsRotation control)416 (Vector3f. 0 0 10))))417 (if @move-down?418 (.applyTorque control419 (.mult (.getPhysicsRotation control)420 (Vector3f. 0 0 -10))))421 (if @move-left?422 (.applyTorque control423 (.mult (.getPhysicsRotation control)424 (Vector3f. 0 10 0))))425 (if @move-right?426 (.applyTorque control427 (.mult (.getPhysicsRotation control)428 (Vector3f. 0 -10 0))))429 (if @roll-left?430 (.applyTorque control431 (.mult (.getPhysicsRotation control)432 (Vector3f. -1 0 0))))433 (if @roll-right?434 (.applyTorque control435 (.mult (.getPhysicsRotation control)436 (Vector3f. 1 0 0))))))))437 #+end_src440 * COMMENT generate source441 #+begin_src clojure :tangle ../src/cortex/silly.clj442 <<body-1>>443 #+end_src