Mercurial > cortex
view org/test-creature.org @ 180:f1b078375484
refactored movement
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 04 Feb 2012 07:20:12 -0700 |
parents | 6fba17a74a57 |
children | cfb71209ddc6 |
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 style116 (defn gray-scale [num]117 (+ num118 (bit-shift-left num 8)119 (bit-shift-left num 16)))121 (defn debug-touch-window122 "creates function that offers a debug view of sensor data"123 []124 (let [vi (view-image)]125 (fn126 [[coords sensor-data]]127 (let [image (points->image coords)]128 (dorun129 (for [i (range (count coords))]130 (.setRGB image ((coords i) 0) ((coords i) 1)131 (gray-scale (sensor-data i)))))134 (vi image)))))136 (defn debug-vision-window137 "creates function that offers a debug view of sensor data"138 []139 (let [vi (view-image)]140 (fn141 [[coords sensor-data]]142 (let [image (points->image coords)]143 (dorun144 (for [i (range (count coords))]145 (.setRGB image ((coords i) 0) ((coords i) 1)146 (sensor-data i))))147 (vi image)))))149 (defn debug-hearing-window150 "view audio data"151 [height]152 (let [vi (view-image)]153 (fn [[coords sensor-data]]154 (let [image (BufferedImage. (count coords) height155 BufferedImage/TYPE_INT_RGB)]156 (dorun157 (for [x (range (count coords))]158 (dorun159 (for [y (range height)]160 (let [raw-sensor (sensor-data x)]161 (.setRGB image x y (gray-scale raw-sensor)))))))163 (vi image)))))165 (defn test-creature [thing]166 (let [x-axis167 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)168 y-axis169 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)170 z-axis171 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)172 creature (doto173 (load-blender-model thing)174 (body!))175 touch-nerves (touch! creature)176 touch-debug-windows (map (fn [_] (debug-touch-window)) touch-nerves)177 vision-data (vision! creature)178 vision-debug (map (fn [_] (debug-vision-window)) vision-data)179 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)180 hearing-senses (hearing! creature)181 hearing-windows (map (fn [_] (debug-hearing-window 50))182 hearing-senses)183 bell (AudioNode. (asset-manager)184 "Sounds/pure.wav" false)185 prop (proprioception! creature)186 prop-debug (proprioception-debug-window)188 muscle-fns (movement! creature)189 ;; dream190 fix-display (runonce191 (fn [world] (add-camera! world (.getCamera world) no-op)))192 ]195 (apply196 world197 (with-movement198 (.getChild creature "worm-21")199 ["key-r" "key-t"200 "key-f" "key-g"201 "key-v" "key-b"]202 [10 10 10 10 1 1]203 [(nodify [creature204 (box 10 2 10 :position (Vector3f. 0 -9 0)205 :color ColorRGBA/Gray :mass 0)206 x-axis y-axis z-axis207 me208 ])209 (merge standard-debug-controls210 {"key-return"211 (fn [_ value]212 (if value213 (do214 (println-repl "play-sound")215 (.play bell))))216 "key-h"217 (fn [_ value]218 (if value219 (do220 (println-repl "muscle activating!")221 ((first muscle-fns) 199))))223 })224 (fn [world]225 (light-up-everything world)226 (enable-debug world)227 ;;(dorun (map #(% world) init-vision-fns))228 ;;(dorun (map #(% world) init-hearing-fns))230 (add-camera! world231 (add-eye! creature (test-eye))232 (comp (view-image) BufferedImage!))235 ;;(set-gravity world (Vector3f. 0 0 0))236 ;;(com.aurellem.capture.Capture/captureVideo237 ;; world (file-str "/home/r/proj/ai-videos/hand"))238 ;;(.setTimer world (RatchetTimer. 60))239 (speed-up world)240 (set-gravity world (Vector3f. 0 0 0))241 )242 (fn [world tpf]243 ;;(dorun244 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))246 (prop-debug (prop))248 (dorun249 (map #(%1 (%2 (.getRootNode world)))250 touch-debug-windows touch-nerves))252 (dorun253 (map #(%1 (%2 world))254 vision-debug vision-data))255 (dorun256 (map #(%1 (%2 world)) hearing-windows hearing-senses))259 ;;(println-repl (vision-data))260 (.setLocalTranslation me (.getLocation (.getCamera world)))261 (fix-display world)263 )]264 ;;(let [timer (atom 0)]265 ;; (fn [_ _]266 ;; (swap! timer inc)267 ;; (if (= (rem @timer 60) 0)268 ;; (println-repl (float (/ @timer 60))))))269 ))))273 ;; the camera will stay in its initial position/rotation with relation274 ;; to the spatial.277 ;;dylan (defn follow-sense, adjoin-sense, attach-stimuli,278 ;;anchor-qualia, augment-organ, with-organ280 (defn follow-test281 "show a camera that stays in the same relative position to a blue cube."282 []283 (let [camera-pos (Vector3f. 0 30 0)284 rock (box 1 1 1 :color ColorRGBA/Blue285 :position (Vector3f. 0 10 0)286 :mass 30287 )288 rot (.getWorldRotation rock)290 table (box 3 1 10 :color ColorRGBA/Gray :mass 0291 :position (Vector3f. 0 -3 0))]293 (world294 (nodify [rock table])295 standard-debug-controls296 (fn [world]297 (let298 [cam (doto (.clone (.getCamera world))299 (.setLocation camera-pos)300 (.lookAt Vector3f/ZERO301 Vector3f/UNIT_X))]302 (bind-sense rock cam)304 (.setTimer world (RatchetTimer. 60))305 (add-camera! world cam (comp (view-image) BufferedImage!))306 (add-camera! world (.getCamera world) no-op))307 )308 (fn [_ _] (println-repl rot)))))312 #+end_src314 #+results: body-1315 : #'cortex.silly/follow-test318 * COMMENT purgatory319 #+begin_src clojure321 (defn bullet-trans* []322 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red323 :position (Vector3f. 5 0 0)324 :mass 90)325 obj-b (sphere 0.5 :color ColorRGBA/Blue326 :position (Vector3f. -5 0 0)327 :mass 0)328 control-a (.getControl obj-a RigidBodyControl)329 control-b (.getControl obj-b RigidBodyControl)330 move-up? (atom nil)331 move-down? (atom nil)332 move-left? (atom nil)333 move-right? (atom nil)334 roll-left? (atom nil)335 roll-right? (atom nil)336 force 100337 swivel338 (.toRotationMatrix339 (doto (Quaternion.)340 (.fromAngleAxis (/ Math/PI 2)341 Vector3f/UNIT_X)))342 x-move343 (doto (Matrix3f.)344 (.fromStartEndVectors Vector3f/UNIT_X345 (.normalize (Vector3f. 1 1 0))))347 timer (atom 0)]348 (doto349 (ConeJoint.350 control-a control-b351 (Vector3f. -8 0 0)352 (Vector3f. 2 0 0)353 ;;swivel swivel354 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY355 x-move Matrix3f/IDENTITY356 )357 (.setCollisionBetweenLinkedBodys false)358 (.setLimit (* 1 (/ Math/PI 4)) ;; twist359 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane360 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane361 (world (nodify362 [obj-a obj-b])363 (merge standard-debug-controls364 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))365 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))366 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))367 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))368 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))369 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})371 (fn [world]372 (enable-debug world)373 (set-gravity world Vector3f/ZERO)374 )376 (fn [world _]378 (if @move-up?379 (.applyForce control-a380 (Vector3f. force 0 0)381 (Vector3f. 0 0 0)))382 (if @move-down?383 (.applyForce control-a384 (Vector3f. (- force) 0 0)385 (Vector3f. 0 0 0)))386 (if @move-left?387 (.applyForce control-a388 (Vector3f. 0 force 0)389 (Vector3f. 0 0 0)))390 (if @move-right?391 (.applyForce control-a392 (Vector3f. 0 (- force) 0)393 (Vector3f. 0 0 0)))395 (if @roll-left?396 (.applyForce control-a397 (Vector3f. 0 0 force)398 (Vector3f. 0 0 0)))399 (if @roll-right?400 (.applyForce control-a401 (Vector3f. 0 0 (- force))402 (Vector3f. 0 0 0)))404 (if (zero? (rem (swap! timer inc) 100))405 (.attachChild406 (.getRootNode world)407 (sphere 0.05 :color ColorRGBA/Yellow408 :physical? false :position409 (.getWorldTranslation obj-a)))))410 )411 ))413 (defn test-joint [joint]414 (let [[origin top bottom floor] (world-setup joint)415 control (.getControl top RigidBodyControl)416 move-up? (atom false)417 move-down? (atom false)418 move-left? (atom false)419 move-right? (atom false)420 roll-left? (atom false)421 roll-right? (atom false)422 timer (atom 0)]424 (world425 (nodify [top bottom floor origin])426 (merge standard-debug-controls427 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))428 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))429 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))430 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))431 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))432 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})434 (fn [world]435 (light-up-everything world)436 (enable-debug world)437 (set-gravity world (Vector3f. 0 0 0))438 )440 (fn [world _]441 (if (zero? (rem (swap! timer inc) 100))442 (do443 ;; (println-repl @timer)444 (.attachChild (.getRootNode world)445 (sphere 0.05 :color ColorRGBA/Yellow446 :position (.getWorldTranslation top)447 :physical? false))448 (.attachChild (.getRootNode world)449 (sphere 0.05 :color ColorRGBA/LightGray450 :position (.getWorldTranslation bottom)451 :physical? false))))453 (if @move-up?454 (.applyTorque control455 (.mult (.getPhysicsRotation control)456 (Vector3f. 0 0 10))))457 (if @move-down?458 (.applyTorque control459 (.mult (.getPhysicsRotation control)460 (Vector3f. 0 0 -10))))461 (if @move-left?462 (.applyTorque control463 (.mult (.getPhysicsRotation control)464 (Vector3f. 0 10 0))))465 (if @move-right?466 (.applyTorque control467 (.mult (.getPhysicsRotation control)468 (Vector3f. 0 -10 0))))469 (if @roll-left?470 (.applyTorque control471 (.mult (.getPhysicsRotation control)472 (Vector3f. -1 0 0))))473 (if @roll-right?474 (.applyTorque control475 (.mult (.getPhysicsRotation control)476 (Vector3f. 1 0 0))))))))477 #+end_src480 * COMMENT generate source481 #+begin_src clojure :tangle ../src/cortex/silly.clj482 <<body-1>>483 #+end_src