view org/body.org @ 130:b26017d1fe9a

added workaround for problem with point2point joints in native bullet; added basic muscle description image.
author Robert McIntyre <rlm@mit.edu>
date Mon, 30 Jan 2012 05:47:51 -0700
parents fb810a2c50c2
children e98850b83c2c
line wrap: on
line source
1 #+title: The BODY!!!
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description: Simulating a body (movement, touch, propioception) in jMonkeyEngine3.
5 #+SETUPFILE: ../../aurellem/org/setup.org
6 #+INCLUDE: ../../aurellem/org/level-0.org
8 * Proprioception
9 #+name: proprioception
10 #+begin_src clojure
11 (ns cortex.body
12 (:use (cortex world util))
13 (:import
14 com.jme3.math.Vector3f
15 com.jme3.math.Quaternion
16 com.jme3.math.Vector2f
17 com.jme3.math.Matrix3f
18 com.jme3.bullet.control.RigidBodyControl))
20 (defn any-orthogonal
21 "Generate an arbitray (but stable) orthogonal vector to a given
22 vector."
23 [vector]
24 (let [x (.getX vector)
25 y (.getY vector)
26 z (.getZ vector)]
27 (cond
28 (not= x (float 0)) (Vector3f. (- z) 0 x)
29 (not= y (float 0)) (Vector3f. 0 (- z) y)
30 (not= z (float 0)) (Vector3f. 0 (- z) y)
31 true Vector3f/ZERO)))
33 (defn project-quaternion
34 "From http://stackoverflow.com/questions/3684269/
35 component-of-a-quaternion-rotation-around-an-axis.
37 Determine the amount of rotation a quaternion will
38 cause about a given axis."
39 [#^Quaternion q #^Vector3f axis]
40 (let [basis-1 (any-orthogonal axis)
41 basis-2 (.cross axis basis-1)
42 rotated (.mult q basis-1)
43 alpha (.dot basis-1 (.project rotated basis-1))
44 beta (.dot basis-2 (.project rotated basis-2))]
45 (Math/atan2 beta alpha)))
47 (defn joint-proprioception
48 "Relative position information for a two-part system connected by a
49 joint. Gives the pitch, yaw, and roll of the 'B' object relative to
50 the 'A' object, as determined by the joint."
51 [joint]
52 (let [object-a (.getUserObject (.getBodyA joint))
53 object-b (.getUserObject (.getBodyB joint))
54 arm-a
55 (.normalize
56 (.subtract
57 (.localToWorld object-a (.getPivotA joint) nil)
58 (.getWorldTranslation object-a)))
59 rotate-a
60 (doto (Matrix3f.)
61 (.fromStartEndVectors arm-a Vector3f/UNIT_X))
62 arm-b
63 (.mult
64 rotate-a
65 (.normalize
66 (.subtract
67 (.localToWorld object-b (.getPivotB joint) nil)
68 (.getWorldTranslation object-b))))
69 pitch
70 (.angleBetween
71 (.normalize (Vector2f. (.getX arm-b) (.getY arm-b)))
72 (Vector2f. 1 0))
73 yaw
74 (.angleBetween
75 (.normalize (Vector2f. (.getX arm-b) (.getZ arm-b)))
76 (Vector2f. 1 0))
78 roll
79 (project-quaternion
80 (.mult
81 (.getLocalRotation object-b)
82 (doto (Quaternion.)
83 (.fromRotationMatrix rotate-a)))
84 arm-b)]
85 ;;(println-repl (.getName object-a) (.getName object-b))
86 [pitch yaw roll]))
88 (defn proprioception
89 "Create a function that provides proprioceptive information about an
90 entire body."
91 [body]
92 ;; extract the body's joints
93 (let [joints
94 (distinct
95 (reduce
96 concat
97 (map #(.getJoints %)
98 (keep
99 #(.getControl % RigidBodyControl)
100 (node-seq body)))))]
101 (fn []
102 (map joint-proprioception joints))))
104 #+end_src
106 * Motor Control
107 #+name: motor-control
108 #+begin_src clojure
109 (in-ns 'cortex.body)
111 ;; surprisingly enough, terristerial creatures only move by using
112 ;; torque applied about their joints. There's not a single straight
113 ;; line of force in the human body at all! (A straight line of force
114 ;; would correspond to some sort of jet or rocket propulseion.)
116 (defn vector-motor-control
117 "Create a function that accepts a sequence of Vector3f objects that
118 describe the torque to be applied to each part of the body."
119 [body]
120 (let [nodes (node-seq body)
121 controls (keep #(.getControl % RigidBodyControl) nodes)]
122 (fn [torques]
123 (map #(.applyTorque %1 %2)
124 controls torques))))
125 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
126 #+end_src
128 ## note -- might want to add a lower dimensional, discrete version of
129 ## this if it proves useful from a x-modal clustering perspective.
131 * Examples
133 #+name: test-body
134 #+begin_src clojure
135 (ns cortex.test.body
136 (:use (cortex world util body))
137 (:import
138 com.jme3.math.Vector3f
139 com.jme3.math.ColorRGBA
140 com.jme3.bullet.joints.Point2PointJoint
141 com.jme3.bullet.control.RigidBodyControl
142 com.jme3.system.NanoTimer))
144 (defn worm-segments
145 "Create multiple evenly spaced box segments. They're fabulous!"
146 [segment-length num-segments interstitial-space radius]
147 (letfn [(nth-segment
148 [n]
149 (box segment-length radius radius :mass 0.1
150 :position
151 (Vector3f.
152 (* 2 n (+ interstitial-space segment-length)) 0 0)
153 :name (str "worm-segment" n)
154 :color (ColorRGBA/randomColor)))]
155 (map nth-segment (range num-segments))))
157 (defn connect-at-midpoint
158 "Connect two physics objects with a Point2Point joint constraint at
159 the point equidistant from both objects' centers."
160 [segmentA segmentB]
161 (let [centerA (.getWorldTranslation segmentA)
162 centerB (.getWorldTranslation segmentB)
163 midpoint (.mult (.add centerA centerB) (float 0.5))
164 pivotA (.subtract midpoint centerA)
165 pivotB (.subtract midpoint centerB)
167 ;; A side-effect of creating a joint registers
168 ;; it with both physics objects which in turn
169 ;; will register the joint with the physics system
170 ;; when the simulation is started.
171 joint (Point2PointJoint.
172 (.getControl segmentA RigidBodyControl)
173 (.getControl segmentB RigidBodyControl)
174 pivotA
175 pivotB)]
176 segmentB))
178 (defn eve-worm
179 "Create a worm-like body bound by invisible joint constraints."
180 []
181 (let [segments (worm-segments 0.2 5 0.1 0.1)]
182 (dorun (map (partial apply connect-at-midpoint)
183 (partition 2 1 segments)))
184 (nodify "worm" segments)))
186 (defn worm-pattern
187 "This is a simple, mindless motor control pattern that drives the
188 second segment of the worm's body at an offset angle with
189 sinusoidally varying strength."
190 [time]
191 (let [angle (* Math/PI (/ 9 20))
192 direction (Vector3f. 0 (Math/sin angle) (Math/cos angle))]
193 [Vector3f/ZERO
194 (.mult
195 direction
196 (float (* 2 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))))
197 Vector3f/ZERO
198 Vector3f/ZERO
199 Vector3f/ZERO]))
201 (defn test-motor-control
202 "Testing motor-control:
203 You should see a multi-segmented worm-like object fall onto the
204 table and begin writhing and moving."
205 []
206 (let [worm (eve-worm)
207 time (atom 0)
208 worm-motor-map (vector-motor-control worm)]
209 (world
210 (nodify [worm
211 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
212 :color ColorRGBA/Gray)])
213 standard-debug-controls
214 (fn [world]
215 (enable-debug world)
216 (light-up-everything world)
217 (comment
218 (com.aurellem.capture.Capture/captureVideo
219 world
220 (file-str "/home/r/proj/cortex/tmp/moving-worm")))
221 )
223 (fn [_ _]
224 (swap! time inc)
225 (Thread/sleep 20)
226 (dorun (worm-motor-map
227 (worm-pattern @time)))))))
230 (require 'cortex.silly)
231 (defn join-at-point [obj-a obj-b world-pivot]
232 (cortex.silly/joint-dispatch
233 {:type :point}
234 (.getControl obj-a RigidBodyControl)
235 (.getControl obj-b RigidBodyControl)
236 (cortex.silly/world-to-local obj-a world-pivot)
237 (cortex.silly/world-to-local obj-b world-pivot)
238 nil
239 ))
243 (defn blab-* []
244 (let [hand (box 0.5 0.2 0.2 :position (Vector3f. 0 0 0)
245 :mass 0 :color ColorRGBA/Green)
246 finger (box 0.5 0.2 0.2 :position (Vector3f. 2.4 0 0)
247 :mass 1 :color ColorRGBA/Red)
248 connection-point (Vector3f. 1.2 0 0)
249 root (nodify [hand finger])]
251 (join-at-point hand finger (Vector3f. 1.2 0 0))
253 (.setCollisionGroup
254 (.getControl hand RigidBodyControl)
255 PhysicsCollisionObject/COLLISION_GROUP_NONE)
256 (world
257 root
258 standard-debug-controls
259 (fn [world]
260 (enable-debug world)
261 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
262 (set-gravity world Vector3f/ZERO)
263 )
264 no-op)))
266 (defn proprioception-debug-window
267 []
268 (let [vi (view-image)]
269 (fn [prop-data]
270 (dorun
271 (map
272 (comp
273 println-repl
274 (fn [[p y r]]
275 (format
276 "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n"
277 p y r)))
278 prop-data)))))
284 (defn test-proprioception
285 "Testing proprioception:
286 You should see two foating bars, and a printout of pitch, yaw, and
287 roll. Pressing key-r/key-t should move the blue bar up and down and
288 change only the value of pitch. key-f/key-g moves it side to side
289 and changes yaw. key-v/key-b will spin the blue segment clockwise
290 and counterclockwise, and only affect roll."
291 []
292 (let [hand (box 1 0.2 0.2 :position (Vector3f. 0 2 0)
293 :mass 0 :color ColorRGBA/Green)
294 finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0)
295 :mass 1 :color ColorRGBA/Red)
296 floor (box 10 0.5 10 :position (Vector3f. 0 -5 0)
297 :mass 0 :color ColorRGBA/Gray)
299 move-up? (atom false)
300 move-down? (atom false)
301 move-left? (atom false)
302 move-right? (atom false)
303 roll-left? (atom false)
304 roll-right? (atom false)
305 control (.getControl finger RigidBodyControl)
306 time (atom 0)
307 joint (join-at-point hand finger (Vector3f. 1.2 2 0 ))
308 creature (nodify [hand finger])
309 prop (proprioception creature)
311 prop-view (proprioception-debug-window)
314 ]
318 (.setCollisionGroup
319 (.getControl hand RigidBodyControl)
320 PhysicsCollisionObject/COLLISION_GROUP_NONE)
323 (world
324 (nodify [hand finger floor])
325 (merge standard-debug-controls
326 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
327 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
328 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
329 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
330 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
331 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
332 (fn [world]
333 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
334 (set-gravity world (Vector3f. 0 0 0))
335 (light-up-everything world))
336 (fn [_ _]
337 (if @move-up?
338 (.applyTorque control
339 (.mult (.getPhysicsRotation control)
340 (Vector3f. 0 0 10))))
341 (if @move-down?
342 (.applyTorque control
343 (.mult (.getPhysicsRotation control)
344 (Vector3f. 0 0 -10))))
345 (if @move-left?
346 (.applyTorque control
347 (.mult (.getPhysicsRotation control)
348 (Vector3f. 0 10 0))))
349 (if @move-right?
350 (.applyTorque control
351 (.mult (.getPhysicsRotation control)
352 (Vector3f. 0 -10 0))))
353 (if @roll-left?
354 (.applyTorque control
355 (.mult (.getPhysicsRotation control)
356 (Vector3f. -1 0 0))))
357 (if @roll-right?
358 (.applyTorque control
359 (.mult (.getPhysicsRotation control)
360 (Vector3f. 1 0 0))))
362 (if (= 0 (rem (swap! time inc) 20))
363 (prop-view
364 (joint-proprioception joint)))))))
367 #+end_src
369 #+results: test-body
370 : #'cortex.test.body/test-proprioception
373 * COMMENT code-limbo
374 #+begin_src clojure
375 ;;(.loadModel
376 ;; (doto (asset-manager)
377 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
378 ;; "Models/person/person.blend")
381 (defn load-blender-model
382 "Load a .blend file using an asset folder relative path."
383 [^String model]
384 (.loadModel
385 (doto (asset-manager)
386 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
387 model))
390 (defn view-model [^String model]
391 (view
392 (.loadModel
393 (doto (asset-manager)
394 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
395 model)))
397 (defn load-blender-scene [^String model]
398 (.loadModel
399 (doto (asset-manager)
400 (.registerLoader BlenderLoader (into-array String ["blend"])))
401 model))
403 (defn worm
404 []
405 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
407 (defn oto
408 []
409 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
411 (defn sinbad
412 []
413 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
415 (defn worm-blender
416 []
417 (first (seq (.getChildren (load-blender-model
418 "Models/anim2/simple-worm.blend")))))
420 (defn body
421 "given a node with a SkeletonControl, will produce a body sutiable
422 for AI control with movement and proprioception."
423 [node]
424 (let [skeleton-control (.getControl node SkeletonControl)
425 krc (KinematicRagdollControl.)]
426 (comment
427 (dorun
428 (map #(.addBoneName krc %)
429 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
430 ;;"mid2" "mid3" "tail" "head"]
431 )))
432 (.addControl node krc)
433 (.setRagdollMode krc)
434 )
435 node
436 )
437 (defn show-skeleton [node]
438 (let [sd
440 (doto
441 (SkeletonDebugger. "aurellem-skel-debug"
442 (skel node))
443 (.setMaterial (green-x-ray)))]
444 (.attachChild node sd)
445 node))
449 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
451 ;; this could be a good way to give objects special properties like
452 ;; being eyes and the like
454 (.getUserData
455 (.getChild
456 (load-blender-model "Models/property/test.blend") 0)
457 "properties")
459 ;; the properties are saved along with the blender file.
460 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
465 (defn init-debug-skel-node
466 [f debug-node skeleton]
467 (let [bones
468 (map #(.getBone skeleton %)
469 (range (.getBoneCount skeleton)))]
470 (dorun (map #(.setUserControl % true) bones))
471 (dorun (map (fn [b]
472 (println (.getName b)
473 " -- " (f b)))
474 bones))
475 (dorun
476 (map #(.attachChild
477 debug-node
478 (doto
479 (sphere 0.1
480 :position (f %)
481 :physical? false)
482 (.setMaterial (green-x-ray))))
483 bones)))
484 debug-node)
486 (import jme3test.bullet.PhysicsTestHelper)
489 (defn test-zzz [the-worm world value]
490 (if (not value)
491 (let [skeleton (skel the-worm)]
492 (println-repl "enabling bones")
493 (dorun
494 (map
495 #(.setUserControl (.getBone skeleton %) true)
496 (range (.getBoneCount skeleton))))
499 (let [b (.getBone skeleton 2)]
500 (println-repl "moving " (.getName b))
501 (println-repl (.getLocalPosition b))
502 (.setUserTransforms b
503 Vector3f/UNIT_X
504 Quaternion/IDENTITY
505 ;;(doto (Quaternion.)
506 ;; (.fromAngles (/ Math/PI 2)
507 ;; 0
508 ;; 0
510 (Vector3f. 1 1 1))
511 )
513 (println-repl "hi! <3"))))
516 (defn test-ragdoll []
518 (let [the-worm
520 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
521 (doto (show-skeleton (worm-blender))
522 (.setLocalTranslation (Vector3f. 0 10 0))
523 ;;(worm)
524 ;;(oto)
525 ;;(sinbad)
526 )
527 ]
530 (.start
531 (world
532 (doto (Node.)
533 (.attachChild the-worm))
534 {"key-return" (fire-cannon-ball)
535 "key-space" (partial test-zzz the-worm)
536 }
537 (fn [world]
538 (light-up-everything world)
539 (PhysicsTestHelper/createPhysicsTestWorld
540 (.getRootNode world)
541 (asset-manager)
542 (.getPhysicsSpace
543 (.getState (.getStateManager world) BulletAppState)))
544 (set-gravity world Vector3f/ZERO)
545 ;;(.setTimer world (NanoTimer.))
546 ;;(org.lwjgl.input.Mouse/setGrabbed false)
547 )
548 no-op
549 )
552 )))
555 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
556 ;;; here is the ragdoll stuff
558 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
559 (def mesh worm-mesh)
561 (.getFloatBuffer mesh VertexBuffer$Type/Position)
562 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
563 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
566 (defn position [index]
567 (.get
568 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
569 index))
571 (defn bones [index]
572 (.get
573 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
574 index))
576 (defn bone-weights [index]
577 (.get
578 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
579 index))
583 (defn vertex-bones [vertex]
584 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
586 (defn vertex-weights [vertex]
587 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
589 (defn vertex-position [index]
590 (let [offset (* index 3)]
591 (Vector3f. (position offset)
592 (position (inc offset))
593 (position (inc(inc offset))))))
595 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))
597 (defn bone-control-color [index]
598 (get {[1 0 0 0] ColorRGBA/Red
599 [1 2 0 0] ColorRGBA/Magenta
600 [2 0 0 0] ColorRGBA/Blue}
601 (vertex-bones index)
602 ColorRGBA/White))
604 (defn influence-color [index bone-num]
605 (get
606 {(float 0) ColorRGBA/Blue
607 (float 0.5) ColorRGBA/Green
608 (float 1) ColorRGBA/Red}
609 ;; find the weight of the desired bone
610 ((zipmap (vertex-bones index)(vertex-weights index))
611 bone-num)
612 ColorRGBA/Blue))
614 (def worm-vertices (set (map vertex-info (range 60))))
617 (defn test-info []
618 (let [points (Node.)]
619 (dorun
620 (map #(.attachChild points %)
621 (map #(sphere 0.01
622 :position (vertex-position %)
623 :color (influence-color % 1)
624 :physical? false)
625 (range 60))))
626 (view points)))
629 (defrecord JointControl [joint physics-space]
630 PhysicsControl
631 (setPhysicsSpace [this space]
632 (dosync
633 (ref-set (:physics-space this) space))
634 (.addJoint space (:joint this)))
635 (update [this tpf])
636 (setSpatial [this spatial])
637 (render [this rm vp])
638 (getPhysicsSpace [this] (deref (:physics-space this)))
639 (isEnabled [this] true)
640 (setEnabled [this state]))
642 (defn add-joint
643 "Add a joint to a particular object. When the object is added to the
644 PhysicsSpace of a simulation, the joint will also be added"
645 [object joint]
646 (let [control (JointControl. joint (ref nil))]
647 (.addControl object control))
648 object)
651 (defn hinge-world
652 []
653 (let [sphere1 (sphere)
654 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
655 joint (Point2PointJoint.
656 (.getControl sphere1 RigidBodyControl)
657 (.getControl sphere2 RigidBodyControl)
658 Vector3f/ZERO (Vector3f. 3 3 3))]
659 (add-joint sphere1 joint)
660 (doto (Node. "hinge-world")
661 (.attachChild sphere1)
662 (.attachChild sphere2))))
665 (defn test-joint []
666 (view (hinge-world)))
668 ;; (defn copier-gen []
669 ;; (let [count (atom 0)]
670 ;; (fn [in]
671 ;; (swap! count inc)
672 ;; (clojure.contrib.duck-streams/copy
673 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"
674 ;; ;;/home/r/tmp/mao-test/clojure-images
675 ;; (format "%08d.png" @count)))))))
676 ;; (defn decrease-framerate []
677 ;; (map
678 ;; (copier-gen)
679 ;; (sort
680 ;; (map first
681 ;; (partition
682 ;; 4
683 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))
684 ;; (file-seq
685 ;; (file-str
686 ;; "/home/r/media/anime/mao-temp/images"))))))))
690 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
692 (defn proprioception
693 "Create a proprioception map that reports the rotations of the
694 various limbs of the creature's body"
695 [creature]
696 [#^Node creature]
697 (let [
698 nodes (node-seq creature)
699 joints
700 (map
701 :joint
702 (filter
703 #(isa? (class %) JointControl)
704 (reduce
705 concat
706 (map (fn [node]
707 (map (fn [num] (.getControl node num))
708 (range (.getNumControls node))))
709 nodes))))]
710 (fn []
711 (reduce concat (map relative-positions (list (first joints)))))))
714 (defn skel [node]
715 (doto
716 (.getSkeleton
717 (.getControl node SkeletonControl))
718 ;; this is necessary to force the skeleton to have accurate world
719 ;; transforms before it is rendered to the screen.
720 (.resetAndUpdate)))
722 (defn green-x-ray []
723 (doto (Material. (asset-manager)
724 "Common/MatDefs/Misc/Unshaded.j3md")
725 (.setColor "Color" ColorRGBA/Green)
726 (-> (.getAdditionalRenderState)
727 (.setDepthTest false))))
729 (defn test-worm []
730 (.start
731 (world
732 (doto (Node.)
733 ;;(.attachChild (point-worm))
734 (.attachChild (load-blender-model
735 "Models/anim2/joint-worm.blend"))
737 (.attachChild (box 10 1 10
738 :position (Vector3f. 0 -2 0) :mass 0
739 :color (ColorRGBA/Gray))))
740 {
741 "key-space" (fire-cannon-ball)
742 }
743 (fn [world]
744 (enable-debug world)
745 (light-up-everything world)
746 ;;(.setTimer world (NanoTimer.))
747 )
748 no-op)))
752 ;; defunct movement stuff
753 (defn torque-controls [control]
754 (let [torques
755 (concat
756 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))
757 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))
758 [Vector3f/UNIT_X])]
759 (map (fn [torque-axis]
760 (fn [torque]
761 (.applyTorque
762 control
763 (.mult (.mult (.getPhysicsRotation control)
764 torque-axis)
765 (float
766 (* (.getMass control) torque))))))
767 torques)))
769 (defn motor-map
770 "Take a creature and generate a function that will enable fine
771 grained control over all the creature's limbs."
772 [#^Node creature]
773 (let [controls (keep #(.getControl % RigidBodyControl)
774 (node-seq creature))
775 limb-controls (reduce concat (map torque-controls controls))
776 body-control (partial map #(%1 %2) limb-controls)]
777 body-control))
779 (defn test-motor-map
780 "see how torque works."
781 []
782 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
783 :mass 1 :color ColorRGBA/Green)
784 motor-map (motor-map finger)]
785 (world
786 (nodify [finger
787 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
788 :color ColorRGBA/Gray)])
789 standard-debug-controls
790 (fn [world]
791 (set-gravity world Vector3f/ZERO)
792 (light-up-everything world)
793 (.setTimer world (NanoTimer.)))
794 (fn [_ _]
795 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))
796 #+end_src
804 * COMMENT generate Source
805 #+begin_src clojure :tangle ../src/cortex/body.clj
806 <<proprioception>>
807 <<motor-control>>
808 #+end_src
810 #+begin_src clojure :tangle ../src/cortex/test/body.clj
811 <<test-body>>
812 #+end_src