Mercurial > cortex
view org/body.org @ 140:22444eb20ecc
de-macrofication complete.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 02 Feb 2012 01:32:31 -0700 |
parents | ffbab4199c0d |
children | 22e193b5c60f |
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 (defn with-movement349 [object350 [up down left right roll-up roll-down :as keyboard]351 forces352 [root-node353 keymap354 intilization355 world-loop]]356 (let [add-keypress357 (fn [state keymap key]358 (merge keymap359 {key360 (fn [_ pressed?]361 (reset! state pressed?))}))362 move-up? (atom false)363 move-down? (atom false)364 move-left? (atom false)365 move-right? (atom false)366 roll-left? (atom false)367 roll-right? (atom false)369 directions [(Vector3f. 0 1 0)(Vector3f. 0 -1 0)370 (Vector3f. 0 0 1)(Vector3f. 0 0 -1)371 (Vector3f. -1 0 0)(Vector3f. 1 0 0)]372 atoms [move-left? move-right? move-up? move-down?373 roll-left? roll-right?]375 keymap* (reduce merge376 (map #(add-keypress %1 keymap %2)377 atoms378 keyboard))380 splice-loop (fn []381 (dorun382 (map383 (fn [sym direction force]384 (if @sym385 (tap object direction force)))386 atoms directions forces)))388 world-loop* (fn [world tpf]389 (world-loop world tpf)390 (splice-loop))]392 [root-node393 keymap*394 intilization395 world-loop*]))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 1 :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 }439 )441 ]442 (comment443 (.setCollisionGroup444 (.getControl hand RigidBodyControl)445 PhysicsCollisionObject/COLLISION_GROUP_NONE)446 )447 (apply448 world449 (with-movement450 hand451 ["key-y" "key-u" "key-h" "key-j" "key-n" "key-m"]452 [10 10 10 10 1 1]453 (with-movement454 finger455 ["key-r" "key-t" "key-f" "key-g" "key-v" "key-b"]456 [10 10 10 10 1 1]457 [root458 controls459 (fn [world]460 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))461 (set-gravity world (Vector3f. 0 0 0))462 (light-up-everything world))463 (fn [_ _] (prop-view (list (prop))))])))))465 #+end_src467 #+results: test-body468 : #'cortex.test.body/test-proprioception471 * COMMENT code-limbo472 #+begin_src clojure473 ;;(.loadModel474 ;; (doto (asset-manager)475 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))476 ;; "Models/person/person.blend")479 (defn load-blender-model480 "Load a .blend file using an asset folder relative path."481 [^String model]482 (.loadModel483 (doto (asset-manager)484 (.registerLoader BlenderModelLoader (into-array String ["blend"])))485 model))488 (defn view-model [^String model]489 (view490 (.loadModel491 (doto (asset-manager)492 (.registerLoader BlenderModelLoader (into-array String ["blend"])))493 model)))495 (defn load-blender-scene [^String model]496 (.loadModel497 (doto (asset-manager)498 (.registerLoader BlenderLoader (into-array String ["blend"])))499 model))501 (defn worm502 []503 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))505 (defn oto506 []507 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))509 (defn sinbad510 []511 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))513 (defn worm-blender514 []515 (first (seq (.getChildren (load-blender-model516 "Models/anim2/simple-worm.blend")))))518 (defn body519 "given a node with a SkeletonControl, will produce a body sutiable520 for AI control with movement and proprioception."521 [node]522 (let [skeleton-control (.getControl node SkeletonControl)523 krc (KinematicRagdollControl.)]524 (comment525 (dorun526 (map #(.addBoneName krc %)527 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]528 ;;"mid2" "mid3" "tail" "head"]529 )))530 (.addControl node krc)531 (.setRagdollMode krc)532 )533 node534 )535 (defn show-skeleton [node]536 (let [sd538 (doto539 (SkeletonDebugger. "aurellem-skel-debug"540 (skel node))541 (.setMaterial (green-x-ray)))]542 (.attachChild node sd)543 node))547 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;549 ;; this could be a good way to give objects special properties like550 ;; being eyes and the like552 (.getUserData553 (.getChild554 (load-blender-model "Models/property/test.blend") 0)555 "properties")557 ;; the properties are saved along with the blender file.558 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;563 (defn init-debug-skel-node564 [f debug-node skeleton]565 (let [bones566 (map #(.getBone skeleton %)567 (range (.getBoneCount skeleton)))]568 (dorun (map #(.setUserControl % true) bones))569 (dorun (map (fn [b]570 (println (.getName b)571 " -- " (f b)))572 bones))573 (dorun574 (map #(.attachChild575 debug-node576 (doto577 (sphere 0.1578 :position (f %)579 :physical? false)580 (.setMaterial (green-x-ray))))581 bones)))582 debug-node)584 (import jme3test.bullet.PhysicsTestHelper)587 (defn test-zzz [the-worm world value]588 (if (not value)589 (let [skeleton (skel the-worm)]590 (println-repl "enabling bones")591 (dorun592 (map593 #(.setUserControl (.getBone skeleton %) true)594 (range (.getBoneCount skeleton))))597 (let [b (.getBone skeleton 2)]598 (println-repl "moving " (.getName b))599 (println-repl (.getLocalPosition b))600 (.setUserTransforms b601 Vector3f/UNIT_X602 Quaternion/IDENTITY603 ;;(doto (Quaternion.)604 ;; (.fromAngles (/ Math/PI 2)605 ;; 0606 ;; 0608 (Vector3f. 1 1 1))609 )611 (println-repl "hi! <3"))))614 (defn test-ragdoll []616 (let [the-worm618 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")619 (doto (show-skeleton (worm-blender))620 (.setLocalTranslation (Vector3f. 0 10 0))621 ;;(worm)622 ;;(oto)623 ;;(sinbad)624 )625 ]628 (.start629 (world630 (doto (Node.)631 (.attachChild the-worm))632 {"key-return" (fire-cannon-ball)633 "key-space" (partial test-zzz the-worm)634 }635 (fn [world]636 (light-up-everything world)637 (PhysicsTestHelper/createPhysicsTestWorld638 (.getRootNode world)639 (asset-manager)640 (.getPhysicsSpace641 (.getState (.getStateManager world) BulletAppState)))642 (set-gravity world Vector3f/ZERO)643 ;;(.setTimer world (NanoTimer.))644 ;;(org.lwjgl.input.Mouse/setGrabbed false)645 )646 no-op647 )650 )))653 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;654 ;;; here is the ragdoll stuff656 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))657 (def mesh worm-mesh)659 (.getFloatBuffer mesh VertexBuffer$Type/Position)660 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)661 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))664 (defn position [index]665 (.get666 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)667 index))669 (defn bones [index]670 (.get671 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))672 index))674 (defn bone-weights [index]675 (.get676 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)677 index))681 (defn vertex-bones [vertex]682 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))684 (defn vertex-weights [vertex]685 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))687 (defn vertex-position [index]688 (let [offset (* index 3)]689 (Vector3f. (position offset)690 (position (inc offset))691 (position (inc(inc offset))))))693 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))695 (defn bone-control-color [index]696 (get {[1 0 0 0] ColorRGBA/Red697 [1 2 0 0] ColorRGBA/Magenta698 [2 0 0 0] ColorRGBA/Blue}699 (vertex-bones index)700 ColorRGBA/White))702 (defn influence-color [index bone-num]703 (get704 {(float 0) ColorRGBA/Blue705 (float 0.5) ColorRGBA/Green706 (float 1) ColorRGBA/Red}707 ;; find the weight of the desired bone708 ((zipmap (vertex-bones index)(vertex-weights index))709 bone-num)710 ColorRGBA/Blue))712 (def worm-vertices (set (map vertex-info (range 60))))715 (defn test-info []716 (let [points (Node.)]717 (dorun718 (map #(.attachChild points %)719 (map #(sphere 0.01720 :position (vertex-position %)721 :color (influence-color % 1)722 :physical? false)723 (range 60))))724 (view points)))727 (defrecord JointControl [joint physics-space]728 PhysicsControl729 (setPhysicsSpace [this space]730 (dosync731 (ref-set (:physics-space this) space))732 (.addJoint space (:joint this)))733 (update [this tpf])734 (setSpatial [this spatial])735 (render [this rm vp])736 (getPhysicsSpace [this] (deref (:physics-space this)))737 (isEnabled [this] true)738 (setEnabled [this state]))740 (defn add-joint741 "Add a joint to a particular object. When the object is added to the742 PhysicsSpace of a simulation, the joint will also be added"743 [object joint]744 (let [control (JointControl. joint (ref nil))]745 (.addControl object control))746 object)749 (defn hinge-world750 []751 (let [sphere1 (sphere)752 sphere2 (sphere 1 :position (Vector3f. 3 3 3))753 joint (Point2PointJoint.754 (.getControl sphere1 RigidBodyControl)755 (.getControl sphere2 RigidBodyControl)756 Vector3f/ZERO (Vector3f. 3 3 3))]757 (add-joint sphere1 joint)758 (doto (Node. "hinge-world")759 (.attachChild sphere1)760 (.attachChild sphere2))))763 (defn test-joint []764 (view (hinge-world)))766 ;; (defn copier-gen []767 ;; (let [count (atom 0)]768 ;; (fn [in]769 ;; (swap! count inc)770 ;; (clojure.contrib.duck-streams/copy771 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"772 ;; ;;/home/r/tmp/mao-test/clojure-images773 ;; (format "%08d.png" @count)))))))774 ;; (defn decrease-framerate []775 ;; (map776 ;; (copier-gen)777 ;; (sort778 ;; (map first779 ;; (partition780 ;; 4781 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))782 ;; (file-seq783 ;; (file-str784 ;; "/home/r/media/anime/mao-temp/images"))))))))788 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;790 (defn proprioception791 "Create a proprioception map that reports the rotations of the792 various limbs of the creature's body"793 [creature]794 [#^Node creature]795 (let [796 nodes (node-seq creature)797 joints798 (map799 :joint800 (filter801 #(isa? (class %) JointControl)802 (reduce803 concat804 (map (fn [node]805 (map (fn [num] (.getControl node num))806 (range (.getNumControls node))))807 nodes))))]808 (fn []809 (reduce concat (map relative-positions (list (first joints)))))))812 (defn skel [node]813 (doto814 (.getSkeleton815 (.getControl node SkeletonControl))816 ;; this is necessary to force the skeleton to have accurate world817 ;; transforms before it is rendered to the screen.818 (.resetAndUpdate)))820 (defn green-x-ray []821 (doto (Material. (asset-manager)822 "Common/MatDefs/Misc/Unshaded.j3md")823 (.setColor "Color" ColorRGBA/Green)824 (-> (.getAdditionalRenderState)825 (.setDepthTest false))))827 (defn test-worm []828 (.start829 (world830 (doto (Node.)831 ;;(.attachChild (point-worm))832 (.attachChild (load-blender-model833 "Models/anim2/joint-worm.blend"))835 (.attachChild (box 10 1 10836 :position (Vector3f. 0 -2 0) :mass 0837 :color (ColorRGBA/Gray))))838 {839 "key-space" (fire-cannon-ball)840 }841 (fn [world]842 (enable-debug world)843 (light-up-everything world)844 ;;(.setTimer world (NanoTimer.))845 )846 no-op)))850 ;; defunct movement stuff851 (defn torque-controls [control]852 (let [torques853 (concat854 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))855 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))856 [Vector3f/UNIT_X])]857 (map (fn [torque-axis]858 (fn [torque]859 (.applyTorque860 control861 (.mult (.mult (.getPhysicsRotation control)862 torque-axis)863 (float864 (* (.getMass control) torque))))))865 torques)))867 (defn motor-map868 "Take a creature and generate a function that will enable fine869 grained control over all the creature's limbs."870 [#^Node creature]871 (let [controls (keep #(.getControl % RigidBodyControl)872 (node-seq creature))873 limb-controls (reduce concat (map torque-controls controls))874 body-control (partial map #(%1 %2) limb-controls)]875 body-control))877 (defn test-motor-map878 "see how torque works."879 []880 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)881 :mass 1 :color ColorRGBA/Green)882 motor-map (motor-map finger)]883 (world884 (nodify [finger885 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0886 :color ColorRGBA/Gray)])887 standard-debug-controls888 (fn [world]889 (set-gravity world Vector3f/ZERO)890 (light-up-everything world)891 (.setTimer world (NanoTimer.)))892 (fn [_ _]893 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))894 #+end_src902 * COMMENT generate Source903 #+begin_src clojure :tangle ../src/cortex/body.clj904 <<proprioception>>905 <<motor-control>>906 #+end_src908 #+begin_src clojure :tangle ../src/cortex/test/body.clj909 <<test-body>>910 #+end_src