Mercurial > cortex
view 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 |
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 style113 (defn debug-vision-window114 "creates function that offers a debug view of sensor data"115 []116 (let [vi (view-image)]117 (fn118 [[coords sensor-data]]119 (let [image (points->image coords)]120 (dorun121 (for [i (range (count coords))]122 (.setRGB image ((coords i) 0) ((coords i) 1)123 (sensor-data i))))124 (vi image)))))126 (defn debug-hearing-window127 "view audio data"128 [height]129 (let [vi (view-image)]130 (fn [[coords sensor-data]]131 (let [image (BufferedImage. (count coords) height132 BufferedImage/TYPE_INT_RGB)]133 (dorun134 (for [x (range (count coords))]135 (dorun136 (for [y (range height)]137 (let [raw-sensor (sensor-data x)]138 (.setRGB image x y (gray-scale raw-sensor)))))))140 (vi image)))))142 (defn test-creature [thing]143 (let [x-axis144 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)145 y-axis146 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)147 z-axis148 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)149 creature (doto150 (load-blender-model thing)151 (body!))152 touch (touch! creature)153 touch-view (view-touch)154 vision-data (vision! creature)155 vision-debug (map (fn [_] (debug-vision-window)) vision-data)156 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)157 hearing-senses (hearing! creature)158 hearing-windows (map (fn [_] (debug-hearing-window 50))159 hearing-senses)160 bell (AudioNode. (asset-manager)161 "Sounds/pure.wav" false)162 prop (proprioception! creature)163 prop-debug (proprioception-debug-window)165 muscle-fns (movement! creature)166 ;; dream167 fix-display (runonce168 (fn [world] (add-camera! world (.getCamera world) no-op)))169 ]172 (apply173 world174 (with-movement175 (.getChild creature "worm-21")176 ["key-r" "key-t"177 "key-f" "key-g"178 "key-v" "key-b"]179 [10 10 10 10 1 1]180 [(nodify [creature181 (box 10 2 10 :position (Vector3f. 0 -9 0)182 :color ColorRGBA/Gray :mass 0)183 x-axis y-axis z-axis184 me185 ])186 (merge standard-debug-controls187 {"key-return"188 (fn [_ value]189 (if value190 (do191 (println-repl "play-sound")192 (.play bell))))193 "key-h"194 (fn [_ value]195 (if value196 (do197 (println-repl "muscle activating!")198 ((first muscle-fns) 199))))200 })201 (fn [world]202 (light-up-everything world)203 (enable-debug world)204 ;;(dorun (map #(% world) init-vision-fns))205 ;;(dorun (map #(% world) init-hearing-fns))207 (add-camera! world208 (add-eye! creature (test-eye))209 (comp (view-image) BufferedImage!))212 ;;(set-gravity world (Vector3f. 0 0 0))213 ;;(com.aurellem.capture.Capture/captureVideo214 ;; world (file-str "/home/r/proj/ai-videos/hand"))215 ;;(.setTimer world (RatchetTimer. 60))216 (speed-up world)217 (set-gravity world (Vector3f. 0 0 0))218 )219 (fn [world tpf]220 ;;(dorun221 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))223 (prop-debug (prop))225 (touch-view (map #(% (.getRootNode world)) touch))227 (dorun228 (map #(%1 (%2 world))229 vision-debug vision-data))230 (dorun231 (map #(%1 (%2 world)) hearing-windows hearing-senses))234 ;;(println-repl (vision-data))235 (.setLocalTranslation me (.getLocation (.getCamera world)))236 (fix-display world)238 )]239 ;;(let [timer (atom 0)]240 ;; (fn [_ _]241 ;; (swap! timer inc)242 ;; (if (= (rem @timer 60) 0)243 ;; (println-repl (float (/ @timer 60))))))244 ))))248 ;; the camera will stay in its initial position/rotation with relation249 ;; to the spatial.252 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli,253 ;;anchor-qualia, augment-organ, with-organ255 (defn follow-test256 "show a camera that stays in the same relative position to a blue cube."257 []258 (let [camera-pos (Vector3f. 0 30 0)259 rock (box 1 1 1 :color ColorRGBA/Blue260 :position (Vector3f. 0 10 0)261 :mass 30262 )263 rot (.getWorldRotation rock)265 table (box 3 1 10 :color ColorRGBA/Gray :mass 0266 :position (Vector3f. 0 -3 0))]268 (world269 (nodify [rock table])270 standard-debug-controls271 (fn [world]272 (let273 [cam (doto (.clone (.getCamera world))274 (.setLocation camera-pos)275 (.lookAt Vector3f/ZERO276 Vector3f/UNIT_X))]277 (bind-sense rock cam)279 (.setTimer world (RatchetTimer. 60))280 (add-camera! world cam (comp (view-image) BufferedImage!))281 (add-camera! world (.getCamera world) no-op))282 )283 (fn [_ _] (println-repl rot)))))287 #+end_src289 #+results: body-1290 : #'cortex.silly/follow-test293 * COMMENT purgatory294 #+begin_src clojure296 (defn bullet-trans* []297 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red298 :position (Vector3f. 5 0 0)299 :mass 90)300 obj-b (sphere 0.5 :color ColorRGBA/Blue301 :position (Vector3f. -5 0 0)302 :mass 0)303 control-a (.getControl obj-a RigidBodyControl)304 control-b (.getControl obj-b RigidBodyControl)305 move-up? (atom nil)306 move-down? (atom nil)307 move-left? (atom nil)308 move-right? (atom nil)309 roll-left? (atom nil)310 roll-right? (atom nil)311 force 100312 swivel313 (.toRotationMatrix314 (doto (Quaternion.)315 (.fromAngleAxis (/ Math/PI 2)316 Vector3f/UNIT_X)))317 x-move318 (doto (Matrix3f.)319 (.fromStartEndVectors Vector3f/UNIT_X320 (.normalize (Vector3f. 1 1 0))))322 timer (atom 0)]323 (doto324 (ConeJoint.325 control-a control-b326 (Vector3f. -8 0 0)327 (Vector3f. 2 0 0)328 ;;swivel swivel329 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY330 x-move Matrix3f/IDENTITY331 )332 (.setCollisionBetweenLinkedBodys false)333 (.setLimit (* 1 (/ Math/PI 4)) ;; twist334 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane335 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane336 (world (nodify337 [obj-a obj-b])338 (merge standard-debug-controls339 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))340 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))341 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))342 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))343 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))344 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})346 (fn [world]347 (enable-debug world)348 (set-gravity world Vector3f/ZERO)349 )351 (fn [world _]353 (if @move-up?354 (.applyForce control-a355 (Vector3f. force 0 0)356 (Vector3f. 0 0 0)))357 (if @move-down?358 (.applyForce control-a359 (Vector3f. (- force) 0 0)360 (Vector3f. 0 0 0)))361 (if @move-left?362 (.applyForce control-a363 (Vector3f. 0 force 0)364 (Vector3f. 0 0 0)))365 (if @move-right?366 (.applyForce control-a367 (Vector3f. 0 (- force) 0)368 (Vector3f. 0 0 0)))370 (if @roll-left?371 (.applyForce control-a372 (Vector3f. 0 0 force)373 (Vector3f. 0 0 0)))374 (if @roll-right?375 (.applyForce control-a376 (Vector3f. 0 0 (- force))377 (Vector3f. 0 0 0)))379 (if (zero? (rem (swap! timer inc) 100))380 (.attachChild381 (.getRootNode world)382 (sphere 0.05 :color ColorRGBA/Yellow383 :physical? false :position384 (.getWorldTranslation obj-a)))))385 )386 ))388 (defn test-joint [joint]389 (let [[origin top bottom floor] (world-setup joint)390 control (.getControl top RigidBodyControl)391 move-up? (atom false)392 move-down? (atom false)393 move-left? (atom false)394 move-right? (atom false)395 roll-left? (atom false)396 roll-right? (atom false)397 timer (atom 0)]399 (world400 (nodify [top bottom floor origin])401 (merge standard-debug-controls402 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))403 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))404 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))405 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))406 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))407 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})409 (fn [world]410 (light-up-everything world)411 (enable-debug world)412 (set-gravity world (Vector3f. 0 0 0))413 )415 (fn [world _]416 (if (zero? (rem (swap! timer inc) 100))417 (do418 ;; (println-repl @timer)419 (.attachChild (.getRootNode world)420 (sphere 0.05 :color ColorRGBA/Yellow421 :position (.getWorldTranslation top)422 :physical? false))423 (.attachChild (.getRootNode world)424 (sphere 0.05 :color ColorRGBA/LightGray425 :position (.getWorldTranslation bottom)426 :physical? false))))428 (if @move-up?429 (.applyTorque control430 (.mult (.getPhysicsRotation control)431 (Vector3f. 0 0 10))))432 (if @move-down?433 (.applyTorque control434 (.mult (.getPhysicsRotation control)435 (Vector3f. 0 0 -10))))436 (if @move-left?437 (.applyTorque control438 (.mult (.getPhysicsRotation control)439 (Vector3f. 0 10 0))))440 (if @move-right?441 (.applyTorque control442 (.mult (.getPhysicsRotation control)443 (Vector3f. 0 -10 0))))444 (if @roll-left?445 (.applyTorque control446 (.mult (.getPhysicsRotation control)447 (Vector3f. -1 0 0))))448 (if @roll-right?449 (.applyTorque control450 (.mult (.getPhysicsRotation control)451 (Vector3f. 1 0 0))))))))452 #+end_src455 * COMMENT generate source456 #+begin_src clojure :tangle ../src/cortex/silly.clj457 <<body-1>>458 #+end_src