Mercurial > cortex
view org/body.org @ 139:ffbab4199c0d
going to de-macroify debug code
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 02 Feb 2012 00:57:18 -0700 |
parents | 16bdf9e80daf |
children | 22444eb20ecc |
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 #+end_src112 #+results: proprioception113 : #'cortex.body/proprioception115 * Motor Control116 #+name: motor-control117 #+begin_src clojure118 (in-ns 'cortex.body)120 ;; surprisingly enough, terristerial creatures only move by using121 ;; torque applied about their joints. There's not a single straight122 ;; line of force in the human body at all! (A straight line of force123 ;; would correspond to some sort of jet or rocket propulseion.)125 (defn vector-motor-control126 "Create a function that accepts a sequence of Vector3f objects that127 describe the torque to be applied to each part of the body."128 [body]129 (let [nodes (node-seq body)130 controls (keep #(.getControl % RigidBodyControl) nodes)]131 (fn [torques]132 (map #(.applyTorque %1 %2)133 controls torques))))134 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;135 #+end_src137 ## note -- might want to add a lower dimensional, discrete version of138 ## this if it proves useful from a x-modal clustering perspective.140 * Examples142 #+name: test-body143 #+begin_src clojure144 (ns cortex.test.body145 (:use (cortex world util body))146 (:require cortex.silly)147 (:import148 com.jme3.math.Vector3f149 com.jme3.math.ColorRGBA150 com.jme3.bullet.joints.Point2PointJoint151 com.jme3.bullet.control.RigidBodyControl152 com.jme3.system.NanoTimer))154 (defn worm-segments155 "Create multiple evenly spaced box segments. They're fabulous!"156 [segment-length num-segments interstitial-space radius]157 (letfn [(nth-segment158 [n]159 (box segment-length radius radius :mass 0.1160 :position161 (Vector3f.162 (* 2 n (+ interstitial-space segment-length)) 0 0)163 :name (str "worm-segment" n)164 :color (ColorRGBA/randomColor)))]165 (map nth-segment (range num-segments))))167 (defn connect-at-midpoint168 "Connect two physics objects with a Point2Point joint constraint at169 the point equidistant from both objects' centers."170 [segmentA segmentB]171 (let [centerA (.getWorldTranslation segmentA)172 centerB (.getWorldTranslation segmentB)173 midpoint (.mult (.add centerA centerB) (float 0.5))174 pivotA (.subtract midpoint centerA)175 pivotB (.subtract midpoint centerB)177 ;; A side-effect of creating a joint registers178 ;; it with both physics objects which in turn179 ;; will register the joint with the physics system180 ;; when the simulation is started.181 joint (Point2PointJoint.182 (.getControl segmentA RigidBodyControl)183 (.getControl segmentB RigidBodyControl)184 pivotA185 pivotB)]186 segmentB))188 (defn eve-worm189 "Create a worm-like body bound by invisible joint constraints."190 []191 (let [segments (worm-segments 0.2 5 0.1 0.1)]192 (dorun (map (partial apply connect-at-midpoint)193 (partition 2 1 segments)))194 (nodify "worm" segments)))196 (defn worm-pattern197 "This is a simple, mindless motor control pattern that drives the198 second segment of the worm's body at an offset angle with199 sinusoidally varying strength."200 [time]201 (let [angle (* Math/PI (/ 9 20))202 direction (Vector3f. 0 (Math/sin angle) (Math/cos angle))]203 [Vector3f/ZERO204 (.mult205 direction206 (float (* 2 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))))207 Vector3f/ZERO208 Vector3f/ZERO209 Vector3f/ZERO]))211 (defn test-motor-control212 "Testing motor-control:213 You should see a multi-segmented worm-like object fall onto the214 table and begin writhing and moving."215 []216 (let [worm (eve-worm)217 time (atom 0)218 worm-motor-map (vector-motor-control worm)]219 (world220 (nodify [worm221 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0222 :color ColorRGBA/Gray)])223 standard-debug-controls224 (fn [world]225 (enable-debug world)226 (light-up-everything world)227 (comment228 (com.aurellem.capture.Capture/captureVideo229 world230 (file-str "/home/r/proj/cortex/tmp/moving-worm")))231 )233 (fn [_ _]234 (swap! time inc)235 (Thread/sleep 20)236 (dorun (worm-motor-map237 (worm-pattern @time)))))))241 (defn join-at-point [obj-a obj-b world-pivot]242 (cortex.silly/joint-dispatch243 {:type :point}244 (.getControl obj-a RigidBodyControl)245 (.getControl obj-b RigidBodyControl)246 (cortex.silly/world-to-local obj-a world-pivot)247 (cortex.silly/world-to-local obj-b world-pivot)248 nil249 ))251 (import com.jme3.bullet.collision.PhysicsCollisionObject)253 (defn blab-* []254 (let [hand (box 0.5 0.2 0.2 :position (Vector3f. 0 0 0)255 :mass 0 :color ColorRGBA/Green)256 finger (box 0.5 0.2 0.2 :position (Vector3f. 2.4 0 0)257 :mass 1 :color ColorRGBA/Red)258 connection-point (Vector3f. 1.2 0 0)259 root (nodify [hand finger])]261 (join-at-point hand finger (Vector3f. 1.2 0 0))263 (.setCollisionGroup264 (.getControl hand RigidBodyControl)265 PhysicsCollisionObject/COLLISION_GROUP_NONE)266 (world267 root268 standard-debug-controls269 (fn [world]270 (enable-debug world)271 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))272 (set-gravity world Vector3f/ZERO)273 )274 no-op)))275 (import java.awt.image.BufferedImage)277 (defn draw-sprite [image sprite x y color ]278 (dorun279 (for [[u v] sprite]280 (.setRGB image (+ u x) (+ v y) color))))282 (defn view-angle283 "create a debug view of an angle"284 [color]285 (let [image (BufferedImage. 50 50 BufferedImage/TYPE_INT_RGB)286 previous (atom [25 25])287 sprite [[0 0] [0 1]288 [0 -1] [-1 0] [1 0]]]289 (fn [angle]290 (let [angle (float angle)]291 (let [position292 [(+ 25 (int (* 20 (Math/cos angle))))293 (+ 25 (int (* 20(Math/sin angle))))]]294 (draw-sprite image sprite (@previous 0) (@previous 1) 0x000000)295 (draw-sprite image sprite (position 0) (position 1) color)296 (reset! previous position))297 image))))299 (defn proprioception-debug-window300 []301 (let [yaw (view-angle 0xFF0000)302 roll (view-angle 0x00FF00)303 pitch (view-angle 0xFFFFFF)304 v-yaw (view-image)305 v-roll (view-image)306 v-pitch (view-image)307 ]308 (fn [prop-data]309 (dorun310 (map311 (fn [[y r p]]312 (v-yaw (yaw y))313 (v-roll (roll r))314 (v-pitch (pitch p)))315 prop-data)))))316 (comment318 (defn proprioception-debug-window319 []320 (let [time (atom 0)]321 (fn [prop-data]322 (if (= 0 (rem (swap! time inc) 40))323 (println-repl prop-data)))))324 )326 (comment327 (dorun328 (map329 (comp330 println-repl331 (fn [[p y r]]332 (format333 "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n"334 p y r)))335 prop-data)))339 (defn tap [obj direction force]340 (let [control (.getControl obj RigidBodyControl)]341 (.applyTorque342 control343 (.mult (.getPhysicsRotation control)344 (.mult (.normalize direction) (float force))))))348 (defmacro with-movement349 [object350 [up down left right roll-up roll-down :as keyboard]351 forces352 [world-invocation353 root-node354 keymap355 intilization356 world-loop]]357 (let [add-keypress358 (fn [state keymap key]359 `(merge ~keymap360 {~key361 (fn [_# pressed?#]362 (reset! ~state pressed?#))}))363 move-left? (gensym "move-left?")364 move-right? (gensym "move-right?")365 move-up? (gensym "move-up?")366 move-down? (gensym "move-down?")367 roll-left? (gensym "roll-left?")368 roll-right? (gensym "roll-right?")369 directions [[0 1 0][0 -1 0][0 0 1][0 0 -1][-1 0 0][1 0 0]]370 symbols [move-left? move-right? move-up? move-down?371 roll-left? roll-right?]373 keymap* (vec (map #(add-keypress %1 keymap %2)374 symbols375 keyboard))377 splice-loop (map (fn [sym direction force]378 `(if (deref ~sym)379 (tap ~object380 (Vector3f. ~@direction)381 ~force)))382 symbols directions forces)384 world-loop* `(fn [world# tpf#]385 (~world-loop world# tpf#)386 ~@splice-loop)]387 `(let [~move-up? (atom false)388 ~move-down? (atom false)389 ~move-left? (atom false)390 ~move-right? (atom false)391 ~roll-left? (atom false)392 ~roll-right? (atom false)]393 (~world-invocation394 ~root-node395 (reduce merge ~keymap*)396 ~intilization397 ~world-loop*))))400 (defn test-proprioception401 "Testing proprioception:402 You should see two foating bars, and a printout of pitch, yaw, and403 roll. Pressing key-r/key-t should move the blue bar up and down and404 change only the value of pitch. key-f/key-g moves it side to side405 and changes yaw. key-v/key-b will spin the blue segment clockwise406 and counterclockwise, and only affect roll."407 []408 (let [hand (box 1 0.2 0.2 :position (Vector3f. 0 2 0)409 :mass 1 :color ColorRGBA/Green :name "hand")410 finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0)411 :mass 1 :color ColorRGBA/Red :name "finger")412 joint-node (box 0.1 0.05 0.05 :color ColorRGBA/Yellow413 :position (Vector3f. 1.2 2 0)414 :physical? false)415 joint (join-at-point hand finger (Vector3f. 1.2 2 0 ))416 creature (nodify [hand finger joint-node])417 ;; *******************************************419 floor (box 10 10 10 :position (Vector3f. 0 -15 0)420 :mass 0 :color ColorRGBA/Gray)422 root (nodify [creature floor])423 prop (joint-proprioception creature joint-node)424 prop-view (proprioception-debug-window)425 finger-control (.getControl finger RigidBodyControl)426 hand-control (.getControl hand RigidBodyControl)428 controls429 (merge standard-debug-controls430 {"key-y"431 (fn [_ _] (.setEnabled finger-control true))432 "key-u"433 (fn [_ _] (.setEnabled finger-control false))434 "key-i"435 (fn [_ _] (.setEnabled hand-control true))436 "key-o"437 (fn [_ _] (.setEnabled hand-control false))438 "key-q"439 (fn [world _] (set-gravity world (Vector3f. 0 0 0)))440 }441 )443 ]444 (comment445 (.setCollisionGroup446 (.getControl hand RigidBodyControl)447 PhysicsCollisionObject/COLLISION_GROUP_NONE)448 )450 (with-movement451 hand452 ["key-w" "key-e" "key-s" "key-d" "key-x" "key-c"]453 [10 10 10 10 1 1]454 (with-movement455 finger456 ["key-r" "key-t" "key-f" "key-g" "key-v" "key-b"]457 [10 10 10 10 1 1]458 (world459 root460 controls461 (fn [world]462 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))463 (set-gravity world (Vector3f. 0 0 0))464 (light-up-everything world))465 (fn [_ _] (prop-view (list (prop)))))))))470 #+end_src472 #+results: test-body473 : #'cortex.test.body/test-proprioception476 * COMMENT code-limbo477 #+begin_src clojure478 ;;(.loadModel479 ;; (doto (asset-manager)480 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))481 ;; "Models/person/person.blend")484 (defn load-blender-model485 "Load a .blend file using an asset folder relative path."486 [^String model]487 (.loadModel488 (doto (asset-manager)489 (.registerLoader BlenderModelLoader (into-array String ["blend"])))490 model))493 (defn view-model [^String model]494 (view495 (.loadModel496 (doto (asset-manager)497 (.registerLoader BlenderModelLoader (into-array String ["blend"])))498 model)))500 (defn load-blender-scene [^String model]501 (.loadModel502 (doto (asset-manager)503 (.registerLoader BlenderLoader (into-array String ["blend"])))504 model))506 (defn worm507 []508 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))510 (defn oto511 []512 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))514 (defn sinbad515 []516 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))518 (defn worm-blender519 []520 (first (seq (.getChildren (load-blender-model521 "Models/anim2/simple-worm.blend")))))523 (defn body524 "given a node with a SkeletonControl, will produce a body sutiable525 for AI control with movement and proprioception."526 [node]527 (let [skeleton-control (.getControl node SkeletonControl)528 krc (KinematicRagdollControl.)]529 (comment530 (dorun531 (map #(.addBoneName krc %)532 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]533 ;;"mid2" "mid3" "tail" "head"]534 )))535 (.addControl node krc)536 (.setRagdollMode krc)537 )538 node539 )540 (defn show-skeleton [node]541 (let [sd543 (doto544 (SkeletonDebugger. "aurellem-skel-debug"545 (skel node))546 (.setMaterial (green-x-ray)))]547 (.attachChild node sd)548 node))552 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;554 ;; this could be a good way to give objects special properties like555 ;; being eyes and the like557 (.getUserData558 (.getChild559 (load-blender-model "Models/property/test.blend") 0)560 "properties")562 ;; the properties are saved along with the blender file.563 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;568 (defn init-debug-skel-node569 [f debug-node skeleton]570 (let [bones571 (map #(.getBone skeleton %)572 (range (.getBoneCount skeleton)))]573 (dorun (map #(.setUserControl % true) bones))574 (dorun (map (fn [b]575 (println (.getName b)576 " -- " (f b)))577 bones))578 (dorun579 (map #(.attachChild580 debug-node581 (doto582 (sphere 0.1583 :position (f %)584 :physical? false)585 (.setMaterial (green-x-ray))))586 bones)))587 debug-node)589 (import jme3test.bullet.PhysicsTestHelper)592 (defn test-zzz [the-worm world value]593 (if (not value)594 (let [skeleton (skel the-worm)]595 (println-repl "enabling bones")596 (dorun597 (map598 #(.setUserControl (.getBone skeleton %) true)599 (range (.getBoneCount skeleton))))602 (let [b (.getBone skeleton 2)]603 (println-repl "moving " (.getName b))604 (println-repl (.getLocalPosition b))605 (.setUserTransforms b606 Vector3f/UNIT_X607 Quaternion/IDENTITY608 ;;(doto (Quaternion.)609 ;; (.fromAngles (/ Math/PI 2)610 ;; 0611 ;; 0613 (Vector3f. 1 1 1))614 )616 (println-repl "hi! <3"))))619 (defn test-ragdoll []621 (let [the-worm623 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")624 (doto (show-skeleton (worm-blender))625 (.setLocalTranslation (Vector3f. 0 10 0))626 ;;(worm)627 ;;(oto)628 ;;(sinbad)629 )630 ]633 (.start634 (world635 (doto (Node.)636 (.attachChild the-worm))637 {"key-return" (fire-cannon-ball)638 "key-space" (partial test-zzz the-worm)639 }640 (fn [world]641 (light-up-everything world)642 (PhysicsTestHelper/createPhysicsTestWorld643 (.getRootNode world)644 (asset-manager)645 (.getPhysicsSpace646 (.getState (.getStateManager world) BulletAppState)))647 (set-gravity world Vector3f/ZERO)648 ;;(.setTimer world (NanoTimer.))649 ;;(org.lwjgl.input.Mouse/setGrabbed false)650 )651 no-op652 )655 )))658 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;659 ;;; here is the ragdoll stuff661 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))662 (def mesh worm-mesh)664 (.getFloatBuffer mesh VertexBuffer$Type/Position)665 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)666 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))669 (defn position [index]670 (.get671 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)672 index))674 (defn bones [index]675 (.get676 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))677 index))679 (defn bone-weights [index]680 (.get681 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)682 index))686 (defn vertex-bones [vertex]687 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))689 (defn vertex-weights [vertex]690 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))692 (defn vertex-position [index]693 (let [offset (* index 3)]694 (Vector3f. (position offset)695 (position (inc offset))696 (position (inc(inc offset))))))698 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))700 (defn bone-control-color [index]701 (get {[1 0 0 0] ColorRGBA/Red702 [1 2 0 0] ColorRGBA/Magenta703 [2 0 0 0] ColorRGBA/Blue}704 (vertex-bones index)705 ColorRGBA/White))707 (defn influence-color [index bone-num]708 (get709 {(float 0) ColorRGBA/Blue710 (float 0.5) ColorRGBA/Green711 (float 1) ColorRGBA/Red}712 ;; find the weight of the desired bone713 ((zipmap (vertex-bones index)(vertex-weights index))714 bone-num)715 ColorRGBA/Blue))717 (def worm-vertices (set (map vertex-info (range 60))))720 (defn test-info []721 (let [points (Node.)]722 (dorun723 (map #(.attachChild points %)724 (map #(sphere 0.01725 :position (vertex-position %)726 :color (influence-color % 1)727 :physical? false)728 (range 60))))729 (view points)))732 (defrecord JointControl [joint physics-space]733 PhysicsControl734 (setPhysicsSpace [this space]735 (dosync736 (ref-set (:physics-space this) space))737 (.addJoint space (:joint this)))738 (update [this tpf])739 (setSpatial [this spatial])740 (render [this rm vp])741 (getPhysicsSpace [this] (deref (:physics-space this)))742 (isEnabled [this] true)743 (setEnabled [this state]))745 (defn add-joint746 "Add a joint to a particular object. When the object is added to the747 PhysicsSpace of a simulation, the joint will also be added"748 [object joint]749 (let [control (JointControl. joint (ref nil))]750 (.addControl object control))751 object)754 (defn hinge-world755 []756 (let [sphere1 (sphere)757 sphere2 (sphere 1 :position (Vector3f. 3 3 3))758 joint (Point2PointJoint.759 (.getControl sphere1 RigidBodyControl)760 (.getControl sphere2 RigidBodyControl)761 Vector3f/ZERO (Vector3f. 3 3 3))]762 (add-joint sphere1 joint)763 (doto (Node. "hinge-world")764 (.attachChild sphere1)765 (.attachChild sphere2))))768 (defn test-joint []769 (view (hinge-world)))771 ;; (defn copier-gen []772 ;; (let [count (atom 0)]773 ;; (fn [in]774 ;; (swap! count inc)775 ;; (clojure.contrib.duck-streams/copy776 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"777 ;; ;;/home/r/tmp/mao-test/clojure-images778 ;; (format "%08d.png" @count)))))))779 ;; (defn decrease-framerate []780 ;; (map781 ;; (copier-gen)782 ;; (sort783 ;; (map first784 ;; (partition785 ;; 4786 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))787 ;; (file-seq788 ;; (file-str789 ;; "/home/r/media/anime/mao-temp/images"))))))))793 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;795 (defn proprioception796 "Create a proprioception map that reports the rotations of the797 various limbs of the creature's body"798 [creature]799 [#^Node creature]800 (let [801 nodes (node-seq creature)802 joints803 (map804 :joint805 (filter806 #(isa? (class %) JointControl)807 (reduce808 concat809 (map (fn [node]810 (map (fn [num] (.getControl node num))811 (range (.getNumControls node))))812 nodes))))]813 (fn []814 (reduce concat (map relative-positions (list (first joints)))))))817 (defn skel [node]818 (doto819 (.getSkeleton820 (.getControl node SkeletonControl))821 ;; this is necessary to force the skeleton to have accurate world822 ;; transforms before it is rendered to the screen.823 (.resetAndUpdate)))825 (defn green-x-ray []826 (doto (Material. (asset-manager)827 "Common/MatDefs/Misc/Unshaded.j3md")828 (.setColor "Color" ColorRGBA/Green)829 (-> (.getAdditionalRenderState)830 (.setDepthTest false))))832 (defn test-worm []833 (.start834 (world835 (doto (Node.)836 ;;(.attachChild (point-worm))837 (.attachChild (load-blender-model838 "Models/anim2/joint-worm.blend"))840 (.attachChild (box 10 1 10841 :position (Vector3f. 0 -2 0) :mass 0842 :color (ColorRGBA/Gray))))843 {844 "key-space" (fire-cannon-ball)845 }846 (fn [world]847 (enable-debug world)848 (light-up-everything world)849 ;;(.setTimer world (NanoTimer.))850 )851 no-op)))855 ;; defunct movement stuff856 (defn torque-controls [control]857 (let [torques858 (concat859 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))860 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))861 [Vector3f/UNIT_X])]862 (map (fn [torque-axis]863 (fn [torque]864 (.applyTorque865 control866 (.mult (.mult (.getPhysicsRotation control)867 torque-axis)868 (float869 (* (.getMass control) torque))))))870 torques)))872 (defn motor-map873 "Take a creature and generate a function that will enable fine874 grained control over all the creature's limbs."875 [#^Node creature]876 (let [controls (keep #(.getControl % RigidBodyControl)877 (node-seq creature))878 limb-controls (reduce concat (map torque-controls controls))879 body-control (partial map #(%1 %2) limb-controls)]880 body-control))882 (defn test-motor-map883 "see how torque works."884 []885 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)886 :mass 1 :color ColorRGBA/Green)887 motor-map (motor-map finger)]888 (world889 (nodify [finger890 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0891 :color ColorRGBA/Gray)])892 standard-debug-controls893 (fn [world]894 (set-gravity world Vector3f/ZERO)895 (light-up-everything world)896 (.setTimer world (NanoTimer.)))897 (fn [_ _]898 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))899 #+end_src907 * COMMENT generate Source908 #+begin_src clojure :tangle ../src/cortex/body.clj909 <<proprioception>>910 <<motor-control>>911 #+end_src913 #+begin_src clojure :tangle ../src/cortex/test/body.clj914 <<test-body>>915 #+end_src