Mercurial > cortex
view org/test-creature.org @ 191:66fbab414d45
added muscle exertion debug view.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 04 Feb 2012 11:02:19 -0700 |
parents | 2902aca33c6e |
children |
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)136 creature (doto (load-blender-model thing) (body!))138 touch (touch! creature)139 touch-display (view-touch)141 vision (vision! creature)142 vision-display (view-vision)144 hearing (hearing! creature)145 hearing-display (view-hearing)147 prop (proprioception! creature)148 prop-display (view-proprioception)150 muscle-exertion (atom 0)151 muscles (movement! creature)152 muscle-display (view-movement)154 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)155 bell (AudioNode. (asset-manager)156 "Sounds/pure.wav" false)158 fix-display (runonce159 (fn [world] (add-camera! world (.getCamera world) no-op)))160 ]163 (apply164 world165 (with-movement166 (.getChild creature "worm-21")167 ["key-r" "key-t"168 "key-f" "key-g"169 "key-v" "key-b"]170 [10 10 10 10 1 1]171 [(nodify [creature172 (box 10 2 10 :position (Vector3f. 0 -9 0)173 :color ColorRGBA/Gray :mass 0)174 x-axis y-axis z-axis175 me176 ])177 (merge standard-debug-controls178 {"key-return"179 (fn [_ value]180 (if value181 (do182 (println-repl "play-sound")183 (.play bell))))184 "key-h"185 (fn [_ value]186 (if value187 (swap! muscle-exertion (partial + 20))))188 "key-n"189 (fn [_ value]190 (if value191 (swap! muscle-exertion (fn [v] (- v 20)))))193 })194 (fn [world]195 (light-up-everything world)196 (enable-debug world)197 ;;(dorun (map #(% world) init-vision-fns))198 ;;(dorun (map #(% world) init-hearing-fns))200 (add-camera! world201 (add-eye! creature (test-eye))202 (comp (view-image) BufferedImage!))205 ;;(set-gravity world (Vector3f. 0 0 0))206 ;;(com.aurellem.capture.Capture/captureVideo207 ;; world (file-str "/home/r/proj/ai-videos/hand"))208 ;;(.setTimer world (RatchetTimer. 60))209 (speed-up world)210 (set-gravity world (Vector3f. 0 0 0))211 )212 (fn [world tpf]213 ;;(dorun214 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))216 (prop-display (prop))218 (touch-display (map #(% (.getRootNode world)) touch))220 (vision-display (map #(% world) vision))222 (hearing-display (map #(% world) hearing))224 (muscle-display (map #(% @muscle-exertion) muscles))226 ;;(println-repl (vision-data))227 (.setLocalTranslation me (.getLocation (.getCamera world)))228 (fix-display world)230 )]231 ;;(let [timer (atom 0)]232 ;; (fn [_ _]233 ;; (swap! timer inc)234 ;; (if (= (rem @timer 60) 0)235 ;; (println-repl (float (/ @timer 60))))))236 ))))240 ;; the camera will stay in its initial position/rotation with relation241 ;; to the spatial.244 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli,245 ;;anchor-qualia, augment-organ, with-organ247 (defn follow-test248 "show a camera that stays in the same relative position to a blue cube."249 []250 (let [camera-pos (Vector3f. 0 30 0)251 rock (box 1 1 1 :color ColorRGBA/Blue252 :position (Vector3f. 0 10 0)253 :mass 30254 )255 rot (.getWorldRotation rock)257 table (box 3 1 10 :color ColorRGBA/Gray :mass 0258 :position (Vector3f. 0 -3 0))]260 (world261 (nodify [rock table])262 standard-debug-controls263 (fn [world]264 (let265 [cam (doto (.clone (.getCamera world))266 (.setLocation camera-pos)267 (.lookAt Vector3f/ZERO268 Vector3f/UNIT_X))]269 (bind-sense rock cam)271 (.setTimer world (RatchetTimer. 60))272 (add-camera! world cam (comp (view-image) BufferedImage!))273 (add-camera! world (.getCamera world) no-op))274 )275 (fn [_ _] (println-repl rot)))))279 #+end_src281 #+results: body-1282 : #'cortex.silly/follow-test285 * COMMENT purgatory286 #+begin_src clojure288 (defn bullet-trans* []289 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red290 :position (Vector3f. 5 0 0)291 :mass 90)292 obj-b (sphere 0.5 :color ColorRGBA/Blue293 :position (Vector3f. -5 0 0)294 :mass 0)295 control-a (.getControl obj-a RigidBodyControl)296 control-b (.getControl obj-b RigidBodyControl)297 move-up? (atom nil)298 move-down? (atom nil)299 move-left? (atom nil)300 move-right? (atom nil)301 roll-left? (atom nil)302 roll-right? (atom nil)303 force 100304 swivel305 (.toRotationMatrix306 (doto (Quaternion.)307 (.fromAngleAxis (/ Math/PI 2)308 Vector3f/UNIT_X)))309 x-move310 (doto (Matrix3f.)311 (.fromStartEndVectors Vector3f/UNIT_X312 (.normalize (Vector3f. 1 1 0))))314 timer (atom 0)]315 (doto316 (ConeJoint.317 control-a control-b318 (Vector3f. -8 0 0)319 (Vector3f. 2 0 0)320 ;;swivel swivel321 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY322 x-move Matrix3f/IDENTITY323 )324 (.setCollisionBetweenLinkedBodys false)325 (.setLimit (* 1 (/ Math/PI 4)) ;; twist326 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane327 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane328 (world (nodify329 [obj-a obj-b])330 (merge standard-debug-controls331 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))332 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))333 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))334 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))335 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))336 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})338 (fn [world]339 (enable-debug world)340 (set-gravity world Vector3f/ZERO)341 )343 (fn [world _]345 (if @move-up?346 (.applyForce control-a347 (Vector3f. force 0 0)348 (Vector3f. 0 0 0)))349 (if @move-down?350 (.applyForce control-a351 (Vector3f. (- force) 0 0)352 (Vector3f. 0 0 0)))353 (if @move-left?354 (.applyForce control-a355 (Vector3f. 0 force 0)356 (Vector3f. 0 0 0)))357 (if @move-right?358 (.applyForce control-a359 (Vector3f. 0 (- force) 0)360 (Vector3f. 0 0 0)))362 (if @roll-left?363 (.applyForce control-a364 (Vector3f. 0 0 force)365 (Vector3f. 0 0 0)))366 (if @roll-right?367 (.applyForce control-a368 (Vector3f. 0 0 (- force))369 (Vector3f. 0 0 0)))371 (if (zero? (rem (swap! timer inc) 100))372 (.attachChild373 (.getRootNode world)374 (sphere 0.05 :color ColorRGBA/Yellow375 :physical? false :position376 (.getWorldTranslation obj-a)))))377 )378 ))380 (defn test-joint [joint]381 (let [[origin top bottom floor] (world-setup joint)382 control (.getControl top RigidBodyControl)383 move-up? (atom false)384 move-down? (atom false)385 move-left? (atom false)386 move-right? (atom false)387 roll-left? (atom false)388 roll-right? (atom false)389 timer (atom 0)]391 (world392 (nodify [top bottom floor origin])393 (merge standard-debug-controls394 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))395 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))396 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))397 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))398 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))399 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})401 (fn [world]402 (light-up-everything world)403 (enable-debug world)404 (set-gravity world (Vector3f. 0 0 0))405 )407 (fn [world _]408 (if (zero? (rem (swap! timer inc) 100))409 (do410 ;; (println-repl @timer)411 (.attachChild (.getRootNode world)412 (sphere 0.05 :color ColorRGBA/Yellow413 :position (.getWorldTranslation top)414 :physical? false))415 (.attachChild (.getRootNode world)416 (sphere 0.05 :color ColorRGBA/LightGray417 :position (.getWorldTranslation bottom)418 :physical? false))))420 (if @move-up?421 (.applyTorque control422 (.mult (.getPhysicsRotation control)423 (Vector3f. 0 0 10))))424 (if @move-down?425 (.applyTorque control426 (.mult (.getPhysicsRotation control)427 (Vector3f. 0 0 -10))))428 (if @move-left?429 (.applyTorque control430 (.mult (.getPhysicsRotation control)431 (Vector3f. 0 10 0))))432 (if @move-right?433 (.applyTorque control434 (.mult (.getPhysicsRotation control)435 (Vector3f. 0 -10 0))))436 (if @roll-left?437 (.applyTorque control438 (.mult (.getPhysicsRotation control)439 (Vector3f. -1 0 0))))440 (if @roll-right?441 (.applyTorque control442 (.mult (.getPhysicsRotation control)443 (Vector3f. 1 0 0))))))))444 #+end_src447 * COMMENT generate source448 #+begin_src clojure :tangle ../src/cortex/silly.clj449 <<body-1>>450 #+end_src