Mercurial > cortex
view org/test-creature.org @ 92:e70ec4bba96b
saving progress...
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 09 Jan 2012 06:05:29 -0700 |
parents | 2bcc7636cfea |
children | 7b739503836a |
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.org9 * Intro10 So far, I've made the following senses --11 - Vision12 - Hearing13 - Touch14 - Proprioception16 And one effector:17 - Movement19 However, the code so far has only enabled these senses, but has not20 actually implemented them. For example, there is still a lot of work21 to be done for vision. I need to be able to create an /eyeball/ in22 simulation that can be moved around and see the world from different23 angles. I also need to determine weather to use log-polar or cartesian24 for the visual input, and I need to determine how/wether to25 disceritise the visual input.27 I also want to be able to visualize both the sensors and the28 effectors in pretty pictures. This semi-retarted creature will by my29 first attempt at bringing everything together.31 * The creature's body33 Still going to do an eve-like body in blender, but due to problems34 importing the joints, etc into jMonkeyEngine3, I',m going to do all35 the connecting here in clojure code, using the names of the individual36 components and trial and error. Later, I'll maybe make some sort of37 creature-building modifications to blender that support whatever38 discreitized senses I'm going to make.40 #+name: body-141 #+begin_src clojure42 (ns cortex.silly43 "let's play!"44 {:author "Robert McIntyre"})46 ;; TODO remove this!47 (require 'cortex.import)48 (cortex.import/mega-import-jme3)49 (use '(cortex world util body hearing touch vision))51 (rlm.rlm-commands/help)53 (declare joint-create)55 (defn load-bullet []56 (let [sim (world (Node.) {} no-op no-op)]57 (.enqueue58 sim59 (fn []60 (.stop sim)))61 (.start sim)))63 (defn load-blender-model64 "Load a .blend file using an asset folder relative path."65 [^String model]66 (.loadModel67 (doto (asset-manager)68 (.registerLoader BlenderModelLoader (into-array String ["blend"])))69 model))71 (defn meta-data [blender-node key]72 (if-let [data (.getUserData blender-node "properties")]73 (.findValue data key)74 nil))76 (defn blender-to-jme77 "Convert from Blender coordinates to JME coordinates"78 [#^Vector3f in]79 (Vector3f. (.getX in)80 (.getZ in)81 (- (.getY in))))83 (defn jme-to-blender84 "Convert from JME coordinates to Blender coordinates"85 [#^Vector3f in]86 (Vector3f. (.getX in)87 (- (.getZ in))88 (.getY in)))90 (defn joint-targets91 "Return the two closest two objects to the joint object, ordered92 from bottom to top according to the joint's rotation."93 [#^Node parts #^Node joint]94 ;;(println (meta-data joint "joint"))95 (.getWorldRotation joint)96 (loop [radius (float 0.01)]97 (let [results (CollisionResults.)]98 (.collideWith99 parts100 (BoundingBox. (.getWorldTranslation joint)101 radius radius radius)102 results)103 (let [targets104 (distinct105 (map #(.getGeometry %) results))]106 (if (>= (count targets) 2)107 (sort-by108 #(let [v109 (jme-to-blender110 (.mult111 (.inverse (.getWorldRotation joint))112 (.subtract (.getWorldTranslation %)113 (.getWorldTranslation joint))))]114 (println-repl (.getName %) ":" v)115 (.dot (Vector3f. 1 1 1)116 v))117 (take 2 targets))118 (recur (float (* radius 2))))))))120 (defn world-to-local121 "Convert the world coordinates into coordinates relative to the122 object (i.e. local coordinates), taking into account the rotation123 of object."124 [#^Spatial object world-coordinate]125 (let [out (Vector3f.)]126 (.worldToLocal object world-coordinate out) out))128 (defmulti joint-dispatch129 "Translate blender pseudo-joints into real JME joints."130 (fn [constraints & _]131 (:type constraints)))133 (defmethod joint-dispatch :point134 [constraints control-a control-b pivot-a pivot-b rotation]135 (println-repl "creating POINT2POINT joint")136 (Point2PointJoint.137 control-a138 control-b139 pivot-a140 pivot-b))142 (defmethod joint-dispatch :hinge143 [constraints control-a control-b pivot-a pivot-b rotation]144 (println-repl "creating HINGE joint")145 (let [axis146 (if-let147 [axis (:axis constraints)]148 axis149 Vector3f/UNIT_X)150 [limit-1 limit-2] (:limit constraints)151 hinge-axis152 (.mult153 rotation154 (blender-to-jme axis))]155 (doto156 (HingeJoint.157 control-a158 control-b159 pivot-a160 pivot-b161 hinge-axis162 hinge-axis)163 (.setLimit limit-1 limit-2))))165 (defmethod joint-dispatch :cone166 [constraints control-a control-b pivot-a pivot-b rotation]167 (let [limit-xz (:limit-xz constraints)168 limit-xy (:limit-xy constraints)169 twist (:twist constraints)]171 (println-repl "creating CONE joint")172 (println-repl rotation)173 (println-repl174 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))175 (println-repl176 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))177 (println-repl178 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))179 (doto180 (ConeJoint.181 control-a182 control-b183 pivot-a184 pivot-b185 rotation186 rotation)187 (.setLimit (float limit-xz)188 (float limit-xy)189 (float twist)))))191 (defn connect192 "here are some examples:193 {:type :point}194 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}195 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)197 {:type :cone :limit-xz 0]198 :limit-xy 0]199 :twist 0]} (use XZY rotation mode in blender!)"200 [#^Node obj-a #^Node obj-b #^Node joint]201 (let [control-a (.getControl obj-a RigidBodyControl)202 control-b (.getControl obj-b RigidBodyControl)203 joint-center (.getWorldTranslation joint)204 joint-rotation (.toRotationMatrix (.getWorldRotation joint))205 pivot-a (world-to-local obj-a joint-center)206 pivot-b (world-to-local obj-b joint-center)]208 (if-let [constraints209 (map-vals210 eval211 (read-string212 (meta-data joint "joint")))]213 ;; A side-effect of creating a joint registers214 ;; it with both physics objects which in turn215 ;; will register the joint with the physics system216 ;; when the simulation is started.217 (do218 (println-repl "creating joint between"219 (.getName obj-a) "and" (.getName obj-b))220 (joint-dispatch constraints221 control-a control-b222 pivot-a pivot-b223 joint-rotation))224 (println-repl "could not find joint meta-data!"))))226 (defn assemble-creature [#^Node pieces joints]227 (dorun228 (map229 (fn [geom]230 (let [physics-control231 (RigidBodyControl.232 (HullCollisionShape.233 (.getMesh geom))234 (if-let [mass (meta-data geom "mass")]235 (do236 (println-repl237 "setting" (.getName geom) "mass to" (float mass))238 (float mass))239 (float 1)))]241 (.addControl geom physics-control)))242 (filter #(isa? (class %) Geometry )243 (node-seq pieces))))245 (dorun246 (map247 (fn [joint]248 (let [[obj-a obj-b]249 (joint-targets pieces joint)]250 (connect obj-a obj-b joint)))251 joints))252 pieces)254 (defn blender-creature [blender-path]255 (let [model (load-blender-model blender-path)256 joints257 (if-let [joint-node (.getChild model "joints")]258 (seq (.getChildren joint-node))259 (do (println-repl "could not find joints node")260 []))]261 (assemble-creature model joints)))263 (def hand "Models/creature1/one.blend")265 (def worm "Models/creature1/try-again.blend")267 (def touch "Models/creature1/touch.blend")269 (defn worm-model [] (load-blender-model worm))271 (defn x-ray [#^ColorRGBA color]272 (doto (Material. (asset-manager)273 "Common/MatDefs/Misc/Unshaded.j3md")274 (.setColor "Color" color)275 (-> (.getAdditionalRenderState)276 (.setDepthTest false))))278 (defn test-creature [thing]279 (let [x-axis280 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)281 y-axis282 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)283 z-axis284 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)]285 (world286 (nodify [(blender-creature thing)287 (box 10 2 10 :position (Vector3f. 0 -9 0)288 :color ColorRGBA/Gray :mass 0)289 x-axis y-axis z-axis290 ])291 standard-debug-controls292 (fn [world]293 (light-up-everything world)294 (enable-debug world)295 ;;(com.aurellem.capture.Capture/captureVideo296 ;; world (file-str "/home/r/proj/ai-videos/hand"))297 (.setTimer world (NanoTimer.))298 ;;(set-gravity world (Vector3f. 0 0 0))299 (speed-up world)300 )301 no-op302 ;;(let [timer (atom 0)]303 ;; (fn [_ _]304 ;; (swap! timer inc)305 ;; (if (= (rem @timer 60) 0)306 ;; (println-repl (float (/ @timer 60))))))307 )))310 (defn colorful []311 (.getChild (worm-model) "worm-21"))313 (import jme3tools.converters.ImageToAwt)315 (import ij.ImagePlus)317 (defn triangle-indices318 "Get the triangle vertex indices of a given triangle from a given319 mesh."320 [#^Mesh mesh triangle-index]321 (let [indices (int-array 3)]322 (.getTriangle mesh triangle-index indices)323 (vec indices)))325 (defn uv-coord326 "Get the uv-coordinates of the vertex named by vertex-index"327 [#^Mesh mesh vertex-index]328 (let [UV-buffer329 (.getData330 (.getBuffer331 mesh332 VertexBuffer$Type/TexCoord))]333 (Vector2f.334 (.get UV-buffer (* vertex-index 2))335 (.get UV-buffer (+ 1 (* vertex-index 2))))))337 (defn touch-receptor-image338 "Return the touch-sensor distribution image in ImagePlus format."339 [#^Geometry obj]340 (let341 [mat (.getMaterial obj)342 texture343 (.getTextureValue344 (.getTextureParam345 mat346 MaterialHelper/TEXTURE_TYPE_DIFFUSE))347 im (.getImage texture)]348 (ImagePlus.349 "UV-map"350 (ImageToAwt/convert im false false 0))))353 (import ij.process.ImageProcessor)354 (import java.awt.image.BufferedImage)356 (defprotocol Frame357 (frame [this]))359 (extend-type BufferedImage360 Frame361 (frame [image]362 (merge363 (apply364 hash-map365 (interleave366 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]367 (vector x y)))368 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]369 (let [data (.getRGB image x y)]370 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)371 :g (bit-shift-right (bit-and 0x00ff00 data) 8)372 :b (bit-and 0x0000ff data)))))))373 {:width (.getWidth image) :height (.getHeight image)})))376 (extend-type ImagePlus377 Frame378 (frame [image+]379 (frame (.getBufferedImage image+))))381 (def white -1)383 (defn filter-pixels384 "List the coordinates of all pixels matching pred."385 {:author "Dylan Holmes"}386 [pred #^ImageProcessor ip]387 (let388 [width (.getWidth ip)389 height (.getHeight ip)]390 ((fn accumulate [x y matches]391 (cond392 (>= y height) matches393 (>= x width) (recur 0 (inc y) matches)394 (pred (.getPixel ip x y))395 (recur (inc x) y (conj matches (Vector2f. x y)))396 :else (recur (inc x) y matches)))397 0 0 [])))399 (defn white-coordinates400 "List the coordinates of all the white pixels in an image."401 [#^ImageProcessor ip]402 (filter-pixels #(= % white) ip))404 (defn same-side? [p1 p2 ref p]405 (<=406 0407 (.dot408 (.cross (.subtract p2 p1) (.subtract p p1))409 (.cross (.subtract p2 p1) (.subtract ref p1)))))411 (defn inside-triangle?412 [vert-1 vert-2 vert-3 p]413 (and414 (same-side? vert-1 vert-2 vert-3 p)415 (same-side? vert-2 vert-3 vert-1 p)416 (same-side? vert-3 vert-1 vert-2 p)))419 (defn white? [color]420 (and421 (= (:r color) 255)422 (= (:b color) 255)423 (= (:g color) 255)))426 ;; for each triangle in the mesh,427 ;; get the normal to the triangle,428 ;; look at the UV touch map, restricted to that triangle,429 ;; get the positions of those touch sensors in geometry-relative430 ;; coordinates.431 (defn tactile-coords [#^Geometry obj]432 (let [mesh (.getMesh obj)433 num-triangles (.getTriangleCount mesh)434 num-verticies (.getVertexCount mesh)435 uv-coord (partial uv-coord mesh)436 triangle-indices (partial triangle-indices mesh)437 receptors (touch-receptor-image obj)438 ]439 (map440 (fn [[tri-1 tri-2 tri-3]]441 (let [width (.getWidth receptors)442 height (.getHeight receptors)443 uv-1 (uv-coord tri-1)444 uv-2 (uv-coord tri-2)445 uv-3 (uv-coord tri-3)446 x-coords (map #(.getX %) [uv-1 uv-2 uv-3])447 y-coords (map #(.getY %) [uv-1 uv-2 uv-3])448 max-x (Math/ceil (* width (apply max x-coords)))449 min-x (Math/floor (* width (apply min x-coords)))450 max-y (Math/ceil (* height (apply max y-coords)))451 min-y (Math/floor (* height (apply min y-coords)))453 image-1 (Vector2f. (* width (.getX uv-1))454 (* height (.getY uv-1)))455 image-2 (Vector2f. (* width (.getX uv-2))456 (* height (.getY uv-2)))457 image-3 (Vector2f. (* width (.getX uv-3))458 (* height (.getY uv-3)))459 left-corner460 (Vector2f. min-x min-y)462 ]464 (.setRoi receptors min-x min-y (- max-x min-x) (- max-y min-y))465 (let [processor (.crop (.getProcessor receptors))466 image (frame (.getBufferedImage processor))]467 (with-meta468 (filter-keys469 (fn [[x y]]470 (inside-triangle?471 (.subtract image-1 left-corner)472 (.subtract image-2 left-corner)473 (.subtract image-3 left-corner)474 (Vector2f. x y)))477 (filter-vals white? image))478 {:image479 (comment480 (.getBufferedImage481 (doto processor482 (.flipVertical))))483 }484 ))485 )) (map triangle-indices (range num-triangles)))))493 (defn all-names []494 (concat495 (re-split #"\n" (slurp (file-str496 "/home/r/proj/names/dist.female.first")))497 (re-split #"\n" (slurp (file-str498 "/home/r/proj/names/dist.male.first")))499 (re-split #"\n" (slurp (file-str500 "/home/r/proj/names/dist.all.last")))))510 (defrecord LulzLoader [])511 (defprotocol Lulzable (load-lulz [this]))512 (extend-type LulzLoader513 Lulzable514 (load-lulz [this] (println "the lulz have arrived!")))517 (defn world-setup [joint]518 (let [joint-position (Vector3f. 0 0 0)519 joint-rotation520 (.toRotationMatrix521 (.mult522 (doto (Quaternion.)523 (.fromAngleAxis524 (* 1 (/ Math/PI 4))525 (Vector3f. -1 0 0)))526 (doto (Quaternion.)527 (.fromAngleAxis528 (* 1 (/ Math/PI 2))529 (Vector3f. 0 0 1)))))530 top-position (.mult joint-rotation (Vector3f. 8 0 0))532 origin (doto533 (sphere 0.1 :physical? false :color ColorRGBA/Cyan534 :position top-position))535 top (doto536 (sphere 0.1 :physical? false :color ColorRGBA/Yellow537 :position top-position)539 (.addControl540 (RigidBodyControl.541 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))542 bottom (doto543 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray544 :position (Vector3f. 0 0 0))545 (.addControl546 (RigidBodyControl.547 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))548 table (box 10 2 10 :position (Vector3f. 0 -20 0)549 :color ColorRGBA/Gray :mass 0)550 a (.getControl top RigidBodyControl)551 b (.getControl bottom RigidBodyControl)]553 (cond554 (= joint :cone)556 (doto (ConeJoint.557 a b558 (world-to-local top joint-position)559 (world-to-local bottom joint-position)560 joint-rotation561 joint-rotation562 )565 (.setLimit (* (/ 10) Math/PI)566 (* (/ 4) Math/PI)567 0)))568 [origin top bottom table]))570 (defn test-joint [joint]571 (let [[origin top bottom floor] (world-setup joint)572 control (.getControl top RigidBodyControl)573 move-up? (atom false)574 move-down? (atom false)575 move-left? (atom false)576 move-right? (atom false)577 roll-left? (atom false)578 roll-right? (atom false)579 timer (atom 0)]581 (world582 (nodify [top bottom floor origin])583 (merge standard-debug-controls584 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))585 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))586 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))587 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))588 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))589 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})591 (fn [world]592 (light-up-everything world)593 (enable-debug world)594 (set-gravity world (Vector3f. 0 0 0))595 )597 (fn [world _]598 (if (zero? (rem (swap! timer inc) 100))599 (do600 ;; (println-repl @timer)601 (.attachChild (.getRootNode world)602 (sphere 0.05 :color ColorRGBA/Yellow603 :position (.getWorldTranslation top)604 :physical? false))605 (.attachChild (.getRootNode world)606 (sphere 0.05 :color ColorRGBA/LightGray607 :position (.getWorldTranslation bottom)608 :physical? false))))610 (if @move-up?611 (.applyTorque control612 (.mult (.getPhysicsRotation control)613 (Vector3f. 0 0 10))))614 (if @move-down?615 (.applyTorque control616 (.mult (.getPhysicsRotation control)617 (Vector3f. 0 0 -10))))618 (if @move-left?619 (.applyTorque control620 (.mult (.getPhysicsRotation control)621 (Vector3f. 0 10 0))))622 (if @move-right?623 (.applyTorque control624 (.mult (.getPhysicsRotation control)625 (Vector3f. 0 -10 0))))626 (if @roll-left?627 (.applyTorque control628 (.mult (.getPhysicsRotation control)629 (Vector3f. -1 0 0))))630 (if @roll-right?631 (.applyTorque control632 (.mult (.getPhysicsRotation control)633 (Vector3f. 1 0 0))))))))637 #+end_src639 #+results: body-1640 : #'cortex.silly/test-joint643 * COMMENT purgatory644 #+begin_src clojure645 (defn bullet-trans []646 (let [obj-a (sphere 0.5 :color ColorRGBA/Red647 :position (Vector3f. -10 5 0))648 obj-b (sphere 0.5 :color ColorRGBA/Blue649 :position (Vector3f. -10 -5 0)650 :mass 0)651 control-a (.getControl obj-a RigidBodyControl)652 control-b (.getControl obj-b RigidBodyControl)653 swivel654 (.toRotationMatrix655 (doto (Quaternion.)656 (.fromAngleAxis (/ Math/PI 2)657 Vector3f/UNIT_X)))]658 (doto659 (ConeJoint.660 control-a control-b661 (Vector3f. 0 5 0)662 (Vector3f. 0 -5 0)663 swivel swivel)664 (.setLimit (* 0.6 (/ Math/PI 4))665 (/ Math/PI 4)666 (* Math/PI 0.8)))667 (world (nodify668 [obj-a obj-b])669 standard-debug-controls670 enable-debug671 no-op)))674 (defn bullet-trans* []675 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red676 :position (Vector3f. 5 0 0)677 :mass 90)678 obj-b (sphere 0.5 :color ColorRGBA/Blue679 :position (Vector3f. -5 0 0)680 :mass 0)681 control-a (.getControl obj-a RigidBodyControl)682 control-b (.getControl obj-b RigidBodyControl)683 move-up? (atom nil)684 move-down? (atom nil)685 move-left? (atom nil)686 move-right? (atom nil)687 roll-left? (atom nil)688 roll-right? (atom nil)689 force 100690 swivel691 (.toRotationMatrix692 (doto (Quaternion.)693 (.fromAngleAxis (/ Math/PI 2)694 Vector3f/UNIT_X)))695 x-move696 (doto (Matrix3f.)697 (.fromStartEndVectors Vector3f/UNIT_X698 (.normalize (Vector3f. 1 1 0))))700 timer (atom 0)]701 (doto702 (ConeJoint.703 control-a control-b704 (Vector3f. -8 0 0)705 (Vector3f. 2 0 0)706 ;;swivel swivel707 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY708 x-move Matrix3f/IDENTITY709 )710 (.setCollisionBetweenLinkedBodys false)711 (.setLimit (* 1 (/ Math/PI 4)) ;; twist712 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane713 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane714 (world (nodify715 [obj-a obj-b])716 (merge standard-debug-controls717 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))718 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))719 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))720 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))721 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))722 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})724 (fn [world]725 (enable-debug world)726 (set-gravity world Vector3f/ZERO)727 )729 (fn [world _]731 (if @move-up?732 (.applyForce control-a733 (Vector3f. force 0 0)734 (Vector3f. 0 0 0)))735 (if @move-down?736 (.applyForce control-a737 (Vector3f. (- force) 0 0)738 (Vector3f. 0 0 0)))739 (if @move-left?740 (.applyForce control-a741 (Vector3f. 0 force 0)742 (Vector3f. 0 0 0)))743 (if @move-right?744 (.applyForce control-a745 (Vector3f. 0 (- force) 0)746 (Vector3f. 0 0 0)))748 (if @roll-left?749 (.applyForce control-a750 (Vector3f. 0 0 force)751 (Vector3f. 0 0 0)))752 (if @roll-right?753 (.applyForce control-a754 (Vector3f. 0 0 (- force))755 (Vector3f. 0 0 0)))757 (if (zero? (rem (swap! timer inc) 100))758 (.attachChild759 (.getRootNode world)760 (sphere 0.05 :color ColorRGBA/Yellow761 :physical? false :position762 (.getWorldTranslation obj-a)))))763 )764 ))768 #+end_src771 * COMMENT generate source772 #+begin_src clojure :tangle ../src/cortex/silly.clj773 <<body-1>>774 #+end_src