Mercurial > cortex
view org/test-creature.org @ 167:9e6a30b8c99a
renamed some functions in eyes.org
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 04 Feb 2012 04:07:25 -0700 |
parents | c33a8e5fe7bc |
children | 94b79c191fc7 |
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 (declare blender-creature)76 (defn blender-creature77 "Return a creature with all joints in place."78 [blender-path]79 (let [model (load-blender-model blender-path)80 joints (creature-joints model)]81 (assemble-creature model joints)))83 (def hand "Models/creature1/one.blend")85 (def worm "Models/creature1/try-again.blend")87 (defn worm-model [] (load-blender-model worm))89 (defn x-ray [#^ColorRGBA color]90 (doto (Material. (asset-manager)91 "Common/MatDefs/Misc/Unshaded.j3md")92 (.setColor "Color" color)93 (-> (.getAdditionalRenderState)94 (.setDepthTest false))))96 (defn colorful []97 (.getChild (worm-model) "worm-21"))99 (import jme3tools.converters.ImageToAwt)101 (import ij.ImagePlus)105 (defn test-eye []106 (.getChild107 (.getChild (worm-model) "eyes")108 "eye"))112 ;; lower level --- nodes113 ;; closest-node "parse/compile-x" -> makes organ, which is spatial, fn pair115 ;; higher level -- organs116 ;;118 ;; higher level --- sense/effector119 ;; these are the functions that provide world i/o, chinese-room style125 (defn gray-scale [num]126 (+ num127 (bit-shift-left num 8)128 (bit-shift-left num 16)))130 (defn debug-touch-window131 "creates function that offers a debug view of sensor data"132 []133 (let [vi (view-image)]134 (fn135 [[coords sensor-data]]136 (let [image (points->image coords)]137 (dorun138 (for [i (range (count coords))]139 (.setRGB image ((coords i) 0) ((coords i) 1)140 (gray-scale (sensor-data i)))))143 (vi image)))))145 (defn debug-vision-window146 "creates function that offers a debug view of sensor data"147 []148 (let [vi (view-image)]149 (fn150 [[coords sensor-data]]151 (let [image (points->image coords)]152 (dorun153 (for [i (range (count coords))]154 (.setRGB image ((coords i) 0) ((coords i) 1)155 (sensor-data i))))156 (vi image)))))158 (defn debug-hearing-window159 "view audio data"160 [height]161 (let [vi (view-image)]162 (fn [[coords sensor-data]]163 (let [image (BufferedImage. (count coords) height164 BufferedImage/TYPE_INT_RGB)]165 (dorun166 (for [x (range (count coords))]167 (dorun168 (for [y (range height)]169 (let [raw-sensor (sensor-data x)]170 (.setRGB image x y (gray-scale raw-sensor)))))))172 (vi image)))))174 (defn test-creature [thing]175 (let [x-axis176 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)177 y-axis178 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)179 z-axis180 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)181 creature (blender-creature thing)182 touch-nerves (touch creature)183 touch-debug-windows (map (fn [_] (debug-touch-window)) touch-nerves)184 [init-vision-fns vision-data] (vision creature)185 vision-debug (map (fn [_] (debug-vision-window)) vision-data)186 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)187 hearing-senses (hearing! creature)188 hearing-windows (map (fn [_] (debug-hearing-window 50))189 hearing-senses)190 bell (AudioNode. (asset-manager)191 "Sounds/pure.wav" false)192 prop (proprioception creature)193 prop-debug (proprioception-debug-window)195 muscle-fns (enable-muscles creature)196 ;; dream198 ]201 (apply202 world203 (with-movement204 (.getChild creature "worm-21")205 ["key-r" "key-t"206 "key-f" "key-g"207 "key-v" "key-b"]208 [10 10 10 10 1 1]209 [(nodify [creature210 (box 10 2 10 :position (Vector3f. 0 -9 0)211 :color ColorRGBA/Gray :mass 0)212 x-axis y-axis z-axis213 me214 ])215 (merge standard-debug-controls216 {"key-return"217 (fn [_ value]218 (if value219 (do220 (println-repl "play-sound")221 (.play bell))))222 "key-h"223 (fn [_ value]224 (if value225 (do226 (println-repl "muscle activating!")227 ((first muscle-fns) 199))))229 })230 (fn [world]231 (light-up-everything world)232 (enable-debug world)233 (dorun (map #(% world) init-vision-fns))234 ;;(dorun (map #(% world) init-hearing-fns))236 (add-eye! world237 (attach-eye creature (test-eye))238 (comp (view-image) BufferedImage!))240 (add-eye! world (.getCamera world) no-op)241 ;;(set-gravity world (Vector3f. 0 0 0))242 ;;(com.aurellem.capture.Capture/captureVideo243 ;; world (file-str "/home/r/proj/ai-videos/hand"))244 ;;(.setTimer world (RatchetTimer. 60))245 (speed-up world)246 (set-gravity world (Vector3f. 0 0 0))247 )248 (fn [world tpf]249 ;;(dorun250 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))252 (prop-debug (prop))254 (dorun255 (map #(%1 (%2 (.getRootNode world)))256 touch-debug-windows touch-nerves))258 (dorun259 (map #(%1 (%2))260 vision-debug vision-data))261 (dorun262 (map #(%1 (%2 world)) hearing-windows hearing-senses))265 ;;(println-repl (vision-data))266 (.setLocalTranslation me (.getLocation (.getCamera world)))269 )]270 ;;(let [timer (atom 0)]271 ;; (fn [_ _]272 ;; (swap! timer inc)273 ;; (if (= (rem @timer 60) 0)274 ;; (println-repl (float (/ @timer 60))))))275 ))))279 ;; the camera will stay in its initial position/rotation with relation280 ;; to the spatial.283 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli,284 ;;anchor-qualia, augment-organ, with-organ286 (defn follow-test287 "show a camera that stays in the same relative position to a blue cube."288 []289 (let [camera-pos (Vector3f. 0 30 0)290 rock (box 1 1 1 :color ColorRGBA/Blue291 :position (Vector3f. 0 10 0)292 :mass 30293 )294 rot (.getWorldRotation rock)296 table (box 3 1 10 :color ColorRGBA/Gray :mass 0297 :position (Vector3f. 0 -3 0))]299 (world300 (nodify [rock table])301 standard-debug-controls302 (fn [world]303 (let304 [cam (doto (.clone (.getCamera world))305 (.setLocation camera-pos)306 (.lookAt Vector3f/ZERO307 Vector3f/UNIT_X))]308 (bind-sense rock cam)310 (.setTimer world (RatchetTimer. 60))311 (add-eye! world cam (comp (view-image) BufferedImage!))312 (add-eye! world (.getCamera world) no-op))313 )314 (fn [_ _] (println-repl rot)))))318 #+end_src320 #+results: body-1321 : #'cortex.silly/follow-test324 * COMMENT purgatory325 #+begin_src clojure327 (defn bullet-trans* []328 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red329 :position (Vector3f. 5 0 0)330 :mass 90)331 obj-b (sphere 0.5 :color ColorRGBA/Blue332 :position (Vector3f. -5 0 0)333 :mass 0)334 control-a (.getControl obj-a RigidBodyControl)335 control-b (.getControl obj-b RigidBodyControl)336 move-up? (atom nil)337 move-down? (atom nil)338 move-left? (atom nil)339 move-right? (atom nil)340 roll-left? (atom nil)341 roll-right? (atom nil)342 force 100343 swivel344 (.toRotationMatrix345 (doto (Quaternion.)346 (.fromAngleAxis (/ Math/PI 2)347 Vector3f/UNIT_X)))348 x-move349 (doto (Matrix3f.)350 (.fromStartEndVectors Vector3f/UNIT_X351 (.normalize (Vector3f. 1 1 0))))353 timer (atom 0)]354 (doto355 (ConeJoint.356 control-a control-b357 (Vector3f. -8 0 0)358 (Vector3f. 2 0 0)359 ;;swivel swivel360 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY361 x-move Matrix3f/IDENTITY362 )363 (.setCollisionBetweenLinkedBodys false)364 (.setLimit (* 1 (/ Math/PI 4)) ;; twist365 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane366 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane367 (world (nodify368 [obj-a obj-b])369 (merge standard-debug-controls370 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))371 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))372 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))373 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))374 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))375 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})377 (fn [world]378 (enable-debug world)379 (set-gravity world Vector3f/ZERO)380 )382 (fn [world _]384 (if @move-up?385 (.applyForce control-a386 (Vector3f. force 0 0)387 (Vector3f. 0 0 0)))388 (if @move-down?389 (.applyForce control-a390 (Vector3f. (- force) 0 0)391 (Vector3f. 0 0 0)))392 (if @move-left?393 (.applyForce control-a394 (Vector3f. 0 force 0)395 (Vector3f. 0 0 0)))396 (if @move-right?397 (.applyForce control-a398 (Vector3f. 0 (- force) 0)399 (Vector3f. 0 0 0)))401 (if @roll-left?402 (.applyForce control-a403 (Vector3f. 0 0 force)404 (Vector3f. 0 0 0)))405 (if @roll-right?406 (.applyForce control-a407 (Vector3f. 0 0 (- force))408 (Vector3f. 0 0 0)))410 (if (zero? (rem (swap! timer inc) 100))411 (.attachChild412 (.getRootNode world)413 (sphere 0.05 :color ColorRGBA/Yellow414 :physical? false :position415 (.getWorldTranslation obj-a)))))416 )417 ))419 (defn test-joint [joint]420 (let [[origin top bottom floor] (world-setup joint)421 control (.getControl top RigidBodyControl)422 move-up? (atom false)423 move-down? (atom false)424 move-left? (atom false)425 move-right? (atom false)426 roll-left? (atom false)427 roll-right? (atom false)428 timer (atom 0)]430 (world431 (nodify [top bottom floor origin])432 (merge standard-debug-controls433 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))434 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))435 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))436 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))437 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))438 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})440 (fn [world]441 (light-up-everything world)442 (enable-debug world)443 (set-gravity world (Vector3f. 0 0 0))444 )446 (fn [world _]447 (if (zero? (rem (swap! timer inc) 100))448 (do449 ;; (println-repl @timer)450 (.attachChild (.getRootNode world)451 (sphere 0.05 :color ColorRGBA/Yellow452 :position (.getWorldTranslation top)453 :physical? false))454 (.attachChild (.getRootNode world)455 (sphere 0.05 :color ColorRGBA/LightGray456 :position (.getWorldTranslation bottom)457 :physical? false))))459 (if @move-up?460 (.applyTorque control461 (.mult (.getPhysicsRotation control)462 (Vector3f. 0 0 10))))463 (if @move-down?464 (.applyTorque control465 (.mult (.getPhysicsRotation control)466 (Vector3f. 0 0 -10))))467 (if @move-left?468 (.applyTorque control469 (.mult (.getPhysicsRotation control)470 (Vector3f. 0 10 0))))471 (if @move-right?472 (.applyTorque control473 (.mult (.getPhysicsRotation control)474 (Vector3f. 0 -10 0))))475 (if @roll-left?476 (.applyTorque control477 (.mult (.getPhysicsRotation control)478 (Vector3f. -1 0 0))))479 (if @roll-right?480 (.applyTorque control481 (.mult (.getPhysicsRotation control)482 (Vector3f. 1 0 0))))))))483 #+end_src486 * COMMENT generate source487 #+begin_src clojure :tangle ../src/cortex/silly.clj488 <<body-1>>489 #+end_src