Mercurial > cortex
view org/body.org @ 138:16bdf9e80daf
working movement debug framework
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 02 Feb 2012 00:05:17 -0700 |
parents | 39c89ae5c7d0 |
children | ffbab4199c0d |
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)]388 `(let [~move-up? (atom false)389 ~move-down? (atom false)390 ~move-left? (atom false)391 ~move-right? (atom false)392 ~roll-left? (atom false)393 ~roll-right? (atom false)]394 (~world-invocation395 ~root-node396 (reduce merge ~keymap*)397 ~intilization398 ~world-loop*))))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)]427 (.setCollisionGroup428 (.getControl hand RigidBodyControl)429 PhysicsCollisionObject/COLLISION_GROUP_NONE)431 (with-movement432 finger433 ["key-r" "key-t" "key-f" "key-g" "key-v" "key-b"]434 [10 10 10 10 1 1]435 (world436 root437 standard-debug-controls438 (fn [world]439 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))440 (set-gravity world (Vector3f. 0 0 0))441 (light-up-everything world))442 (fn [_ _] (prop-view (list (prop))))))))447 #+end_src449 #+results: test-body450 : #'cortex.test.body/test-proprioception453 * COMMENT code-limbo454 #+begin_src clojure455 ;;(.loadModel456 ;; (doto (asset-manager)457 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))458 ;; "Models/person/person.blend")461 (defn load-blender-model462 "Load a .blend file using an asset folder relative path."463 [^String model]464 (.loadModel465 (doto (asset-manager)466 (.registerLoader BlenderModelLoader (into-array String ["blend"])))467 model))470 (defn view-model [^String model]471 (view472 (.loadModel473 (doto (asset-manager)474 (.registerLoader BlenderModelLoader (into-array String ["blend"])))475 model)))477 (defn load-blender-scene [^String model]478 (.loadModel479 (doto (asset-manager)480 (.registerLoader BlenderLoader (into-array String ["blend"])))481 model))483 (defn worm484 []485 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))487 (defn oto488 []489 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))491 (defn sinbad492 []493 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))495 (defn worm-blender496 []497 (first (seq (.getChildren (load-blender-model498 "Models/anim2/simple-worm.blend")))))500 (defn body501 "given a node with a SkeletonControl, will produce a body sutiable502 for AI control with movement and proprioception."503 [node]504 (let [skeleton-control (.getControl node SkeletonControl)505 krc (KinematicRagdollControl.)]506 (comment507 (dorun508 (map #(.addBoneName krc %)509 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]510 ;;"mid2" "mid3" "tail" "head"]511 )))512 (.addControl node krc)513 (.setRagdollMode krc)514 )515 node516 )517 (defn show-skeleton [node]518 (let [sd520 (doto521 (SkeletonDebugger. "aurellem-skel-debug"522 (skel node))523 (.setMaterial (green-x-ray)))]524 (.attachChild node sd)525 node))529 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;531 ;; this could be a good way to give objects special properties like532 ;; being eyes and the like534 (.getUserData535 (.getChild536 (load-blender-model "Models/property/test.blend") 0)537 "properties")539 ;; the properties are saved along with the blender file.540 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;545 (defn init-debug-skel-node546 [f debug-node skeleton]547 (let [bones548 (map #(.getBone skeleton %)549 (range (.getBoneCount skeleton)))]550 (dorun (map #(.setUserControl % true) bones))551 (dorun (map (fn [b]552 (println (.getName b)553 " -- " (f b)))554 bones))555 (dorun556 (map #(.attachChild557 debug-node558 (doto559 (sphere 0.1560 :position (f %)561 :physical? false)562 (.setMaterial (green-x-ray))))563 bones)))564 debug-node)566 (import jme3test.bullet.PhysicsTestHelper)569 (defn test-zzz [the-worm world value]570 (if (not value)571 (let [skeleton (skel the-worm)]572 (println-repl "enabling bones")573 (dorun574 (map575 #(.setUserControl (.getBone skeleton %) true)576 (range (.getBoneCount skeleton))))579 (let [b (.getBone skeleton 2)]580 (println-repl "moving " (.getName b))581 (println-repl (.getLocalPosition b))582 (.setUserTransforms b583 Vector3f/UNIT_X584 Quaternion/IDENTITY585 ;;(doto (Quaternion.)586 ;; (.fromAngles (/ Math/PI 2)587 ;; 0588 ;; 0590 (Vector3f. 1 1 1))591 )593 (println-repl "hi! <3"))))596 (defn test-ragdoll []598 (let [the-worm600 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")601 (doto (show-skeleton (worm-blender))602 (.setLocalTranslation (Vector3f. 0 10 0))603 ;;(worm)604 ;;(oto)605 ;;(sinbad)606 )607 ]610 (.start611 (world612 (doto (Node.)613 (.attachChild the-worm))614 {"key-return" (fire-cannon-ball)615 "key-space" (partial test-zzz the-worm)616 }617 (fn [world]618 (light-up-everything world)619 (PhysicsTestHelper/createPhysicsTestWorld620 (.getRootNode world)621 (asset-manager)622 (.getPhysicsSpace623 (.getState (.getStateManager world) BulletAppState)))624 (set-gravity world Vector3f/ZERO)625 ;;(.setTimer world (NanoTimer.))626 ;;(org.lwjgl.input.Mouse/setGrabbed false)627 )628 no-op629 )632 )))635 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;636 ;;; here is the ragdoll stuff638 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))639 (def mesh worm-mesh)641 (.getFloatBuffer mesh VertexBuffer$Type/Position)642 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)643 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))646 (defn position [index]647 (.get648 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)649 index))651 (defn bones [index]652 (.get653 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))654 index))656 (defn bone-weights [index]657 (.get658 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)659 index))663 (defn vertex-bones [vertex]664 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))666 (defn vertex-weights [vertex]667 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))669 (defn vertex-position [index]670 (let [offset (* index 3)]671 (Vector3f. (position offset)672 (position (inc offset))673 (position (inc(inc offset))))))675 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))677 (defn bone-control-color [index]678 (get {[1 0 0 0] ColorRGBA/Red679 [1 2 0 0] ColorRGBA/Magenta680 [2 0 0 0] ColorRGBA/Blue}681 (vertex-bones index)682 ColorRGBA/White))684 (defn influence-color [index bone-num]685 (get686 {(float 0) ColorRGBA/Blue687 (float 0.5) ColorRGBA/Green688 (float 1) ColorRGBA/Red}689 ;; find the weight of the desired bone690 ((zipmap (vertex-bones index)(vertex-weights index))691 bone-num)692 ColorRGBA/Blue))694 (def worm-vertices (set (map vertex-info (range 60))))697 (defn test-info []698 (let [points (Node.)]699 (dorun700 (map #(.attachChild points %)701 (map #(sphere 0.01702 :position (vertex-position %)703 :color (influence-color % 1)704 :physical? false)705 (range 60))))706 (view points)))709 (defrecord JointControl [joint physics-space]710 PhysicsControl711 (setPhysicsSpace [this space]712 (dosync713 (ref-set (:physics-space this) space))714 (.addJoint space (:joint this)))715 (update [this tpf])716 (setSpatial [this spatial])717 (render [this rm vp])718 (getPhysicsSpace [this] (deref (:physics-space this)))719 (isEnabled [this] true)720 (setEnabled [this state]))722 (defn add-joint723 "Add a joint to a particular object. When the object is added to the724 PhysicsSpace of a simulation, the joint will also be added"725 [object joint]726 (let [control (JointControl. joint (ref nil))]727 (.addControl object control))728 object)731 (defn hinge-world732 []733 (let [sphere1 (sphere)734 sphere2 (sphere 1 :position (Vector3f. 3 3 3))735 joint (Point2PointJoint.736 (.getControl sphere1 RigidBodyControl)737 (.getControl sphere2 RigidBodyControl)738 Vector3f/ZERO (Vector3f. 3 3 3))]739 (add-joint sphere1 joint)740 (doto (Node. "hinge-world")741 (.attachChild sphere1)742 (.attachChild sphere2))))745 (defn test-joint []746 (view (hinge-world)))748 ;; (defn copier-gen []749 ;; (let [count (atom 0)]750 ;; (fn [in]751 ;; (swap! count inc)752 ;; (clojure.contrib.duck-streams/copy753 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"754 ;; ;;/home/r/tmp/mao-test/clojure-images755 ;; (format "%08d.png" @count)))))))756 ;; (defn decrease-framerate []757 ;; (map758 ;; (copier-gen)759 ;; (sort760 ;; (map first761 ;; (partition762 ;; 4763 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))764 ;; (file-seq765 ;; (file-str766 ;; "/home/r/media/anime/mao-temp/images"))))))))770 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;772 (defn proprioception773 "Create a proprioception map that reports the rotations of the774 various limbs of the creature's body"775 [creature]776 [#^Node creature]777 (let [778 nodes (node-seq creature)779 joints780 (map781 :joint782 (filter783 #(isa? (class %) JointControl)784 (reduce785 concat786 (map (fn [node]787 (map (fn [num] (.getControl node num))788 (range (.getNumControls node))))789 nodes))))]790 (fn []791 (reduce concat (map relative-positions (list (first joints)))))))794 (defn skel [node]795 (doto796 (.getSkeleton797 (.getControl node SkeletonControl))798 ;; this is necessary to force the skeleton to have accurate world799 ;; transforms before it is rendered to the screen.800 (.resetAndUpdate)))802 (defn green-x-ray []803 (doto (Material. (asset-manager)804 "Common/MatDefs/Misc/Unshaded.j3md")805 (.setColor "Color" ColorRGBA/Green)806 (-> (.getAdditionalRenderState)807 (.setDepthTest false))))809 (defn test-worm []810 (.start811 (world812 (doto (Node.)813 ;;(.attachChild (point-worm))814 (.attachChild (load-blender-model815 "Models/anim2/joint-worm.blend"))817 (.attachChild (box 10 1 10818 :position (Vector3f. 0 -2 0) :mass 0819 :color (ColorRGBA/Gray))))820 {821 "key-space" (fire-cannon-ball)822 }823 (fn [world]824 (enable-debug world)825 (light-up-everything world)826 ;;(.setTimer world (NanoTimer.))827 )828 no-op)))832 ;; defunct movement stuff833 (defn torque-controls [control]834 (let [torques835 (concat836 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))837 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))838 [Vector3f/UNIT_X])]839 (map (fn [torque-axis]840 (fn [torque]841 (.applyTorque842 control843 (.mult (.mult (.getPhysicsRotation control)844 torque-axis)845 (float846 (* (.getMass control) torque))))))847 torques)))849 (defn motor-map850 "Take a creature and generate a function that will enable fine851 grained control over all the creature's limbs."852 [#^Node creature]853 (let [controls (keep #(.getControl % RigidBodyControl)854 (node-seq creature))855 limb-controls (reduce concat (map torque-controls controls))856 body-control (partial map #(%1 %2) limb-controls)]857 body-control))859 (defn test-motor-map860 "see how torque works."861 []862 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)863 :mass 1 :color ColorRGBA/Green)864 motor-map (motor-map finger)]865 (world866 (nodify [finger867 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0868 :color ColorRGBA/Gray)])869 standard-debug-controls870 (fn [world]871 (set-gravity world Vector3f/ZERO)872 (light-up-everything world)873 (.setTimer world (NanoTimer.)))874 (fn [_ _]875 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))876 #+end_src884 * COMMENT generate Source885 #+begin_src clojure :tangle ../src/cortex/body.clj886 <<proprioception>>887 <<motor-control>>888 #+end_src890 #+begin_src clojure :tangle ../src/cortex/test/body.clj891 <<test-body>>892 #+end_src