Mercurial > cortex
view org/body.org @ 133:2ed7e60d3821
FINALLY got proprioception working
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Wed, 01 Feb 2012 02:27:18 -0700 |
parents | 3206d5e20bee |
children | ac350a0ac6b0 |
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.RigidBodyControl))20 (comment21 (defn joint-proprioception22 "Relative position information for a two-part system connected by a23 joint. Gives the pitch, yaw, and roll of the 'B' object relative to24 the 'A' object, as determined by the joint."25 [joint]26 (let [object-a (.getUserObject (.getBodyA joint))27 object-b (.getUserObject (.getBodyB joint))28 arm-a29 (.normalize30 (.subtract31 (.localToWorld object-a (.getPivotA joint) nil)32 (.getWorldTranslation object-a)))34 ;; this is probably wrong!35 rotate-a36 (doto (Matrix3f.)37 (.fromStartEndVectors arm-a Vector3f/UNIT_X))39 arm-b40 (.mult41 rotate-a42 (.normalize43 (.subtract44 (.localToWorld object-b (.getPivotB joint) nil)45 (.getWorldTranslation object-b))))46 pitch47 (.angleBetween48 (.normalize (Vector2f. (.getX arm-b) (.getY arm-b)))49 (Vector2f. 1 0))50 yaw51 (.angleBetween52 (.normalize (Vector2f. (.getX arm-b) (.getZ arm-b)))53 (Vector2f. 1 0))55 roll56 (project-quaternion57 (.mult58 (.getLocalRotation object-b)59 (doto (Quaternion.)60 (.fromRotationMatrix rotate-a)))61 arm-b)]62 ;;(println-repl (.getName object-a) (.getName object-b))63 [pitch yaw roll]))64 )66 (defn any-orthogonal67 "Generate an arbitray (but stable) orthogonal vector to a given68 vector."69 [vector]70 (let [x (.getX vector)71 y (.getY vector)72 z (.getZ vector)]73 (cond74 (not= x (float 0)) (Vector3f. (- z) 0 x)75 (not= y (float 0)) (Vector3f. 0 (- z) y)76 (not= z (float 0)) (Vector3f. 0 (- z) y)77 true Vector3f/ZERO)))79 (comment80 (defn project-quaternion81 "From http://stackoverflow.com/questions/3684269/82 component-of-a-quaternion-rotation-around-an-axis.84 Determine the amount of rotation a quaternion will85 cause about a given axis."86 [#^Quaternion q #^Vector3f axis]87 (let [basis-1 (any-orthogonal axis)88 basis-2 (.cross axis basis-1)89 rotated (.mult q basis-1)90 alpha (.dot basis-1 (.project rotated basis-1))91 beta (.dot basis-2 (.project rotated basis-2))]92 (Math/atan2 beta alpha)))93 )95 (defn right-handed? [vec1 vec2 vec3]96 (< 0 (.dot (.cross vec1 vec2) vec3)))98 (defn absolute-angle [vec1 vec2 axis]99 (let [angle (.angleBetween vec1 vec2)]100 (if (right-handed? vec1 vec2 axis)101 angle (- (* 2 Math/PI) angle))))103 (defn angle-min [& angles]104 (first105 (sort-by106 (fn [angle]107 (let [in-circle (Math/abs (rem angle (* 2 Math/PI)))]108 (min in-circle109 (- (* Math/PI 2) in-circle))))110 angles)))112 (defn project-quaternion113 "From http://stackoverflow.com/questions/3684269/114 component-of-a-quaternion-rotation-around-an-axis.116 Determine the amount of rotation a quaternion will117 cause about a given axis."118 [#^Quaternion q #^Vector3f axis]119 (let [axis (.normalize axis)120 basis-1 (.normalize (any-orthogonal axis))121 basis-2 (.cross axis basis-1)122 rotated-1 (.mult q basis-1)123 basis-1* (.normalize124 (.add (.project rotated-1 basis-1)125 (.project rotated-1 basis-2)))126 rotated-2 (.mult q basis-2)127 basis-2* (.normalize128 (.add (.project rotated-2 basis-1)129 (.project rotated-2 basis-2)))130 angle-1131 (absolute-angle basis-1 basis-1* axis)132 angle-2133 (absolute-angle basis-2 basis-2* axis)136 angle (angle-min angle-1 angle-2)137 ]140 ;; be sure to get sign from cross product141 (if false142 (do143 (println-repl "axis" axis)144 (println-repl "basis-1" basis-1)145 (println-repl "basis-2" basis-2)146 (println-repl "rotated-1" rotated-1)147 (println-repl "rotated-2" rotated-2)148 (println-repl "basis-1*" basis-1*)149 (println-repl "basis-2*" basis-2*)150 (println-repl "angle-1" angle-1)151 (println-repl "angle-2" angle-2)153 (println-repl "angle" angle)154 (println-repl "")))155 angle))158 (import com.jme3.scene.Node)160 (defn joint-proprioception [#^Node parts #^Node joint]161 (let [[obj-a obj-b] (cortex.silly/joint-targets parts joint)162 joint-rot (.getWorldRotation joint)163 x (.mult joint-rot Vector3f/UNIT_X)164 y (.mult joint-rot Vector3f/UNIT_Y)165 z (.mult joint-rot Vector3f/UNIT_Z)]166 ;; this function will report proprioceptive information for the167 ;; joint168 (fn []169 ;; x is the "twist" axis, y and z are the "bend" axes170 (let [rot-a (.getWorldRotation obj-a)171 rot-b (.getWorldRotation obj-b)172 relative (.mult (.inverse rot-a) rot-b)173 basis (doto (Matrix3f.)174 (.setColumn 0 x)175 (.setColumn 1 y)176 (.setColumn 2 z))177 rotation-about-joint178 (doto (Quaternion.)179 (.fromRotationMatrix180 (.mult (.invert basis)181 (.toRotationMatrix relative))))182 [yaw roll pitch]183 (seq (.toAngles rotation-about-joint nil))]184 ;;return euler angles of the quaternion around the new basis185 ;;[yaw pitch roll]186 [yaw roll pitch]187 ))))190 (comment192 (defn joint-proprioception193 [joint]194 (let [object-a (.getUserObject (.getBodyA joint))195 object-b (.getUserObject (.getBodyB joint))196 rot-a (.clone (.getWorldRotation object-a))197 rot-b (.clone (.getWorldRotation object-b))198 ]200 (.mult rot-b (.inverse rot-a))202 ;; object-a == hand203 ;; object-b == finger204 ))205 )206 ;; (defn joint-proprioception*207 ;; [joint]208 ;; (let [object-a (.getUserObject (.getBodyA joint))209 ;; object-b (.getUserObject (.getBodyB joint))211 ;; rotate-a (.clone (.getWorldRotation object-a))212 ;; rotate-b (.clone (.getWorldRotation object-b))214 ;; rotate-rel (.mult rotate-b (.inverse rotate-a))215 ;; ]216 ;; ((comp vec map) (partial project-quaternion rotate-rel)217 ;; [Vector3f/UNIT_X218 ;; Vector3f/UNIT_Y219 ;; Vector3f/UNIT_Z])))222 (defn proprioception223 "Create a function that provides proprioceptive information about an224 entire body."225 [body]226 ;; extract the body's joints227 (let [joints228 (distinct229 (reduce230 concat231 (map #(.getJoints %)232 (keep233 #(.getControl % RigidBodyControl)234 (node-seq body)))))]235 (fn []236 (map joint-proprioception joints))))238 #+end_src240 #+results: proprioception241 : #'cortex.body/proprioception243 * Motor Control244 #+name: motor-control245 #+begin_src clojure246 (in-ns 'cortex.body)248 ;; surprisingly enough, terristerial creatures only move by using249 ;; torque applied about their joints. There's not a single straight250 ;; line of force in the human body at all! (A straight line of force251 ;; would correspond to some sort of jet or rocket propulseion.)253 (defn vector-motor-control254 "Create a function that accepts a sequence of Vector3f objects that255 describe the torque to be applied to each part of the body."256 [body]257 (let [nodes (node-seq body)258 controls (keep #(.getControl % RigidBodyControl) nodes)]259 (fn [torques]260 (map #(.applyTorque %1 %2)261 controls torques))))262 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;263 #+end_src265 ## note -- might want to add a lower dimensional, discrete version of266 ## this if it proves useful from a x-modal clustering perspective.268 * Examples270 #+name: test-body271 #+begin_src clojure272 (ns cortex.test.body273 (:use (cortex world util body))274 (:import275 com.jme3.math.Vector3f276 com.jme3.math.ColorRGBA277 com.jme3.bullet.joints.Point2PointJoint278 com.jme3.bullet.control.RigidBodyControl279 com.jme3.system.NanoTimer))281 (defn worm-segments282 "Create multiple evenly spaced box segments. They're fabulous!"283 [segment-length num-segments interstitial-space radius]284 (letfn [(nth-segment285 [n]286 (box segment-length radius radius :mass 0.1287 :position288 (Vector3f.289 (* 2 n (+ interstitial-space segment-length)) 0 0)290 :name (str "worm-segment" n)291 :color (ColorRGBA/randomColor)))]292 (map nth-segment (range num-segments))))294 (defn connect-at-midpoint295 "Connect two physics objects with a Point2Point joint constraint at296 the point equidistant from both objects' centers."297 [segmentA segmentB]298 (let [centerA (.getWorldTranslation segmentA)299 centerB (.getWorldTranslation segmentB)300 midpoint (.mult (.add centerA centerB) (float 0.5))301 pivotA (.subtract midpoint centerA)302 pivotB (.subtract midpoint centerB)304 ;; A side-effect of creating a joint registers305 ;; it with both physics objects which in turn306 ;; will register the joint with the physics system307 ;; when the simulation is started.308 joint (Point2PointJoint.309 (.getControl segmentA RigidBodyControl)310 (.getControl segmentB RigidBodyControl)311 pivotA312 pivotB)]313 segmentB))315 (defn eve-worm316 "Create a worm-like body bound by invisible joint constraints."317 []318 (let [segments (worm-segments 0.2 5 0.1 0.1)]319 (dorun (map (partial apply connect-at-midpoint)320 (partition 2 1 segments)))321 (nodify "worm" segments)))323 (defn worm-pattern324 "This is a simple, mindless motor control pattern that drives the325 second segment of the worm's body at an offset angle with326 sinusoidally varying strength."327 [time]328 (let [angle (* Math/PI (/ 9 20))329 direction (Vector3f. 0 (Math/sin angle) (Math/cos angle))]330 [Vector3f/ZERO331 (.mult332 direction333 (float (* 2 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))))334 Vector3f/ZERO335 Vector3f/ZERO336 Vector3f/ZERO]))338 (defn test-motor-control339 "Testing motor-control:340 You should see a multi-segmented worm-like object fall onto the341 table and begin writhing and moving."342 []343 (let [worm (eve-worm)344 time (atom 0)345 worm-motor-map (vector-motor-control worm)]346 (world347 (nodify [worm348 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0349 :color ColorRGBA/Gray)])350 standard-debug-controls351 (fn [world]352 (enable-debug world)353 (light-up-everything world)354 (comment355 (com.aurellem.capture.Capture/captureVideo356 world357 (file-str "/home/r/proj/cortex/tmp/moving-worm")))358 )360 (fn [_ _]361 (swap! time inc)362 (Thread/sleep 20)363 (dorun (worm-motor-map364 (worm-pattern @time)))))))367 (require 'cortex.silly)368 (defn join-at-point [obj-a obj-b world-pivot]369 (cortex.silly/joint-dispatch370 {:type :point}371 (.getControl obj-a RigidBodyControl)372 (.getControl obj-b RigidBodyControl)373 (cortex.silly/world-to-local obj-a world-pivot)374 (cortex.silly/world-to-local obj-b world-pivot)375 nil376 ))378 (import com.jme3.bullet.collision.PhysicsCollisionObject)380 (defn blab-* []381 (let [hand (box 0.5 0.2 0.2 :position (Vector3f. 0 0 0)382 :mass 0 :color ColorRGBA/Green)383 finger (box 0.5 0.2 0.2 :position (Vector3f. 2.4 0 0)384 :mass 1 :color ColorRGBA/Red)385 connection-point (Vector3f. 1.2 0 0)386 root (nodify [hand finger])]388 (join-at-point hand finger (Vector3f. 1.2 0 0))390 (.setCollisionGroup391 (.getControl hand RigidBodyControl)392 PhysicsCollisionObject/COLLISION_GROUP_NONE)393 (world394 root395 standard-debug-controls396 (fn [world]397 (enable-debug world)398 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))399 (set-gravity world Vector3f/ZERO)400 )401 no-op)))402 (import java.awt.image.BufferedImage)404 (defn draw-sprite [image sprite x y color ]405 (dorun406 (for [[u v] sprite]407 (.setRGB image (+ u x) (+ v y) color))))409 (defn view-angle410 "create a debug view of an angle"411 [color]412 (let [image (BufferedImage. 50 50 BufferedImage/TYPE_INT_RGB)413 previous (atom [25 25])414 sprite [[0 0] [0 1]415 [0 -1] [-1 0] [1 0]]]416 (fn [angle]417 (let [angle (float angle)]418 (let [position419 [(+ 25 (int (* 20 (Math/cos angle))))420 (+ 25 (int (* 20(Math/sin angle))))]]421 (draw-sprite image sprite (@previous 0) (@previous 1) 0x000000)422 (draw-sprite image sprite (position 0) (position 1) color)423 (reset! previous position))424 image))))426 (defn proprioception-debug-window427 []428 (let [yaw (view-angle 0xFF0000)429 roll (view-angle 0x00FF00)430 pitch (view-angle 0xFFFFFF)431 v-yaw (view-image)432 v-roll (view-image)433 v-pitch (view-image)434 ]435 (fn [prop-data]436 (dorun437 (map438 (fn [[y r p]]439 (v-yaw (yaw y))440 (v-roll (roll r))441 (v-pitch (pitch p)))442 prop-data)))))443 (comment445 (defn proprioception-debug-window446 []447 (let [time (atom 0)]448 (fn [prop-data]449 (if (= 0 (rem (swap! time inc) 40))450 (println-repl prop-data)))))451 )453 (comment454 (dorun455 (map456 (comp457 println-repl458 (fn [[p y r]]459 (format460 "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n"461 p y r)))462 prop-data)))468 (defn test-proprioception469 "Testing proprioception:470 You should see two foating bars, and a printout of pitch, yaw, and471 roll. Pressing key-r/key-t should move the blue bar up and down and472 change only the value of pitch. key-f/key-g moves it side to side473 and changes yaw. key-v/key-b will spin the blue segment clockwise474 and counterclockwise, and only affect roll."475 []476 (let [hand (box 1 0.2 0.2 :position (Vector3f. 0 2 0)477 :mass 0 :color ColorRGBA/Green :name "hand")478 finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0)479 :mass 1 :color ColorRGBA/Red :name "finger")480 floor (box 10 10 10 :position (Vector3f. 0 -15 0)481 :mass 0 :color ColorRGBA/Gray)482 joint-node (box 0.1 0.05 0.05 :color ColorRGBA/Yellow483 :position (Vector3f. 1.2 2 0)484 :physical? false)486 move-up? (atom false)487 move-down? (atom false)488 move-left? (atom false)489 move-right? (atom false)490 roll-left? (atom false)491 roll-right? (atom false)492 control (.getControl finger RigidBodyControl)493 time (atom 0)494 joint (join-at-point hand finger (Vector3f. 1.2 2 0 ))495 creature (nodify [hand finger joint-node])496 prop (joint-proprioception creature joint-node)498 prop-view (proprioception-debug-window)501 ]506 (.setCollisionGroup507 (.getControl hand RigidBodyControl)508 PhysicsCollisionObject/COLLISION_GROUP_NONE)511 (world512 (nodify [hand finger floor joint-node])513 (merge standard-debug-controls514 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))515 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))516 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))517 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))518 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))519 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})520 (fn [world]521 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))522 (set-gravity world (Vector3f. 0 0 0))523 (light-up-everything world))524 (fn [_ _]525 (if @move-up?526 (.applyTorque control527 (.mult (.getPhysicsRotation control)528 (Vector3f. 0 0 10))))529 (if @move-down?530 (.applyTorque control531 (.mult (.getPhysicsRotation control)532 (Vector3f. 0 0 -10))))533 (if @move-left?534 (.applyTorque control535 (.mult (.getPhysicsRotation control)536 (Vector3f. 0 10 0))))537 (if @move-right?538 (.applyTorque control539 (.mult (.getPhysicsRotation control)540 (Vector3f. 0 -10 0))))541 (if @roll-left?542 (.applyTorque control543 (.mult (.getPhysicsRotation control)544 (Vector3f. -1 0 0))))545 (if @roll-right?546 (.applyTorque control547 (.mult (.getPhysicsRotation control)548 (Vector3f. 1 0 0))))550 ;;(if (= 0 (rem (swap! time inc) 20))551 (prop-view (list (prop)))))))553 #+end_src555 #+results: test-body556 : #'cortex.test.body/test-proprioception559 * COMMENT code-limbo560 #+begin_src clojure561 ;;(.loadModel562 ;; (doto (asset-manager)563 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))564 ;; "Models/person/person.blend")567 (defn load-blender-model568 "Load a .blend file using an asset folder relative path."569 [^String model]570 (.loadModel571 (doto (asset-manager)572 (.registerLoader BlenderModelLoader (into-array String ["blend"])))573 model))576 (defn view-model [^String model]577 (view578 (.loadModel579 (doto (asset-manager)580 (.registerLoader BlenderModelLoader (into-array String ["blend"])))581 model)))583 (defn load-blender-scene [^String model]584 (.loadModel585 (doto (asset-manager)586 (.registerLoader BlenderLoader (into-array String ["blend"])))587 model))589 (defn worm590 []591 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))593 (defn oto594 []595 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))597 (defn sinbad598 []599 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))601 (defn worm-blender602 []603 (first (seq (.getChildren (load-blender-model604 "Models/anim2/simple-worm.blend")))))606 (defn body607 "given a node with a SkeletonControl, will produce a body sutiable608 for AI control with movement and proprioception."609 [node]610 (let [skeleton-control (.getControl node SkeletonControl)611 krc (KinematicRagdollControl.)]612 (comment613 (dorun614 (map #(.addBoneName krc %)615 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]616 ;;"mid2" "mid3" "tail" "head"]617 )))618 (.addControl node krc)619 (.setRagdollMode krc)620 )621 node622 )623 (defn show-skeleton [node]624 (let [sd626 (doto627 (SkeletonDebugger. "aurellem-skel-debug"628 (skel node))629 (.setMaterial (green-x-ray)))]630 (.attachChild node sd)631 node))635 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;637 ;; this could be a good way to give objects special properties like638 ;; being eyes and the like640 (.getUserData641 (.getChild642 (load-blender-model "Models/property/test.blend") 0)643 "properties")645 ;; the properties are saved along with the blender file.646 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;651 (defn init-debug-skel-node652 [f debug-node skeleton]653 (let [bones654 (map #(.getBone skeleton %)655 (range (.getBoneCount skeleton)))]656 (dorun (map #(.setUserControl % true) bones))657 (dorun (map (fn [b]658 (println (.getName b)659 " -- " (f b)))660 bones))661 (dorun662 (map #(.attachChild663 debug-node664 (doto665 (sphere 0.1666 :position (f %)667 :physical? false)668 (.setMaterial (green-x-ray))))669 bones)))670 debug-node)672 (import jme3test.bullet.PhysicsTestHelper)675 (defn test-zzz [the-worm world value]676 (if (not value)677 (let [skeleton (skel the-worm)]678 (println-repl "enabling bones")679 (dorun680 (map681 #(.setUserControl (.getBone skeleton %) true)682 (range (.getBoneCount skeleton))))685 (let [b (.getBone skeleton 2)]686 (println-repl "moving " (.getName b))687 (println-repl (.getLocalPosition b))688 (.setUserTransforms b689 Vector3f/UNIT_X690 Quaternion/IDENTITY691 ;;(doto (Quaternion.)692 ;; (.fromAngles (/ Math/PI 2)693 ;; 0694 ;; 0696 (Vector3f. 1 1 1))697 )699 (println-repl "hi! <3"))))702 (defn test-ragdoll []704 (let [the-worm706 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")707 (doto (show-skeleton (worm-blender))708 (.setLocalTranslation (Vector3f. 0 10 0))709 ;;(worm)710 ;;(oto)711 ;;(sinbad)712 )713 ]716 (.start717 (world718 (doto (Node.)719 (.attachChild the-worm))720 {"key-return" (fire-cannon-ball)721 "key-space" (partial test-zzz the-worm)722 }723 (fn [world]724 (light-up-everything world)725 (PhysicsTestHelper/createPhysicsTestWorld726 (.getRootNode world)727 (asset-manager)728 (.getPhysicsSpace729 (.getState (.getStateManager world) BulletAppState)))730 (set-gravity world Vector3f/ZERO)731 ;;(.setTimer world (NanoTimer.))732 ;;(org.lwjgl.input.Mouse/setGrabbed false)733 )734 no-op735 )738 )))741 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;742 ;;; here is the ragdoll stuff744 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))745 (def mesh worm-mesh)747 (.getFloatBuffer mesh VertexBuffer$Type/Position)748 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)749 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))752 (defn position [index]753 (.get754 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)755 index))757 (defn bones [index]758 (.get759 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))760 index))762 (defn bone-weights [index]763 (.get764 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)765 index))769 (defn vertex-bones [vertex]770 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))772 (defn vertex-weights [vertex]773 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))775 (defn vertex-position [index]776 (let [offset (* index 3)]777 (Vector3f. (position offset)778 (position (inc offset))779 (position (inc(inc offset))))))781 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))783 (defn bone-control-color [index]784 (get {[1 0 0 0] ColorRGBA/Red785 [1 2 0 0] ColorRGBA/Magenta786 [2 0 0 0] ColorRGBA/Blue}787 (vertex-bones index)788 ColorRGBA/White))790 (defn influence-color [index bone-num]791 (get792 {(float 0) ColorRGBA/Blue793 (float 0.5) ColorRGBA/Green794 (float 1) ColorRGBA/Red}795 ;; find the weight of the desired bone796 ((zipmap (vertex-bones index)(vertex-weights index))797 bone-num)798 ColorRGBA/Blue))800 (def worm-vertices (set (map vertex-info (range 60))))803 (defn test-info []804 (let [points (Node.)]805 (dorun806 (map #(.attachChild points %)807 (map #(sphere 0.01808 :position (vertex-position %)809 :color (influence-color % 1)810 :physical? false)811 (range 60))))812 (view points)))815 (defrecord JointControl [joint physics-space]816 PhysicsControl817 (setPhysicsSpace [this space]818 (dosync819 (ref-set (:physics-space this) space))820 (.addJoint space (:joint this)))821 (update [this tpf])822 (setSpatial [this spatial])823 (render [this rm vp])824 (getPhysicsSpace [this] (deref (:physics-space this)))825 (isEnabled [this] true)826 (setEnabled [this state]))828 (defn add-joint829 "Add a joint to a particular object. When the object is added to the830 PhysicsSpace of a simulation, the joint will also be added"831 [object joint]832 (let [control (JointControl. joint (ref nil))]833 (.addControl object control))834 object)837 (defn hinge-world838 []839 (let [sphere1 (sphere)840 sphere2 (sphere 1 :position (Vector3f. 3 3 3))841 joint (Point2PointJoint.842 (.getControl sphere1 RigidBodyControl)843 (.getControl sphere2 RigidBodyControl)844 Vector3f/ZERO (Vector3f. 3 3 3))]845 (add-joint sphere1 joint)846 (doto (Node. "hinge-world")847 (.attachChild sphere1)848 (.attachChild sphere2))))851 (defn test-joint []852 (view (hinge-world)))854 ;; (defn copier-gen []855 ;; (let [count (atom 0)]856 ;; (fn [in]857 ;; (swap! count inc)858 ;; (clojure.contrib.duck-streams/copy859 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"860 ;; ;;/home/r/tmp/mao-test/clojure-images861 ;; (format "%08d.png" @count)))))))862 ;; (defn decrease-framerate []863 ;; (map864 ;; (copier-gen)865 ;; (sort866 ;; (map first867 ;; (partition868 ;; 4869 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))870 ;; (file-seq871 ;; (file-str872 ;; "/home/r/media/anime/mao-temp/images"))))))))876 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;878 (defn proprioception879 "Create a proprioception map that reports the rotations of the880 various limbs of the creature's body"881 [creature]882 [#^Node creature]883 (let [884 nodes (node-seq creature)885 joints886 (map887 :joint888 (filter889 #(isa? (class %) JointControl)890 (reduce891 concat892 (map (fn [node]893 (map (fn [num] (.getControl node num))894 (range (.getNumControls node))))895 nodes))))]896 (fn []897 (reduce concat (map relative-positions (list (first joints)))))))900 (defn skel [node]901 (doto902 (.getSkeleton903 (.getControl node SkeletonControl))904 ;; this is necessary to force the skeleton to have accurate world905 ;; transforms before it is rendered to the screen.906 (.resetAndUpdate)))908 (defn green-x-ray []909 (doto (Material. (asset-manager)910 "Common/MatDefs/Misc/Unshaded.j3md")911 (.setColor "Color" ColorRGBA/Green)912 (-> (.getAdditionalRenderState)913 (.setDepthTest false))))915 (defn test-worm []916 (.start917 (world918 (doto (Node.)919 ;;(.attachChild (point-worm))920 (.attachChild (load-blender-model921 "Models/anim2/joint-worm.blend"))923 (.attachChild (box 10 1 10924 :position (Vector3f. 0 -2 0) :mass 0925 :color (ColorRGBA/Gray))))926 {927 "key-space" (fire-cannon-ball)928 }929 (fn [world]930 (enable-debug world)931 (light-up-everything world)932 ;;(.setTimer world (NanoTimer.))933 )934 no-op)))938 ;; defunct movement stuff939 (defn torque-controls [control]940 (let [torques941 (concat942 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))943 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))944 [Vector3f/UNIT_X])]945 (map (fn [torque-axis]946 (fn [torque]947 (.applyTorque948 control949 (.mult (.mult (.getPhysicsRotation control)950 torque-axis)951 (float952 (* (.getMass control) torque))))))953 torques)))955 (defn motor-map956 "Take a creature and generate a function that will enable fine957 grained control over all the creature's limbs."958 [#^Node creature]959 (let [controls (keep #(.getControl % RigidBodyControl)960 (node-seq creature))961 limb-controls (reduce concat (map torque-controls controls))962 body-control (partial map #(%1 %2) limb-controls)]963 body-control))965 (defn test-motor-map966 "see how torque works."967 []968 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)969 :mass 1 :color ColorRGBA/Green)970 motor-map (motor-map finger)]971 (world972 (nodify [finger973 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0974 :color ColorRGBA/Gray)])975 standard-debug-controls976 (fn [world]977 (set-gravity world Vector3f/ZERO)978 (light-up-everything world)979 (.setTimer world (NanoTimer.)))980 (fn [_ _]981 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))982 #+end_src990 * COMMENT generate Source991 #+begin_src clojure :tangle ../src/cortex/body.clj992 <<proprioception>>993 <<motor-control>>994 #+end_src996 #+begin_src clojure :tangle ../src/cortex/test/body.clj997 <<test-body>>998 #+end_src