Mercurial > cortex
view org/body.org @ 144:48f9cba082eb
proprioception is position-dependent, investigating...
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 02 Feb 2012 03:17:11 -0700 |
parents | ccd057319c2a |
children | 7a49b81ca1bf |
line wrap: on
line source
1 #+title: The BODY!!!2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description: Simulating a body (movement, touch, propioception) in jMonkeyEngine3.5 #+SETUPFILE: ../../aurellem/org/setup.org6 #+INCLUDE: ../../aurellem/org/level-0.org8 * Proprioception9 #+name: proprioception10 #+begin_src clojure11 (ns cortex.body12 (:use (cortex world util))13 (:import14 com.jme3.math.Vector3f15 com.jme3.math.Quaternion16 com.jme3.math.Vector2f17 com.jme3.math.Matrix3f18 com.jme3.bullet.control.RigidBodyControl19 com.jme3.collision.CollisionResults20 com.jme3.bounding.BoundingBox))22 (import com.jme3.scene.Node)24 (defn jme-to-blender25 "Convert from JME coordinates to Blender coordinates"26 [#^Vector3f in]27 (Vector3f. (.getX in)28 (- (.getZ in))29 (.getY in)))31 (defn joint-targets32 "Return the two closest two objects to the joint object, ordered33 from bottom to top according to the joint's rotation."34 [#^Node parts #^Node joint]35 (loop [radius (float 0.01)]36 (let [results (CollisionResults.)]37 (.collideWith38 parts39 (BoundingBox. (.getWorldTranslation joint)40 radius radius radius)41 results)42 (let [targets43 (distinct44 (map #(.getGeometry %) results))]45 (if (>= (count targets) 2)46 (sort-by47 #(let [v48 (jme-to-blender49 (.mult50 (.inverse (.getWorldRotation joint))51 (.subtract (.getWorldTranslation %)52 (.getWorldTranslation joint))))]53 (println-repl (.getName %) ":" v)54 (.dot (Vector3f. 1 1 1)55 v))56 (take 2 targets))57 (recur (float (* radius 2))))))))59 (defn creature-joints60 "Return the children of the creature's \"joints\" node."61 [#^Node creature]62 (if-let [joint-node (.getChild creature "joints")]63 (seq (.getChildren joint-node))64 (do (println-repl "could not find JOINTS node") [])))66 (defn joint-proprioception [#^Node parts #^Node joint]67 (let [[obj-a obj-b] (joint-targets parts joint)68 joint-rot (.getWorldRotation joint)69 pre-inv-a (.inverse (.getWorldRotation obj-a))70 x (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_X))71 y (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_Y))72 z (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_Z))73 tmp-rot-a (.getWorldRotation obj-a)]74 (println-repl "x:" (.mult tmp-rot-a x))75 (println-repl "y:" (.mult tmp-rot-a y))76 (println-repl "z:" (.mult tmp-rot-a z))77 (println-repl "rot-a" (.getWorldRotation obj-a))78 (println-repl "rot-b" (.getWorldRotation obj-b))79 ;; this function will report proprioceptive information for the80 ;; joint.81 (fn []82 ;; x is the "twist" axis, y and z are the "bend" axes83 (let [rot-a (.getWorldRotation obj-a)84 ;;inv-a (.inverse rot-a)85 rot-b (.getWorldRotation obj-b)86 ;;relative (.mult rot-b inv-a)87 basis (doto (Matrix3f.)88 (.setColumn 0 (.mult rot-a x))89 (.setColumn 1 (.mult rot-a y))90 (.setColumn 2 (.mult rot-a z)))91 rotation-about-joint92 (doto (Quaternion.)93 (.fromRotationMatrix94 (.mult (.invert basis)95 (.toRotationMatrix rot-b))))96 [yaw roll pitch]97 (seq (.toAngles rotation-about-joint nil))]98 ;;return euler angles of the quaternion around the new basis99 [yaw roll pitch]100 ))))103 (defn proprioception104 "Create a function that provides proprioceptive information about an105 entire body."106 [#^Node creature]107 ;; extract the body's joints108 (let [joints (creature-joints creature)109 senses (map (partial joint-proprioception creature) joints)]110 (fn []111 (map #(%) senses))))113 (defn tap [obj direction force]114 (let [control (.getControl obj RigidBodyControl)]115 (.applyTorque116 control117 (.mult (.getPhysicsRotation control)118 (.mult (.normalize direction) (float force))))))121 (defn with-movement122 [object123 [up down left right roll-up roll-down :as keyboard]124 forces125 [root-node126 keymap127 intilization128 world-loop]]129 (let [add-keypress130 (fn [state keymap key]131 (merge keymap132 {key133 (fn [_ pressed?]134 (reset! state pressed?))}))135 move-up? (atom false)136 move-down? (atom false)137 move-left? (atom false)138 move-right? (atom false)139 roll-left? (atom false)140 roll-right? (atom false)142 directions [(Vector3f. 0 1 0)(Vector3f. 0 -1 0)143 (Vector3f. 0 0 1)(Vector3f. 0 0 -1)144 (Vector3f. -1 0 0)(Vector3f. 1 0 0)]145 atoms [move-left? move-right? move-up? move-down?146 roll-left? roll-right?]148 keymap* (reduce merge149 (map #(add-keypress %1 keymap %2)150 atoms151 keyboard))153 splice-loop (fn []154 (dorun155 (map156 (fn [sym direction force]157 (if @sym158 (tap object direction force)))159 atoms directions forces)))161 world-loop* (fn [world tpf]162 (world-loop world tpf)163 (splice-loop))]165 [root-node166 keymap*167 intilization168 world-loop*]))171 #+end_src173 #+results: proprioception174 : #'cortex.body/proprioception176 * Motor Control177 #+name: motor-control178 #+begin_src clojure179 (in-ns 'cortex.body)181 ;; surprisingly enough, terristerial creatures only move by using182 ;; torque applied about their joints. There's not a single straight183 ;; line of force in the human body at all! (A straight line of force184 ;; would correspond to some sort of jet or rocket propulseion.)186 (defn vector-motor-control187 "Create a function that accepts a sequence of Vector3f objects that188 describe the torque to be applied to each part of the body."189 [body]190 (let [nodes (node-seq body)191 controls (keep #(.getControl % RigidBodyControl) nodes)]192 (fn [torques]193 (map #(.applyTorque %1 %2)194 controls torques))))195 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;196 #+end_src198 ## note -- might want to add a lower dimensional, discrete version of199 ## this if it proves useful from a x-modal clustering perspective.201 * Examples203 #+name: test-body204 #+begin_src clojure205 (ns cortex.test.body206 (:use (cortex world util body))207 (:require cortex.silly)208 (:import209 com.jme3.math.Vector3f210 com.jme3.math.ColorRGBA211 com.jme3.bullet.joints.Point2PointJoint212 com.jme3.bullet.control.RigidBodyControl213 com.jme3.system.NanoTimer))215 (defn worm-segments216 "Create multiple evenly spaced box segments. They're fabulous!"217 [segment-length num-segments interstitial-space radius]218 (letfn [(nth-segment219 [n]220 (box segment-length radius radius :mass 0.1221 :position222 (Vector3f.223 (* 2 n (+ interstitial-space segment-length)) 0 0)224 :name (str "worm-segment" n)225 :color (ColorRGBA/randomColor)))]226 (map nth-segment (range num-segments))))228 (defn connect-at-midpoint229 "Connect two physics objects with a Point2Point joint constraint at230 the point equidistant from both objects' centers."231 [segmentA segmentB]232 (let [centerA (.getWorldTranslation segmentA)233 centerB (.getWorldTranslation segmentB)234 midpoint (.mult (.add centerA centerB) (float 0.5))235 pivotA (.subtract midpoint centerA)236 pivotB (.subtract midpoint centerB)238 ;; A side-effect of creating a joint registers239 ;; it with both physics objects which in turn240 ;; will register the joint with the physics system241 ;; when the simulation is started.242 joint (Point2PointJoint.243 (.getControl segmentA RigidBodyControl)244 (.getControl segmentB RigidBodyControl)245 pivotA246 pivotB)]247 segmentB))249 (defn eve-worm250 "Create a worm-like body bound by invisible joint constraints."251 []252 (let [segments (worm-segments 0.2 5 0.1 0.1)]253 (dorun (map (partial apply connect-at-midpoint)254 (partition 2 1 segments)))255 (nodify "worm" segments)))257 (defn worm-pattern258 "This is a simple, mindless motor control pattern that drives the259 second segment of the worm's body at an offset angle with260 sinusoidally varying strength."261 [time]262 (let [angle (* Math/PI (/ 9 20))263 direction (Vector3f. 0 (Math/sin angle) (Math/cos angle))]264 [Vector3f/ZERO265 (.mult266 direction267 (float (* 2 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))))268 Vector3f/ZERO269 Vector3f/ZERO270 Vector3f/ZERO]))272 (defn test-motor-control273 "Testing motor-control:274 You should see a multi-segmented worm-like object fall onto the275 table and begin writhing and moving."276 []277 (let [worm (eve-worm)278 time (atom 0)279 worm-motor-map (vector-motor-control worm)]280 (world281 (nodify [worm282 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0283 :color ColorRGBA/Gray)])284 standard-debug-controls285 (fn [world]286 (enable-debug world)287 (light-up-everything world)288 (comment289 (com.aurellem.capture.Capture/captureVideo290 world291 (file-str "/home/r/proj/cortex/tmp/moving-worm")))292 )294 (fn [_ _]295 (swap! time inc)296 (Thread/sleep 20)297 (dorun (worm-motor-map298 (worm-pattern @time)))))))302 (defn join-at-point [obj-a obj-b world-pivot]303 (cortex.silly/joint-dispatch304 {:type :point}305 (.getControl obj-a RigidBodyControl)306 (.getControl obj-b RigidBodyControl)307 (cortex.silly/world-to-local obj-a world-pivot)308 (cortex.silly/world-to-local obj-b world-pivot)309 nil310 ))312 (import com.jme3.bullet.collision.PhysicsCollisionObject)314 (defn blab-* []315 (let [hand (box 0.5 0.2 0.2 :position (Vector3f. 0 0 0)316 :mass 0 :color ColorRGBA/Green)317 finger (box 0.5 0.2 0.2 :position (Vector3f. 2.4 0 0)318 :mass 1 :color ColorRGBA/Red)319 connection-point (Vector3f. 1.2 0 0)320 root (nodify [hand finger])]322 (join-at-point hand finger (Vector3f. 1.2 0 0))324 (.setCollisionGroup325 (.getControl hand RigidBodyControl)326 PhysicsCollisionObject/COLLISION_GROUP_NONE)327 (world328 root329 standard-debug-controls330 (fn [world]331 (enable-debug world)332 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))333 (set-gravity world Vector3f/ZERO)334 )335 no-op)))336 (import java.awt.image.BufferedImage)338 (defn draw-sprite [image sprite x y color ]339 (dorun340 (for [[u v] sprite]341 (.setRGB image (+ u x) (+ v y) color))))343 (defn view-angle344 "create a debug view of an angle"345 [color]346 (let [image (BufferedImage. 50 50 BufferedImage/TYPE_INT_RGB)347 previous (atom [25 25])348 sprite [[0 0] [0 1]349 [0 -1] [-1 0] [1 0]]]350 (fn [angle]351 (let [angle (float angle)]352 (let [position353 [(+ 25 (int (* 20 (Math/cos angle))))354 (+ 25 (int (* 20(Math/sin angle))))]]355 (draw-sprite image sprite (@previous 0) (@previous 1) 0x000000)356 (draw-sprite image sprite (position 0) (position 1) color)357 (reset! previous position))358 image))))360 (defn proprioception-debug-window361 []362 (let [yaw (view-angle 0xFF0000)363 roll (view-angle 0x00FF00)364 pitch (view-angle 0xFFFFFF)365 v-yaw (view-image)366 v-roll (view-image)367 v-pitch (view-image)368 ]369 (fn [prop-data]370 (dorun371 (map372 (fn [[y r p]]373 (v-yaw (yaw y))374 (v-roll (roll r))375 (v-pitch (pitch p)))376 prop-data)))))377 (comment379 (defn proprioception-debug-window380 []381 (let [time (atom 0)]382 (fn [prop-data]383 (if (= 0 (rem (swap! time inc) 40))384 (println-repl prop-data)))))385 )387 (comment388 (dorun389 (map390 (comp391 println-repl392 (fn [[p y r]]393 (format394 "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n"395 p y r)))396 prop-data)))401 (defn test-proprioception402 "Testing proprioception:403 You should see two foating bars, and a printout of pitch, yaw, and404 roll. Pressing key-r/key-t should move the blue bar up and down and405 change only the value of pitch. key-f/key-g moves it side to side406 and changes yaw. key-v/key-b will spin the blue segment clockwise407 and counterclockwise, and only affect roll."408 []409 (let [hand (box 1 0.2 0.2 :position (Vector3f. 0 2 0)410 :mass 0 :color ColorRGBA/Green :name "hand")411 finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0)412 :mass 1 :color ColorRGBA/Red :name "finger")413 joint-node (box 0.1 0.05 0.05 :color ColorRGBA/Yellow414 :position (Vector3f. 1.2 2 0)415 :physical? false)416 joint (join-at-point hand finger (Vector3f. 1.2 2 0 ))417 creature (nodify [hand finger joint-node])418 ;; *******************************************420 floor (box 10 10 10 :position (Vector3f. 0 -15 0)421 :mass 0 :color ColorRGBA/Gray)423 root (nodify [creature floor])424 prop (joint-proprioception creature joint-node)425 prop-view (proprioception-debug-window)426 finger-control (.getControl finger RigidBodyControl)427 hand-control (.getControl hand RigidBodyControl)429 controls430 (merge standard-debug-controls431 {"key-o"432 (fn [_ _] (.setEnabled finger-control true))433 "key-p"434 (fn [_ _] (.setEnabled finger-control false))435 "key-k"436 (fn [_ _] (.setEnabled hand-control true))437 "key-l"438 (fn [_ _] (.setEnabled hand-control false))439 "key-i"440 (fn [world _] (set-gravity world (Vector3f. 0 0 0)))441 "key-period"442 (fn [world _]443 (.setEnabled finger-control false)444 (.setEnabled hand-control false)445 (.rotate creature (doto (Quaternion.)446 (.fromAngleAxis447 (float (/ Math/PI 15))448 (Vector3f. 0 0 -1))))450 (.setEnabled finger-control true)451 (.setEnabled hand-control true)452 (set-gravity world (Vector3f. 0 0 0))453 )456 }457 )459 ]460 (comment461 (.setCollisionGroup462 (.getControl hand RigidBodyControl)463 PhysicsCollisionObject/COLLISION_GROUP_NONE)464 )465 (apply466 world467 (with-movement468 hand469 ["key-y" "key-u" "key-h" "key-j" "key-n" "key-m"]470 [10 10 10 10 1 1]471 (with-movement472 finger473 ["key-r" "key-t" "key-f" "key-g" "key-v" "key-b"]474 [10 10 10 10 1 1]475 [root476 controls477 (fn [world]478 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))479 (set-gravity world (Vector3f. 0 0 0))480 (light-up-everything world))481 (fn [_ _] (prop-view (list (prop))))])))))483 #+end_src485 #+results: test-body486 : #'cortex.test.body/test-proprioception489 * COMMENT code-limbo490 #+begin_src clojure491 ;;(.loadModel492 ;; (doto (asset-manager)493 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))494 ;; "Models/person/person.blend")497 (defn load-blender-model498 "Load a .blend file using an asset folder relative path."499 [^String model]500 (.loadModel501 (doto (asset-manager)502 (.registerLoader BlenderModelLoader (into-array String ["blend"])))503 model))506 (defn view-model [^String model]507 (view508 (.loadModel509 (doto (asset-manager)510 (.registerLoader BlenderModelLoader (into-array String ["blend"])))511 model)))513 (defn load-blender-scene [^String model]514 (.loadModel515 (doto (asset-manager)516 (.registerLoader BlenderLoader (into-array String ["blend"])))517 model))519 (defn worm520 []521 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))523 (defn oto524 []525 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))527 (defn sinbad528 []529 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))531 (defn worm-blender532 []533 (first (seq (.getChildren (load-blender-model534 "Models/anim2/simple-worm.blend")))))536 (defn body537 "given a node with a SkeletonControl, will produce a body sutiable538 for AI control with movement and proprioception."539 [node]540 (let [skeleton-control (.getControl node SkeletonControl)541 krc (KinematicRagdollControl.)]542 (comment543 (dorun544 (map #(.addBoneName krc %)545 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]546 ;;"mid2" "mid3" "tail" "head"]547 )))548 (.addControl node krc)549 (.setRagdollMode krc)550 )551 node552 )553 (defn show-skeleton [node]554 (let [sd556 (doto557 (SkeletonDebugger. "aurellem-skel-debug"558 (skel node))559 (.setMaterial (green-x-ray)))]560 (.attachChild node sd)561 node))565 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;567 ;; this could be a good way to give objects special properties like568 ;; being eyes and the like570 (.getUserData571 (.getChild572 (load-blender-model "Models/property/test.blend") 0)573 "properties")575 ;; the properties are saved along with the blender file.576 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;581 (defn init-debug-skel-node582 [f debug-node skeleton]583 (let [bones584 (map #(.getBone skeleton %)585 (range (.getBoneCount skeleton)))]586 (dorun (map #(.setUserControl % true) bones))587 (dorun (map (fn [b]588 (println (.getName b)589 " -- " (f b)))590 bones))591 (dorun592 (map #(.attachChild593 debug-node594 (doto595 (sphere 0.1596 :position (f %)597 :physical? false)598 (.setMaterial (green-x-ray))))599 bones)))600 debug-node)602 (import jme3test.bullet.PhysicsTestHelper)605 (defn test-zzz [the-worm world value]606 (if (not value)607 (let [skeleton (skel the-worm)]608 (println-repl "enabling bones")609 (dorun610 (map611 #(.setUserControl (.getBone skeleton %) true)612 (range (.getBoneCount skeleton))))615 (let [b (.getBone skeleton 2)]616 (println-repl "moving " (.getName b))617 (println-repl (.getLocalPosition b))618 (.setUserTransforms b619 Vector3f/UNIT_X620 Quaternion/IDENTITY621 ;;(doto (Quaternion.)622 ;; (.fromAngles (/ Math/PI 2)623 ;; 0624 ;; 0626 (Vector3f. 1 1 1))627 )629 (println-repl "hi! <3"))))632 (defn test-ragdoll []634 (let [the-worm636 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")637 (doto (show-skeleton (worm-blender))638 (.setLocalTranslation (Vector3f. 0 10 0))639 ;;(worm)640 ;;(oto)641 ;;(sinbad)642 )643 ]646 (.start647 (world648 (doto (Node.)649 (.attachChild the-worm))650 {"key-return" (fire-cannon-ball)651 "key-space" (partial test-zzz the-worm)652 }653 (fn [world]654 (light-up-everything world)655 (PhysicsTestHelper/createPhysicsTestWorld656 (.getRootNode world)657 (asset-manager)658 (.getPhysicsSpace659 (.getState (.getStateManager world) BulletAppState)))660 (set-gravity world Vector3f/ZERO)661 ;;(.setTimer world (NanoTimer.))662 ;;(org.lwjgl.input.Mouse/setGrabbed false)663 )664 no-op665 )668 )))671 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;672 ;;; here is the ragdoll stuff674 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))675 (def mesh worm-mesh)677 (.getFloatBuffer mesh VertexBuffer$Type/Position)678 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)679 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))682 (defn position [index]683 (.get684 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)685 index))687 (defn bones [index]688 (.get689 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))690 index))692 (defn bone-weights [index]693 (.get694 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)695 index))699 (defn vertex-bones [vertex]700 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))702 (defn vertex-weights [vertex]703 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))705 (defn vertex-position [index]706 (let [offset (* index 3)]707 (Vector3f. (position offset)708 (position (inc offset))709 (position (inc(inc offset))))))711 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))713 (defn bone-control-color [index]714 (get {[1 0 0 0] ColorRGBA/Red715 [1 2 0 0] ColorRGBA/Magenta716 [2 0 0 0] ColorRGBA/Blue}717 (vertex-bones index)718 ColorRGBA/White))720 (defn influence-color [index bone-num]721 (get722 {(float 0) ColorRGBA/Blue723 (float 0.5) ColorRGBA/Green724 (float 1) ColorRGBA/Red}725 ;; find the weight of the desired bone726 ((zipmap (vertex-bones index)(vertex-weights index))727 bone-num)728 ColorRGBA/Blue))730 (def worm-vertices (set (map vertex-info (range 60))))733 (defn test-info []734 (let [points (Node.)]735 (dorun736 (map #(.attachChild points %)737 (map #(sphere 0.01738 :position (vertex-position %)739 :color (influence-color % 1)740 :physical? false)741 (range 60))))742 (view points)))745 (defrecord JointControl [joint physics-space]746 PhysicsControl747 (setPhysicsSpace [this space]748 (dosync749 (ref-set (:physics-space this) space))750 (.addJoint space (:joint this)))751 (update [this tpf])752 (setSpatial [this spatial])753 (render [this rm vp])754 (getPhysicsSpace [this] (deref (:physics-space this)))755 (isEnabled [this] true)756 (setEnabled [this state]))758 (defn add-joint759 "Add a joint to a particular object. When the object is added to the760 PhysicsSpace of a simulation, the joint will also be added"761 [object joint]762 (let [control (JointControl. joint (ref nil))]763 (.addControl object control))764 object)767 (defn hinge-world768 []769 (let [sphere1 (sphere)770 sphere2 (sphere 1 :position (Vector3f. 3 3 3))771 joint (Point2PointJoint.772 (.getControl sphere1 RigidBodyControl)773 (.getControl sphere2 RigidBodyControl)774 Vector3f/ZERO (Vector3f. 3 3 3))]775 (add-joint sphere1 joint)776 (doto (Node. "hinge-world")777 (.attachChild sphere1)778 (.attachChild sphere2))))781 (defn test-joint []782 (view (hinge-world)))784 ;; (defn copier-gen []785 ;; (let [count (atom 0)]786 ;; (fn [in]787 ;; (swap! count inc)788 ;; (clojure.contrib.duck-streams/copy789 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"790 ;; ;;/home/r/tmp/mao-test/clojure-images791 ;; (format "%08d.png" @count)))))))792 ;; (defn decrease-framerate []793 ;; (map794 ;; (copier-gen)795 ;; (sort796 ;; (map first797 ;; (partition798 ;; 4799 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))800 ;; (file-seq801 ;; (file-str802 ;; "/home/r/media/anime/mao-temp/images"))))))))806 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;808 (defn proprioception809 "Create a proprioception map that reports the rotations of the810 various limbs of the creature's body"811 [creature]812 [#^Node creature]813 (let [814 nodes (node-seq creature)815 joints816 (map817 :joint818 (filter819 #(isa? (class %) JointControl)820 (reduce821 concat822 (map (fn [node]823 (map (fn [num] (.getControl node num))824 (range (.getNumControls node))))825 nodes))))]826 (fn []827 (reduce concat (map relative-positions (list (first joints)))))))830 (defn skel [node]831 (doto832 (.getSkeleton833 (.getControl node SkeletonControl))834 ;; this is necessary to force the skeleton to have accurate world835 ;; transforms before it is rendered to the screen.836 (.resetAndUpdate)))838 (defn green-x-ray []839 (doto (Material. (asset-manager)840 "Common/MatDefs/Misc/Unshaded.j3md")841 (.setColor "Color" ColorRGBA/Green)842 (-> (.getAdditionalRenderState)843 (.setDepthTest false))))845 (defn test-worm []846 (.start847 (world848 (doto (Node.)849 ;;(.attachChild (point-worm))850 (.attachChild (load-blender-model851 "Models/anim2/joint-worm.blend"))853 (.attachChild (box 10 1 10854 :position (Vector3f. 0 -2 0) :mass 0855 :color (ColorRGBA/Gray))))856 {857 "key-space" (fire-cannon-ball)858 }859 (fn [world]860 (enable-debug world)861 (light-up-everything world)862 ;;(.setTimer world (NanoTimer.))863 )864 no-op)))868 ;; defunct movement stuff869 (defn torque-controls [control]870 (let [torques871 (concat872 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))873 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))874 [Vector3f/UNIT_X])]875 (map (fn [torque-axis]876 (fn [torque]877 (.applyTorque878 control879 (.mult (.mult (.getPhysicsRotation control)880 torque-axis)881 (float882 (* (.getMass control) torque))))))883 torques)))885 (defn motor-map886 "Take a creature and generate a function that will enable fine887 grained control over all the creature's limbs."888 [#^Node creature]889 (let [controls (keep #(.getControl % RigidBodyControl)890 (node-seq creature))891 limb-controls (reduce concat (map torque-controls controls))892 body-control (partial map #(%1 %2) limb-controls)]893 body-control))895 (defn test-motor-map896 "see how torque works."897 []898 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)899 :mass 1 :color ColorRGBA/Green)900 motor-map (motor-map finger)]901 (world902 (nodify [finger903 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0904 :color ColorRGBA/Gray)])905 standard-debug-controls906 (fn [world]907 (set-gravity world Vector3f/ZERO)908 (light-up-everything world)909 (.setTimer world (NanoTimer.)))910 (fn [_ _]911 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))912 #+end_src920 * COMMENT generate Source921 #+begin_src clojure :tangle ../src/cortex/body.clj922 <<proprioception>>923 <<motor-control>>924 #+end_src926 #+begin_src clojure :tangle ../src/cortex/test/body.clj927 <<test-body>>928 #+end_src