Mercurial > cortex
view org/body.org @ 175:0b9ae09eaec3
renamed functions in body
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 04 Feb 2012 06:47:07 -0700 |
parents | 33278bf028e7 |
children | 026f69582022 |
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 * Making a solid, connected body.9 #+name: joints10 #+begin_src clojure11 (ns cortex.body12 (:use (cortex world util sense))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.BoundingBox21 com.jme3.scene.Node))23 (use 'clojure.contrib.def)24 (cortex.import/mega-import-jme3)26 (defn joint-targets27 "Return the two closest two objects to the joint object, ordered28 from bottom to top according to the joint's rotation."29 [#^Node parts #^Node joint]30 (loop [radius (float 0.01)]31 (let [results (CollisionResults.)]32 (.collideWith33 parts34 (BoundingBox. (.getWorldTranslation joint)35 radius radius radius)36 results)37 (let [targets38 (distinct39 (map #(.getGeometry %) results))]40 (if (>= (count targets) 2)41 (sort-by42 #(let [v43 (jme-to-blender44 (.mult45 (.inverse (.getWorldRotation joint))46 (.subtract (.getWorldTranslation %)47 (.getWorldTranslation joint))))]48 (println-repl (.getName %) ":" v)49 (.dot (Vector3f. 1 1 1)50 v))51 (take 2 targets))52 (recur (float (* radius 2))))))))54 (defmulti joint-dispatch55 "Translate blender pseudo-joints into real JME joints."56 (fn [constraints & _]57 (:type constraints)))59 (defmethod joint-dispatch :point60 [constraints control-a control-b pivot-a pivot-b rotation]61 (println-repl "creating POINT2POINT joint")62 ;; bullet's point2point joints are BROKEN, so we must use the63 ;; generic 6DOF joint instead of an actual Point2Point joint!65 ;; should be able to do this:66 (comment67 (Point2PointJoint.68 control-a69 control-b70 pivot-a71 pivot-b))73 ;; but instead we must do this:74 (println-repl "substuting 6DOF joint for POINT2POINT joint!")75 (doto76 (SixDofJoint.77 control-a78 control-b79 pivot-a80 pivot-b81 false)82 (.setLinearLowerLimit Vector3f/ZERO)83 (.setLinearUpperLimit Vector3f/ZERO)84 ;;(.setAngularLowerLimit (Vector3f. 1 1 1))85 ;;(.setAngularUpperLimit (Vector3f. 0 0 0))87 ))90 (defmethod joint-dispatch :hinge91 [constraints control-a control-b pivot-a pivot-b rotation]92 (println-repl "creating HINGE joint")93 (let [axis94 (if-let95 [axis (:axis constraints)]96 axis97 Vector3f/UNIT_X)98 [limit-1 limit-2] (:limit constraints)99 hinge-axis100 (.mult101 rotation102 (blender-to-jme axis))]103 (doto104 (HingeJoint.105 control-a106 control-b107 pivot-a108 pivot-b109 hinge-axis110 hinge-axis)111 (.setLimit limit-1 limit-2))))113 (defmethod joint-dispatch :cone114 [constraints control-a control-b pivot-a pivot-b rotation]115 (let [limit-xz (:limit-xz constraints)116 limit-xy (:limit-xy constraints)117 twist (:twist constraints)]119 (println-repl "creating CONE joint")120 (println-repl rotation)121 (println-repl122 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))123 (println-repl124 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))125 (println-repl126 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))127 (doto128 (ConeJoint.129 control-a130 control-b131 pivot-a132 pivot-b133 rotation134 rotation)135 (.setLimit (float limit-xz)136 (float limit-xy)137 (float twist)))))139 (defn connect140 "Create a joint between 'obj-a and 'obj-b at the location of141 'joint. The type of joint is determined by the metadata on 'joint.143 Here are some examples:144 {:type :point}145 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}146 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)148 {:type :cone :limit-xz 0]149 :limit-xy 0]150 :twist 0]} (use XZY rotation mode in blender!)"151 [#^Node obj-a #^Node obj-b #^Node joint]152 (let [control-a (.getControl obj-a RigidBodyControl)153 control-b (.getControl obj-b RigidBodyControl)154 joint-center (.getWorldTranslation joint)155 joint-rotation (.toRotationMatrix (.getWorldRotation joint))156 pivot-a (world-to-local obj-a joint-center)157 pivot-b (world-to-local obj-b joint-center)]159 (if-let [constraints160 (map-vals161 eval162 (read-string163 (meta-data joint "joint")))]164 ;; A side-effect of creating a joint registers165 ;; it with both physics objects which in turn166 ;; will register the joint with the physics system167 ;; when the simulation is started.168 (do169 (println-repl "creating joint between"170 (.getName obj-a) "and" (.getName obj-b))171 (joint-dispatch constraints172 control-a control-b173 pivot-a pivot-b174 joint-rotation))175 (println-repl "could not find joint meta-data!"))))177 (defvar178 ^{:arglists '([creature])}179 joints180 (sense-nodes "joints")181 "Return the children of the creature's \"joints\" node.")183 (defn physical!184 "Iterate through the nodes in creature and make them real physical185 objects in the simulation."186 [#^Node creature]187 (dorun188 (map189 (fn [geom]190 (let [physics-control191 (RigidBodyControl.192 (HullCollisionShape.193 (.getMesh geom))194 (if-let [mass (meta-data geom "mass")]195 (do196 (println-repl197 "setting" (.getName geom) "mass to" (float mass))198 (float mass))199 (float 1)))]201 (.addControl geom physics-control)))202 (filter #(isa? (class %) Geometry )203 (node-seq creature)))))205 (defn joints!206 "Connect the solid parts of the creature with physical joints. The207 joints are taken from the \"joints\" node in the creature."208 [#^Node creature]209 (dorun210 (map211 (fn [joint]212 (let [[obj-a obj-b] (joint-targets creature joint)]213 (connect obj-a obj-b joint)))214 (joints creature))))216 (defn body!217 "Endow the creature with a physical body connected with joints. The218 particulars of the joints and the masses of each pody part are219 determined in blender."220 [#^Node creature]221 (physical! creature)222 (joints! creature))227 #+end_src229 #+results: joints230 : #'cortex.body/body!232 #+results: proprioception233 : #'cortex.body/proprioception-debug-window235 * Examples237 #+name: test-body238 #+begin_src clojure239 (ns cortex.test.body240 (:use (cortex world util body))241 (:require cortex.silly)242 (:import243 com.jme3.math.Vector3f244 com.jme3.math.ColorRGBA245 com.jme3.bullet.joints.Point2PointJoint246 com.jme3.bullet.control.RigidBodyControl247 com.jme3.system.NanoTimer248 com.jme3.math.Quaternion))250 (defn worm-segments251 "Create multiple evenly spaced box segments. They're fabulous!"252 [segment-length num-segments interstitial-space radius]253 (letfn [(nth-segment254 [n]255 (box segment-length radius radius :mass 0.1256 :position257 (Vector3f.258 (* 2 n (+ interstitial-space segment-length)) 0 0)259 :name (str "worm-segment" n)260 :color (ColorRGBA/randomColor)))]261 (map nth-segment (range num-segments))))263 (defn connect-at-midpoint264 "Connect two physics objects with a Point2Point joint constraint at265 the point equidistant from both objects' centers."266 [segmentA segmentB]267 (let [centerA (.getWorldTranslation segmentA)268 centerB (.getWorldTranslation segmentB)269 midpoint (.mult (.add centerA centerB) (float 0.5))270 pivotA (.subtract midpoint centerA)271 pivotB (.subtract midpoint centerB)273 ;; A side-effect of creating a joint registers274 ;; it with both physics objects which in turn275 ;; will register the joint with the physics system276 ;; when the simulation is started.277 joint (Point2PointJoint.278 (.getControl segmentA RigidBodyControl)279 (.getControl segmentB RigidBodyControl)280 pivotA281 pivotB)]282 segmentB))284 (defn eve-worm285 "Create a worm-like body bound by invisible joint constraints."286 []287 (let [segments (worm-segments 0.2 5 0.1 0.1)]288 (dorun (map (partial apply connect-at-midpoint)289 (partition 2 1 segments)))290 (nodify "worm" segments)))292 (defn worm-pattern293 "This is a simple, mindless motor control pattern that drives the294 second segment of the worm's body at an offset angle with295 sinusoidally varying strength."296 [time]297 (let [angle (* Math/PI (/ 9 20))298 direction (Vector3f. 0 (Math/sin angle) (Math/cos angle))]299 [Vector3f/ZERO300 (.mult301 direction302 (float (* 2 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))))303 Vector3f/ZERO304 Vector3f/ZERO305 Vector3f/ZERO]))307 (defn test-motor-control308 "Testing motor-control:309 You should see a multi-segmented worm-like object fall onto the310 table and begin writhing and moving."311 []312 (let [worm (eve-worm)313 time (atom 0)314 worm-motor-map (vector-motor-control worm)]315 (world316 (nodify [worm317 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0318 :color ColorRGBA/Gray)])319 standard-debug-controls320 (fn [world]321 (enable-debug world)322 (light-up-everything world)323 (comment324 (com.aurellem.capture.Capture/captureVideo325 world326 (file-str "/home/r/proj/cortex/tmp/moving-worm")))327 )329 (fn [_ _]330 (swap! time inc)331 (Thread/sleep 20)332 (dorun (worm-motor-map333 (worm-pattern @time)))))))337 (defn join-at-point [obj-a obj-b world-pivot]338 (cortex.silly/joint-dispatch339 {:type :point}340 (.getControl obj-a RigidBodyControl)341 (.getControl obj-b RigidBodyControl)342 (cortex.silly/world-to-local obj-a world-pivot)343 (cortex.silly/world-to-local obj-b world-pivot)344 nil345 ))347 (import com.jme3.bullet.collision.PhysicsCollisionObject)349 (defn blab-* []350 (let [hand (box 0.5 0.2 0.2 :position (Vector3f. 0 0 0)351 :mass 0 :color ColorRGBA/Green)352 finger (box 0.5 0.2 0.2 :position (Vector3f. 2.4 0 0)353 :mass 1 :color ColorRGBA/Red)354 connection-point (Vector3f. 1.2 0 0)355 root (nodify [hand finger])]357 (join-at-point hand finger (Vector3f. 1.2 0 0))359 (.setCollisionGroup360 (.getControl hand RigidBodyControl)361 PhysicsCollisionObject/COLLISION_GROUP_NONE)362 (world363 root364 standard-debug-controls365 (fn [world]366 (enable-debug world)367 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))368 (set-gravity world Vector3f/ZERO)369 )370 no-op)))371 (comment373 (defn proprioception-debug-window374 []375 (let [time (atom 0)]376 (fn [prop-data]377 (if (= 0 (rem (swap! time inc) 40))378 (println-repl prop-data)))))379 )381 (comment382 (dorun383 (map384 (comp385 println-repl386 (fn [[p y r]]387 (format388 "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n"389 p y r)))390 prop-data)))395 (defn test-proprioception396 "Testing proprioception:397 You should see two foating bars, and a printout of pitch, yaw, and398 roll. Pressing key-r/key-t should move the blue bar up and down and399 change only the value of pitch. key-f/key-g moves it side to side400 and changes yaw. key-v/key-b will spin the blue segment clockwise401 and counterclockwise, and only affect roll."402 []403 (let [hand (box 0.2 1 0.2 :position (Vector3f. 0 0 0)404 :mass 0 :color ColorRGBA/Green :name "hand")405 finger (box 0.2 1 0.2 :position (Vector3f. 0 2.4 0)406 :mass 1 :color ColorRGBA/Red :name "finger")407 joint-node (box 0.1 0.05 0.05 :color ColorRGBA/Yellow408 :position (Vector3f. 0 1.2 0)409 :rotation (doto (Quaternion.)410 (.fromAngleAxis411 (/ Math/PI 2)412 (Vector3f. 0 0 1)))413 :physical? false)414 joint (join-at-point hand finger (Vector3f. 0 1.2 0 ))415 creature (nodify [hand finger joint-node])416 finger-control (.getControl finger RigidBodyControl)417 hand-control (.getControl hand RigidBodyControl)]420 (let421 ;; *******************************************423 [floor (box 10 10 10 :position (Vector3f. 0 -15 0)424 :mass 0 :color ColorRGBA/Gray)426 root (nodify [creature floor])427 prop (joint-proprioception creature joint-node)428 prop-view (proprioception-debug-window)430 controls431 (merge standard-debug-controls432 {"key-o"433 (fn [_ _] (.setEnabled finger-control true))434 "key-p"435 (fn [_ _] (.setEnabled finger-control false))436 "key-k"437 (fn [_ _] (.setEnabled hand-control true))438 "key-l"439 (fn [_ _] (.setEnabled hand-control false))440 "key-i"441 (fn [world _] (set-gravity world (Vector3f. 0 0 0)))442 "key-period"443 (fn [world _]444 (.setEnabled finger-control false)445 (.setEnabled hand-control false)446 (.rotate creature (doto (Quaternion.)447 (.fromAngleAxis448 (float (/ Math/PI 15))449 (Vector3f. 0 0 -1))))451 (.setEnabled finger-control true)452 (.setEnabled hand-control true)453 (set-gravity world (Vector3f. 0 0 0))454 )457 }458 )460 ]461 (comment462 (.setCollisionGroup463 (.getControl hand RigidBodyControl)464 PhysicsCollisionObject/COLLISION_GROUP_NONE)465 )466 (apply467 world468 (with-movement469 hand470 ["key-y" "key-u" "key-h" "key-j" "key-n" "key-m"]471 [10 10 10 10 1 1]472 (with-movement473 finger474 ["key-r" "key-t" "key-f" "key-g" "key-v" "key-b"]475 [1 1 10 10 10 10]476 [root477 controls478 (fn [world]479 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))480 (set-gravity world (Vector3f. 0 0 0))481 (light-up-everything world))482 (fn [_ _] (prop-view (list (prop))))]))))))484 #+end_src486 #+results: test-body487 : #'cortex.test.body/test-proprioception490 * COMMENT code-limbo491 #+begin_src clojure492 ;;(.loadModel493 ;; (doto (asset-manager)494 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))495 ;; "Models/person/person.blend")498 (defn load-blender-model499 "Load a .blend file using an asset folder relative path."500 [^String model]501 (.loadModel502 (doto (asset-manager)503 (.registerLoader BlenderModelLoader (into-array String ["blend"])))504 model))507 (defn view-model [^String model]508 (view509 (.loadModel510 (doto (asset-manager)511 (.registerLoader BlenderModelLoader (into-array String ["blend"])))512 model)))514 (defn load-blender-scene [^String model]515 (.loadModel516 (doto (asset-manager)517 (.registerLoader BlenderLoader (into-array String ["blend"])))518 model))520 (defn worm521 []522 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))524 (defn oto525 []526 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))528 (defn sinbad529 []530 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))532 (defn worm-blender533 []534 (first (seq (.getChildren (load-blender-model535 "Models/anim2/simple-worm.blend")))))537 (defn body538 "given a node with a SkeletonControl, will produce a body sutiable539 for AI control with movement and proprioception."540 [node]541 (let [skeleton-control (.getControl node SkeletonControl)542 krc (KinematicRagdollControl.)]543 (comment544 (dorun545 (map #(.addBoneName krc %)546 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]547 ;;"mid2" "mid3" "tail" "head"]548 )))549 (.addControl node krc)550 (.setRagdollMode krc)551 )552 node553 )554 (defn show-skeleton [node]555 (let [sd557 (doto558 (SkeletonDebugger. "aurellem-skel-debug"559 (skel node))560 (.setMaterial (green-x-ray)))]561 (.attachChild node sd)562 node))566 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;568 ;; this could be a good way to give objects special properties like569 ;; being eyes and the like571 (.getUserData572 (.getChild573 (load-blender-model "Models/property/test.blend") 0)574 "properties")576 ;; the properties are saved along with the blender file.577 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;582 (defn init-debug-skel-node583 [f debug-node skeleton]584 (let [bones585 (map #(.getBone skeleton %)586 (range (.getBoneCount skeleton)))]587 (dorun (map #(.setUserControl % true) bones))588 (dorun (map (fn [b]589 (println (.getName b)590 " -- " (f b)))591 bones))592 (dorun593 (map #(.attachChild594 debug-node595 (doto596 (sphere 0.1597 :position (f %)598 :physical? false)599 (.setMaterial (green-x-ray))))600 bones)))601 debug-node)603 (import jme3test.bullet.PhysicsTestHelper)606 (defn test-zzz [the-worm world value]607 (if (not value)608 (let [skeleton (skel the-worm)]609 (println-repl "enabling bones")610 (dorun611 (map612 #(.setUserControl (.getBone skeleton %) true)613 (range (.getBoneCount skeleton))))616 (let [b (.getBone skeleton 2)]617 (println-repl "moving " (.getName b))618 (println-repl (.getLocalPosition b))619 (.setUserTransforms b620 Vector3f/UNIT_X621 Quaternion/IDENTITY622 ;;(doto (Quaternion.)623 ;; (.fromAngles (/ Math/PI 2)624 ;; 0625 ;; 0627 (Vector3f. 1 1 1))628 )630 (println-repl "hi! <3"))))633 (defn test-ragdoll []635 (let [the-worm637 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")638 (doto (show-skeleton (worm-blender))639 (.setLocalTranslation (Vector3f. 0 10 0))640 ;;(worm)641 ;;(oto)642 ;;(sinbad)643 )644 ]647 (.start648 (world649 (doto (Node.)650 (.attachChild the-worm))651 {"key-return" (fire-cannon-ball)652 "key-space" (partial test-zzz the-worm)653 }654 (fn [world]655 (light-up-everything world)656 (PhysicsTestHelper/createPhysicsTestWorld657 (.getRootNode world)658 (asset-manager)659 (.getPhysicsSpace660 (.getState (.getStateManager world) BulletAppState)))661 (set-gravity world Vector3f/ZERO)662 ;;(.setTimer world (NanoTimer.))663 ;;(org.lwjgl.input.Mouse/setGrabbed false)664 )665 no-op666 )669 )))672 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;673 ;;; here is the ragdoll stuff675 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))676 (def mesh worm-mesh)678 (.getFloatBuffer mesh VertexBuffer$Type/Position)679 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)680 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))683 (defn position [index]684 (.get685 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)686 index))688 (defn bones [index]689 (.get690 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))691 index))693 (defn bone-weights [index]694 (.get695 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)696 index))700 (defn vertex-bones [vertex]701 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))703 (defn vertex-weights [vertex]704 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))706 (defn vertex-position [index]707 (let [offset (* index 3)]708 (Vector3f. (position offset)709 (position (inc offset))710 (position (inc(inc offset))))))712 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))714 (defn bone-control-color [index]715 (get {[1 0 0 0] ColorRGBA/Red716 [1 2 0 0] ColorRGBA/Magenta717 [2 0 0 0] ColorRGBA/Blue}718 (vertex-bones index)719 ColorRGBA/White))721 (defn influence-color [index bone-num]722 (get723 {(float 0) ColorRGBA/Blue724 (float 0.5) ColorRGBA/Green725 (float 1) ColorRGBA/Red}726 ;; find the weight of the desired bone727 ((zipmap (vertex-bones index)(vertex-weights index))728 bone-num)729 ColorRGBA/Blue))731 (def worm-vertices (set (map vertex-info (range 60))))734 (defn test-info []735 (let [points (Node.)]736 (dorun737 (map #(.attachChild points %)738 (map #(sphere 0.01739 :position (vertex-position %)740 :color (influence-color % 1)741 :physical? false)742 (range 60))))743 (view points)))746 (defrecord JointControl [joint physics-space]747 PhysicsControl748 (setPhysicsSpace [this space]749 (dosync750 (ref-set (:physics-space this) space))751 (.addJoint space (:joint this)))752 (update [this tpf])753 (setSpatial [this spatial])754 (render [this rm vp])755 (getPhysicsSpace [this] (deref (:physics-space this)))756 (isEnabled [this] true)757 (setEnabled [this state]))759 (defn add-joint760 "Add a joint to a particular object. When the object is added to the761 PhysicsSpace of a simulation, the joint will also be added"762 [object joint]763 (let [control (JointControl. joint (ref nil))]764 (.addControl object control))765 object)768 (defn hinge-world769 []770 (let [sphere1 (sphere)771 sphere2 (sphere 1 :position (Vector3f. 3 3 3))772 joint (Point2PointJoint.773 (.getControl sphere1 RigidBodyControl)774 (.getControl sphere2 RigidBodyControl)775 Vector3f/ZERO (Vector3f. 3 3 3))]776 (add-joint sphere1 joint)777 (doto (Node. "hinge-world")778 (.attachChild sphere1)779 (.attachChild sphere2))))782 (defn test-joint []783 (view (hinge-world)))785 ;; (defn copier-gen []786 ;; (let [count (atom 0)]787 ;; (fn [in]788 ;; (swap! count inc)789 ;; (clojure.contrib.duck-streams/copy790 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"791 ;; ;;/home/r/tmp/mao-test/clojure-images792 ;; (format "%08d.png" @count)))))))793 ;; (defn decrease-framerate []794 ;; (map795 ;; (copier-gen)796 ;; (sort797 ;; (map first798 ;; (partition799 ;; 4800 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))801 ;; (file-seq802 ;; (file-str803 ;; "/home/r/media/anime/mao-temp/images"))))))))807 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;809 (defn proprioception810 "Create a proprioception map that reports the rotations of the811 various limbs of the creature's body"812 [creature]813 [#^Node creature]814 (let [815 nodes (node-seq creature)816 joints817 (map818 :joint819 (filter820 #(isa? (class %) JointControl)821 (reduce822 concat823 (map (fn [node]824 (map (fn [num] (.getControl node num))825 (range (.getNumControls node))))826 nodes))))]827 (fn []828 (reduce concat (map relative-positions (list (first joints)))))))831 (defn skel [node]832 (doto833 (.getSkeleton834 (.getControl node SkeletonControl))835 ;; this is necessary to force the skeleton to have accurate world836 ;; transforms before it is rendered to the screen.837 (.resetAndUpdate)))839 (defn green-x-ray []840 (doto (Material. (asset-manager)841 "Common/MatDefs/Misc/Unshaded.j3md")842 (.setColor "Color" ColorRGBA/Green)843 (-> (.getAdditionalRenderState)844 (.setDepthTest false))))846 (defn test-worm []847 (.start848 (world849 (doto (Node.)850 ;;(.attachChild (point-worm))851 (.attachChild (load-blender-model852 "Models/anim2/joint-worm.blend"))854 (.attachChild (box 10 1 10855 :position (Vector3f. 0 -2 0) :mass 0856 :color (ColorRGBA/Gray))))857 {858 "key-space" (fire-cannon-ball)859 }860 (fn [world]861 (enable-debug world)862 (light-up-everything world)863 ;;(.setTimer world (NanoTimer.))864 )865 no-op)))869 ;; defunct movement stuff870 (defn torque-controls [control]871 (let [torques872 (concat873 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))874 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))875 [Vector3f/UNIT_X])]876 (map (fn [torque-axis]877 (fn [torque]878 (.applyTorque879 control880 (.mult (.mult (.getPhysicsRotation control)881 torque-axis)882 (float883 (* (.getMass control) torque))))))884 torques)))886 (defn motor-map887 "Take a creature and generate a function that will enable fine888 grained control over all the creature's limbs."889 [#^Node creature]890 (let [controls (keep #(.getControl % RigidBodyControl)891 (node-seq creature))892 limb-controls (reduce concat (map torque-controls controls))893 body-control (partial map #(%1 %2) limb-controls)]894 body-control))896 (defn test-motor-map897 "see how torque works."898 []899 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)900 :mass 1 :color ColorRGBA/Green)901 motor-map (motor-map finger)]902 (world903 (nodify [finger904 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0905 :color ColorRGBA/Gray)])906 standard-debug-controls907 (fn [world]908 (set-gravity world Vector3f/ZERO)909 (light-up-everything world)910 (.setTimer world (NanoTimer.)))911 (fn [_ _]912 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0913 0]))))))915 (defn joint-proprioception [#^Node parts #^Node joint]916 (let [[obj-a obj-b] (joint-targets parts joint)917 joint-rot (.getWorldRotation joint)918 pre-inv-a (.inverse (.getWorldRotation obj-a))919 x (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_X))920 y (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_Y))921 z (.mult pre-inv-a (.mult joint-rot Vector3f/UNIT_Z))923 x Vector3f/UNIT_Y924 y Vector3f/UNIT_Z925 z Vector3f/UNIT_X928 tmp-rot-a (.getWorldRotation obj-a)]929 (println-repl "x:" (.mult tmp-rot-a x))930 (println-repl "y:" (.mult tmp-rot-a y))931 (println-repl "z:" (.mult tmp-rot-a z))932 (println-repl "rot-a" (.getWorldRotation obj-a))933 (println-repl "rot-b" (.getWorldRotation obj-b))934 (println-repl "joint-rot" joint-rot)935 ;; this function will report proprioceptive information for the936 ;; joint.937 (fn []938 ;; x is the "twist" axis, y and z are the "bend" axes939 (let [rot-a (.getWorldRotation obj-a)940 ;;inv-a (.inverse rot-a)941 rot-b (.getWorldRotation obj-b)942 ;;relative (.mult rot-b inv-a)943 basis (doto (Matrix3f.)944 (.setColumn 0 (.mult rot-a x))945 (.setColumn 1 (.mult rot-a y))946 (.setColumn 2 (.mult rot-a z)))947 rotation-about-joint948 (doto (Quaternion.)949 (.fromRotationMatrix950 (.mult (.invert basis)951 (.toRotationMatrix rot-b))))952 [yaw roll pitch]953 (seq (.toAngles rotation-about-joint nil))]954 ;;return euler angles of the quaternion around the new basis955 [yaw roll pitch]))))957 #+end_src965 * COMMENT generate Source966 #+begin_src clojure :tangle ../src/cortex/body.clj967 <<joints>>968 #+end_src970 #+begin_src clojure :tangle ../src/cortex/test/body.clj971 <<test-body>>972 #+end_src