Mercurial > cortex
view org/body.org @ 142:ccd057319c2a
improved proprioception test
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 02 Feb 2012 02:03:19 -0700 |
parents | 22e193b5c60f |
children | 48f9cba082eb |
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 (println-repl "x:" x)74 (println-repl "y:" y)75 (println-repl "z:" z)76 ;; this function will report proprioceptive information for the77 ;; joint.78 (fn []79 ;; x is the "twist" axis, y and z are the "bend" axes80 (let [rot-a (.getWorldRotation obj-a)81 ;;inv-a (.inverse rot-a)82 rot-b (.getWorldRotation obj-b)83 ;;relative (.mult rot-b inv-a)84 basis (doto (Matrix3f.)85 (.setColumn 0 (.mult rot-a x))86 (.setColumn 1 (.mult rot-a y))87 (.setColumn 2 (.mult rot-a z)))88 rotation-about-joint89 (doto (Quaternion.)90 (.fromRotationMatrix91 (.mult (.invert basis)92 (.toRotationMatrix rot-b))))93 [yaw roll pitch]94 (seq (.toAngles rotation-about-joint nil))]95 ;;return euler angles of the quaternion around the new basis96 [yaw roll pitch]97 ))))100 (defn proprioception101 "Create a function that provides proprioceptive information about an102 entire body."103 [#^Node creature]104 ;; extract the body's joints105 (let [joints (creature-joints creature)106 senses (map (partial joint-proprioception creature) joints)]107 (fn []108 (map #(%) senses))))110 (defn tap [obj direction force]111 (let [control (.getControl obj RigidBodyControl)]112 (.applyTorque113 control114 (.mult (.getPhysicsRotation control)115 (.mult (.normalize direction) (float force))))))118 (defn with-movement119 [object120 [up down left right roll-up roll-down :as keyboard]121 forces122 [root-node123 keymap124 intilization125 world-loop]]126 (let [add-keypress127 (fn [state keymap key]128 (merge keymap129 {key130 (fn [_ pressed?]131 (reset! state pressed?))}))132 move-up? (atom false)133 move-down? (atom false)134 move-left? (atom false)135 move-right? (atom false)136 roll-left? (atom false)137 roll-right? (atom false)139 directions [(Vector3f. 0 1 0)(Vector3f. 0 -1 0)140 (Vector3f. 0 0 1)(Vector3f. 0 0 -1)141 (Vector3f. -1 0 0)(Vector3f. 1 0 0)]142 atoms [move-left? move-right? move-up? move-down?143 roll-left? roll-right?]145 keymap* (reduce merge146 (map #(add-keypress %1 keymap %2)147 atoms148 keyboard))150 splice-loop (fn []151 (dorun152 (map153 (fn [sym direction force]154 (if @sym155 (tap object direction force)))156 atoms directions forces)))158 world-loop* (fn [world tpf]159 (world-loop world tpf)160 (splice-loop))]162 [root-node163 keymap*164 intilization165 world-loop*]))168 #+end_src170 #+results: proprioception171 : #'cortex.body/proprioception173 * Motor Control174 #+name: motor-control175 #+begin_src clojure176 (in-ns 'cortex.body)178 ;; surprisingly enough, terristerial creatures only move by using179 ;; torque applied about their joints. There's not a single straight180 ;; line of force in the human body at all! (A straight line of force181 ;; would correspond to some sort of jet or rocket propulseion.)183 (defn vector-motor-control184 "Create a function that accepts a sequence of Vector3f objects that185 describe the torque to be applied to each part of the body."186 [body]187 (let [nodes (node-seq body)188 controls (keep #(.getControl % RigidBodyControl) nodes)]189 (fn [torques]190 (map #(.applyTorque %1 %2)191 controls torques))))192 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;193 #+end_src195 ## note -- might want to add a lower dimensional, discrete version of196 ## this if it proves useful from a x-modal clustering perspective.198 * Examples200 #+name: test-body201 #+begin_src clojure202 (ns cortex.test.body203 (:use (cortex world util body))204 (:require cortex.silly)205 (:import206 com.jme3.math.Vector3f207 com.jme3.math.ColorRGBA208 com.jme3.bullet.joints.Point2PointJoint209 com.jme3.bullet.control.RigidBodyControl210 com.jme3.system.NanoTimer))212 (defn worm-segments213 "Create multiple evenly spaced box segments. They're fabulous!"214 [segment-length num-segments interstitial-space radius]215 (letfn [(nth-segment216 [n]217 (box segment-length radius radius :mass 0.1218 :position219 (Vector3f.220 (* 2 n (+ interstitial-space segment-length)) 0 0)221 :name (str "worm-segment" n)222 :color (ColorRGBA/randomColor)))]223 (map nth-segment (range num-segments))))225 (defn connect-at-midpoint226 "Connect two physics objects with a Point2Point joint constraint at227 the point equidistant from both objects' centers."228 [segmentA segmentB]229 (let [centerA (.getWorldTranslation segmentA)230 centerB (.getWorldTranslation segmentB)231 midpoint (.mult (.add centerA centerB) (float 0.5))232 pivotA (.subtract midpoint centerA)233 pivotB (.subtract midpoint centerB)235 ;; A side-effect of creating a joint registers236 ;; it with both physics objects which in turn237 ;; will register the joint with the physics system238 ;; when the simulation is started.239 joint (Point2PointJoint.240 (.getControl segmentA RigidBodyControl)241 (.getControl segmentB RigidBodyControl)242 pivotA243 pivotB)]244 segmentB))246 (defn eve-worm247 "Create a worm-like body bound by invisible joint constraints."248 []249 (let [segments (worm-segments 0.2 5 0.1 0.1)]250 (dorun (map (partial apply connect-at-midpoint)251 (partition 2 1 segments)))252 (nodify "worm" segments)))254 (defn worm-pattern255 "This is a simple, mindless motor control pattern that drives the256 second segment of the worm's body at an offset angle with257 sinusoidally varying strength."258 [time]259 (let [angle (* Math/PI (/ 9 20))260 direction (Vector3f. 0 (Math/sin angle) (Math/cos angle))]261 [Vector3f/ZERO262 (.mult263 direction264 (float (* 2 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))))265 Vector3f/ZERO266 Vector3f/ZERO267 Vector3f/ZERO]))269 (defn test-motor-control270 "Testing motor-control:271 You should see a multi-segmented worm-like object fall onto the272 table and begin writhing and moving."273 []274 (let [worm (eve-worm)275 time (atom 0)276 worm-motor-map (vector-motor-control worm)]277 (world278 (nodify [worm279 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0280 :color ColorRGBA/Gray)])281 standard-debug-controls282 (fn [world]283 (enable-debug world)284 (light-up-everything world)285 (comment286 (com.aurellem.capture.Capture/captureVideo287 world288 (file-str "/home/r/proj/cortex/tmp/moving-worm")))289 )291 (fn [_ _]292 (swap! time inc)293 (Thread/sleep 20)294 (dorun (worm-motor-map295 (worm-pattern @time)))))))299 (defn join-at-point [obj-a obj-b world-pivot]300 (cortex.silly/joint-dispatch301 {:type :point}302 (.getControl obj-a RigidBodyControl)303 (.getControl obj-b RigidBodyControl)304 (cortex.silly/world-to-local obj-a world-pivot)305 (cortex.silly/world-to-local obj-b world-pivot)306 nil307 ))309 (import com.jme3.bullet.collision.PhysicsCollisionObject)311 (defn blab-* []312 (let [hand (box 0.5 0.2 0.2 :position (Vector3f. 0 0 0)313 :mass 0 :color ColorRGBA/Green)314 finger (box 0.5 0.2 0.2 :position (Vector3f. 2.4 0 0)315 :mass 1 :color ColorRGBA/Red)316 connection-point (Vector3f. 1.2 0 0)317 root (nodify [hand finger])]319 (join-at-point hand finger (Vector3f. 1.2 0 0))321 (.setCollisionGroup322 (.getControl hand RigidBodyControl)323 PhysicsCollisionObject/COLLISION_GROUP_NONE)324 (world325 root326 standard-debug-controls327 (fn [world]328 (enable-debug world)329 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))330 (set-gravity world Vector3f/ZERO)331 )332 no-op)))333 (import java.awt.image.BufferedImage)335 (defn draw-sprite [image sprite x y color ]336 (dorun337 (for [[u v] sprite]338 (.setRGB image (+ u x) (+ v y) color))))340 (defn view-angle341 "create a debug view of an angle"342 [color]343 (let [image (BufferedImage. 50 50 BufferedImage/TYPE_INT_RGB)344 previous (atom [25 25])345 sprite [[0 0] [0 1]346 [0 -1] [-1 0] [1 0]]]347 (fn [angle]348 (let [angle (float angle)]349 (let [position350 [(+ 25 (int (* 20 (Math/cos angle))))351 (+ 25 (int (* 20(Math/sin angle))))]]352 (draw-sprite image sprite (@previous 0) (@previous 1) 0x000000)353 (draw-sprite image sprite (position 0) (position 1) color)354 (reset! previous position))355 image))))357 (defn proprioception-debug-window358 []359 (let [yaw (view-angle 0xFF0000)360 roll (view-angle 0x00FF00)361 pitch (view-angle 0xFFFFFF)362 v-yaw (view-image)363 v-roll (view-image)364 v-pitch (view-image)365 ]366 (fn [prop-data]367 (dorun368 (map369 (fn [[y r p]]370 (v-yaw (yaw y))371 (v-roll (roll r))372 (v-pitch (pitch p)))373 prop-data)))))374 (comment376 (defn proprioception-debug-window377 []378 (let [time (atom 0)]379 (fn [prop-data]380 (if (= 0 (rem (swap! time inc) 40))381 (println-repl prop-data)))))382 )384 (comment385 (dorun386 (map387 (comp388 println-repl389 (fn [[p y r]]390 (format391 "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n"392 p y r)))393 prop-data)))398 (defn test-proprioception399 "Testing proprioception:400 You should see two foating bars, and a printout of pitch, yaw, and401 roll. Pressing key-r/key-t should move the blue bar up and down and402 change only the value of pitch. key-f/key-g moves it side to side403 and changes yaw. key-v/key-b will spin the blue segment clockwise404 and counterclockwise, and only affect roll."405 []406 (let [hand (box 1 0.2 0.2 :position (Vector3f. 0 2 0)407 :mass 0 :color ColorRGBA/Green :name "hand")408 finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0)409 :mass 1 :color ColorRGBA/Red :name "finger")410 joint-node (box 0.1 0.05 0.05 :color ColorRGBA/Yellow411 :position (Vector3f. 1.2 2 0)412 :physical? false)413 joint (join-at-point hand finger (Vector3f. 1.2 2 0 ))414 creature (nodify [hand finger joint-node])415 ;; *******************************************417 floor (box 10 10 10 :position (Vector3f. 0 -15 0)418 :mass 0 :color ColorRGBA/Gray)420 root (nodify [creature floor])421 prop (joint-proprioception creature joint-node)422 prop-view (proprioception-debug-window)423 finger-control (.getControl finger RigidBodyControl)424 hand-control (.getControl hand RigidBodyControl)426 controls427 (merge standard-debug-controls428 {"key-o"429 (fn [_ _] (.setEnabled finger-control true))430 "key-p"431 (fn [_ _] (.setEnabled finger-control false))432 "key-k"433 (fn [_ _] (.setEnabled hand-control true))434 "key-l"435 (fn [_ _] (.setEnabled hand-control false))436 "key-i"437 (fn [world _] (set-gravity world (Vector3f. 0 0 0)))438 "key-period"439 (fn [world _]440 (.setEnabled finger-control false)441 (.setEnabled hand-control false)442 (.rotate creature (doto (Quaternion.)443 (.fromAngleAxis444 (float (/ Math/PI 15))445 (Vector3f. 0 0 -1))))447 (.setEnabled finger-control true)448 (.setEnabled hand-control true)449 (set-gravity world (Vector3f. 0 0 0))450 )453 }454 )456 ]457 (comment458 (.setCollisionGroup459 (.getControl hand RigidBodyControl)460 PhysicsCollisionObject/COLLISION_GROUP_NONE)461 )462 (apply463 world464 (with-movement465 hand466 ["key-y" "key-u" "key-h" "key-j" "key-n" "key-m"]467 [10 10 10 10 1 1]468 (with-movement469 finger470 ["key-r" "key-t" "key-f" "key-g" "key-v" "key-b"]471 [10 10 10 10 1 1]472 [root473 controls474 (fn [world]475 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))476 (set-gravity world (Vector3f. 0 0 0))477 (light-up-everything world))478 (fn [_ _] (prop-view (list (prop))))])))))480 #+end_src482 #+results: test-body483 : #'cortex.test.body/test-proprioception486 * COMMENT code-limbo487 #+begin_src clojure488 ;;(.loadModel489 ;; (doto (asset-manager)490 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))491 ;; "Models/person/person.blend")494 (defn load-blender-model495 "Load a .blend file using an asset folder relative path."496 [^String model]497 (.loadModel498 (doto (asset-manager)499 (.registerLoader BlenderModelLoader (into-array String ["blend"])))500 model))503 (defn view-model [^String model]504 (view505 (.loadModel506 (doto (asset-manager)507 (.registerLoader BlenderModelLoader (into-array String ["blend"])))508 model)))510 (defn load-blender-scene [^String model]511 (.loadModel512 (doto (asset-manager)513 (.registerLoader BlenderLoader (into-array String ["blend"])))514 model))516 (defn worm517 []518 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))520 (defn oto521 []522 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))524 (defn sinbad525 []526 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))528 (defn worm-blender529 []530 (first (seq (.getChildren (load-blender-model531 "Models/anim2/simple-worm.blend")))))533 (defn body534 "given a node with a SkeletonControl, will produce a body sutiable535 for AI control with movement and proprioception."536 [node]537 (let [skeleton-control (.getControl node SkeletonControl)538 krc (KinematicRagdollControl.)]539 (comment540 (dorun541 (map #(.addBoneName krc %)542 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]543 ;;"mid2" "mid3" "tail" "head"]544 )))545 (.addControl node krc)546 (.setRagdollMode krc)547 )548 node549 )550 (defn show-skeleton [node]551 (let [sd553 (doto554 (SkeletonDebugger. "aurellem-skel-debug"555 (skel node))556 (.setMaterial (green-x-ray)))]557 (.attachChild node sd)558 node))562 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;564 ;; this could be a good way to give objects special properties like565 ;; being eyes and the like567 (.getUserData568 (.getChild569 (load-blender-model "Models/property/test.blend") 0)570 "properties")572 ;; the properties are saved along with the blender file.573 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;578 (defn init-debug-skel-node579 [f debug-node skeleton]580 (let [bones581 (map #(.getBone skeleton %)582 (range (.getBoneCount skeleton)))]583 (dorun (map #(.setUserControl % true) bones))584 (dorun (map (fn [b]585 (println (.getName b)586 " -- " (f b)))587 bones))588 (dorun589 (map #(.attachChild590 debug-node591 (doto592 (sphere 0.1593 :position (f %)594 :physical? false)595 (.setMaterial (green-x-ray))))596 bones)))597 debug-node)599 (import jme3test.bullet.PhysicsTestHelper)602 (defn test-zzz [the-worm world value]603 (if (not value)604 (let [skeleton (skel the-worm)]605 (println-repl "enabling bones")606 (dorun607 (map608 #(.setUserControl (.getBone skeleton %) true)609 (range (.getBoneCount skeleton))))612 (let [b (.getBone skeleton 2)]613 (println-repl "moving " (.getName b))614 (println-repl (.getLocalPosition b))615 (.setUserTransforms b616 Vector3f/UNIT_X617 Quaternion/IDENTITY618 ;;(doto (Quaternion.)619 ;; (.fromAngles (/ Math/PI 2)620 ;; 0621 ;; 0623 (Vector3f. 1 1 1))624 )626 (println-repl "hi! <3"))))629 (defn test-ragdoll []631 (let [the-worm633 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")634 (doto (show-skeleton (worm-blender))635 (.setLocalTranslation (Vector3f. 0 10 0))636 ;;(worm)637 ;;(oto)638 ;;(sinbad)639 )640 ]643 (.start644 (world645 (doto (Node.)646 (.attachChild the-worm))647 {"key-return" (fire-cannon-ball)648 "key-space" (partial test-zzz the-worm)649 }650 (fn [world]651 (light-up-everything world)652 (PhysicsTestHelper/createPhysicsTestWorld653 (.getRootNode world)654 (asset-manager)655 (.getPhysicsSpace656 (.getState (.getStateManager world) BulletAppState)))657 (set-gravity world Vector3f/ZERO)658 ;;(.setTimer world (NanoTimer.))659 ;;(org.lwjgl.input.Mouse/setGrabbed false)660 )661 no-op662 )665 )))668 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;669 ;;; here is the ragdoll stuff671 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))672 (def mesh worm-mesh)674 (.getFloatBuffer mesh VertexBuffer$Type/Position)675 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)676 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))679 (defn position [index]680 (.get681 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)682 index))684 (defn bones [index]685 (.get686 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))687 index))689 (defn bone-weights [index]690 (.get691 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)692 index))696 (defn vertex-bones [vertex]697 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))699 (defn vertex-weights [vertex]700 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))702 (defn vertex-position [index]703 (let [offset (* index 3)]704 (Vector3f. (position offset)705 (position (inc offset))706 (position (inc(inc offset))))))708 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))710 (defn bone-control-color [index]711 (get {[1 0 0 0] ColorRGBA/Red712 [1 2 0 0] ColorRGBA/Magenta713 [2 0 0 0] ColorRGBA/Blue}714 (vertex-bones index)715 ColorRGBA/White))717 (defn influence-color [index bone-num]718 (get719 {(float 0) ColorRGBA/Blue720 (float 0.5) ColorRGBA/Green721 (float 1) ColorRGBA/Red}722 ;; find the weight of the desired bone723 ((zipmap (vertex-bones index)(vertex-weights index))724 bone-num)725 ColorRGBA/Blue))727 (def worm-vertices (set (map vertex-info (range 60))))730 (defn test-info []731 (let [points (Node.)]732 (dorun733 (map #(.attachChild points %)734 (map #(sphere 0.01735 :position (vertex-position %)736 :color (influence-color % 1)737 :physical? false)738 (range 60))))739 (view points)))742 (defrecord JointControl [joint physics-space]743 PhysicsControl744 (setPhysicsSpace [this space]745 (dosync746 (ref-set (:physics-space this) space))747 (.addJoint space (:joint this)))748 (update [this tpf])749 (setSpatial [this spatial])750 (render [this rm vp])751 (getPhysicsSpace [this] (deref (:physics-space this)))752 (isEnabled [this] true)753 (setEnabled [this state]))755 (defn add-joint756 "Add a joint to a particular object. When the object is added to the757 PhysicsSpace of a simulation, the joint will also be added"758 [object joint]759 (let [control (JointControl. joint (ref nil))]760 (.addControl object control))761 object)764 (defn hinge-world765 []766 (let [sphere1 (sphere)767 sphere2 (sphere 1 :position (Vector3f. 3 3 3))768 joint (Point2PointJoint.769 (.getControl sphere1 RigidBodyControl)770 (.getControl sphere2 RigidBodyControl)771 Vector3f/ZERO (Vector3f. 3 3 3))]772 (add-joint sphere1 joint)773 (doto (Node. "hinge-world")774 (.attachChild sphere1)775 (.attachChild sphere2))))778 (defn test-joint []779 (view (hinge-world)))781 ;; (defn copier-gen []782 ;; (let [count (atom 0)]783 ;; (fn [in]784 ;; (swap! count inc)785 ;; (clojure.contrib.duck-streams/copy786 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"787 ;; ;;/home/r/tmp/mao-test/clojure-images788 ;; (format "%08d.png" @count)))))))789 ;; (defn decrease-framerate []790 ;; (map791 ;; (copier-gen)792 ;; (sort793 ;; (map first794 ;; (partition795 ;; 4796 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))797 ;; (file-seq798 ;; (file-str799 ;; "/home/r/media/anime/mao-temp/images"))))))))803 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;805 (defn proprioception806 "Create a proprioception map that reports the rotations of the807 various limbs of the creature's body"808 [creature]809 [#^Node creature]810 (let [811 nodes (node-seq creature)812 joints813 (map814 :joint815 (filter816 #(isa? (class %) JointControl)817 (reduce818 concat819 (map (fn [node]820 (map (fn [num] (.getControl node num))821 (range (.getNumControls node))))822 nodes))))]823 (fn []824 (reduce concat (map relative-positions (list (first joints)))))))827 (defn skel [node]828 (doto829 (.getSkeleton830 (.getControl node SkeletonControl))831 ;; this is necessary to force the skeleton to have accurate world832 ;; transforms before it is rendered to the screen.833 (.resetAndUpdate)))835 (defn green-x-ray []836 (doto (Material. (asset-manager)837 "Common/MatDefs/Misc/Unshaded.j3md")838 (.setColor "Color" ColorRGBA/Green)839 (-> (.getAdditionalRenderState)840 (.setDepthTest false))))842 (defn test-worm []843 (.start844 (world845 (doto (Node.)846 ;;(.attachChild (point-worm))847 (.attachChild (load-blender-model848 "Models/anim2/joint-worm.blend"))850 (.attachChild (box 10 1 10851 :position (Vector3f. 0 -2 0) :mass 0852 :color (ColorRGBA/Gray))))853 {854 "key-space" (fire-cannon-ball)855 }856 (fn [world]857 (enable-debug world)858 (light-up-everything world)859 ;;(.setTimer world (NanoTimer.))860 )861 no-op)))865 ;; defunct movement stuff866 (defn torque-controls [control]867 (let [torques868 (concat869 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))870 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))871 [Vector3f/UNIT_X])]872 (map (fn [torque-axis]873 (fn [torque]874 (.applyTorque875 control876 (.mult (.mult (.getPhysicsRotation control)877 torque-axis)878 (float879 (* (.getMass control) torque))))))880 torques)))882 (defn motor-map883 "Take a creature and generate a function that will enable fine884 grained control over all the creature's limbs."885 [#^Node creature]886 (let [controls (keep #(.getControl % RigidBodyControl)887 (node-seq creature))888 limb-controls (reduce concat (map torque-controls controls))889 body-control (partial map #(%1 %2) limb-controls)]890 body-control))892 (defn test-motor-map893 "see how torque works."894 []895 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)896 :mass 1 :color ColorRGBA/Green)897 motor-map (motor-map finger)]898 (world899 (nodify [finger900 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0901 :color ColorRGBA/Gray)])902 standard-debug-controls903 (fn [world]904 (set-gravity world Vector3f/ZERO)905 (light-up-everything world)906 (.setTimer world (NanoTimer.)))907 (fn [_ _]908 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))909 #+end_src917 * COMMENT generate Source918 #+begin_src clojure :tangle ../src/cortex/body.clj919 <<proprioception>>920 <<motor-control>>921 #+end_src923 #+begin_src clojure :tangle ../src/cortex/test/body.clj924 <<test-body>>925 #+end_src