view org/body.org @ 134:ac350a0ac6b0

proprioception refrence frame is wrong, trying to fix...
author Robert McIntyre <rlm@mit.edu>
date Wed, 01 Feb 2012 02:44:07 -0700
parents 2ed7e60d3821
children 421cc43441ae
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 (import com.jme3.scene.Node)
22 (defn joint-proprioception [#^Node parts #^Node joint]
23 (let [[obj-a obj-b] (cortex.silly/joint-targets parts joint)
24 joint-rot (.getWorldRotation joint)
25 x (.mult joint-rot Vector3f/UNIT_X)
26 y (.mult joint-rot Vector3f/UNIT_Y)
27 z (.mult joint-rot Vector3f/UNIT_Z)]
28 ;; this function will report proprioceptive information for the
29 ;; joint
30 (fn []
31 ;; x is the "twist" axis, y and z are the "bend" axes
32 (let [rot-a (.getWorldRotation obj-a)
33 rot-b (.getWorldRotation obj-b)
34 relative (.mult (.inverse rot-a) rot-b)
35 basis (doto (Matrix3f.)
36 (.setColumn 0 x)
37 (.setColumn 1 y)
38 (.setColumn 2 z))
39 rotation-about-joint
40 (doto (Quaternion.)
41 (.fromRotationMatrix
42 (.mult (.invert basis)
43 (.toRotationMatrix relative))))
44 [yaw roll pitch]
45 (seq (.toAngles rotation-about-joint nil))]
46 ;;return euler angles of the quaternion around the new basis
47 ;;[yaw pitch roll]
48 [yaw roll pitch]
49 ))))
52 (defn proprioception
53 "Create a function that provides proprioceptive information about an
54 entire body."
55 [#^Node creature]
56 ;; extract the body's joints
57 (let [joints (cortex.silly/creature-joints creature)
58 senses (map (partial joint-proprioception creature) joints)]
59 (fn []
60 (map #(%) senses))))
62 #+end_src
64 #+results: proprioception
65 : #'cortex.body/proprioception
67 * Motor Control
68 #+name: motor-control
69 #+begin_src clojure
70 (in-ns 'cortex.body)
72 ;; surprisingly enough, terristerial creatures only move by using
73 ;; torque applied about their joints. There's not a single straight
74 ;; line of force in the human body at all! (A straight line of force
75 ;; would correspond to some sort of jet or rocket propulseion.)
77 (defn vector-motor-control
78 "Create a function that accepts a sequence of Vector3f objects that
79 describe the torque to be applied to each part of the body."
80 [body]
81 (let [nodes (node-seq body)
82 controls (keep #(.getControl % RigidBodyControl) nodes)]
83 (fn [torques]
84 (map #(.applyTorque %1 %2)
85 controls torques))))
86 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
87 #+end_src
89 ## note -- might want to add a lower dimensional, discrete version of
90 ## this if it proves useful from a x-modal clustering perspective.
92 * Examples
94 #+name: test-body
95 #+begin_src clojure
96 (ns cortex.test.body
97 (:use (cortex world util body))
98 (:import
99 com.jme3.math.Vector3f
100 com.jme3.math.ColorRGBA
101 com.jme3.bullet.joints.Point2PointJoint
102 com.jme3.bullet.control.RigidBodyControl
103 com.jme3.system.NanoTimer))
105 (defn worm-segments
106 "Create multiple evenly spaced box segments. They're fabulous!"
107 [segment-length num-segments interstitial-space radius]
108 (letfn [(nth-segment
109 [n]
110 (box segment-length radius radius :mass 0.1
111 :position
112 (Vector3f.
113 (* 2 n (+ interstitial-space segment-length)) 0 0)
114 :name (str "worm-segment" n)
115 :color (ColorRGBA/randomColor)))]
116 (map nth-segment (range num-segments))))
118 (defn connect-at-midpoint
119 "Connect two physics objects with a Point2Point joint constraint at
120 the point equidistant from both objects' centers."
121 [segmentA segmentB]
122 (let [centerA (.getWorldTranslation segmentA)
123 centerB (.getWorldTranslation segmentB)
124 midpoint (.mult (.add centerA centerB) (float 0.5))
125 pivotA (.subtract midpoint centerA)
126 pivotB (.subtract midpoint centerB)
128 ;; A side-effect of creating a joint registers
129 ;; it with both physics objects which in turn
130 ;; will register the joint with the physics system
131 ;; when the simulation is started.
132 joint (Point2PointJoint.
133 (.getControl segmentA RigidBodyControl)
134 (.getControl segmentB RigidBodyControl)
135 pivotA
136 pivotB)]
137 segmentB))
139 (defn eve-worm
140 "Create a worm-like body bound by invisible joint constraints."
141 []
142 (let [segments (worm-segments 0.2 5 0.1 0.1)]
143 (dorun (map (partial apply connect-at-midpoint)
144 (partition 2 1 segments)))
145 (nodify "worm" segments)))
147 (defn worm-pattern
148 "This is a simple, mindless motor control pattern that drives the
149 second segment of the worm's body at an offset angle with
150 sinusoidally varying strength."
151 [time]
152 (let [angle (* Math/PI (/ 9 20))
153 direction (Vector3f. 0 (Math/sin angle) (Math/cos angle))]
154 [Vector3f/ZERO
155 (.mult
156 direction
157 (float (* 2 (Math/sin (* Math/PI 2 (/ (rem time 300 ) 300))))))
158 Vector3f/ZERO
159 Vector3f/ZERO
160 Vector3f/ZERO]))
162 (defn test-motor-control
163 "Testing motor-control:
164 You should see a multi-segmented worm-like object fall onto the
165 table and begin writhing and moving."
166 []
167 (let [worm (eve-worm)
168 time (atom 0)
169 worm-motor-map (vector-motor-control worm)]
170 (world
171 (nodify [worm
172 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
173 :color ColorRGBA/Gray)])
174 standard-debug-controls
175 (fn [world]
176 (enable-debug world)
177 (light-up-everything world)
178 (comment
179 (com.aurellem.capture.Capture/captureVideo
180 world
181 (file-str "/home/r/proj/cortex/tmp/moving-worm")))
182 )
184 (fn [_ _]
185 (swap! time inc)
186 (Thread/sleep 20)
187 (dorun (worm-motor-map
188 (worm-pattern @time)))))))
191 (require 'cortex.silly)
192 (defn join-at-point [obj-a obj-b world-pivot]
193 (cortex.silly/joint-dispatch
194 {:type :point}
195 (.getControl obj-a RigidBodyControl)
196 (.getControl obj-b RigidBodyControl)
197 (cortex.silly/world-to-local obj-a world-pivot)
198 (cortex.silly/world-to-local obj-b world-pivot)
199 nil
200 ))
202 (import com.jme3.bullet.collision.PhysicsCollisionObject)
204 (defn blab-* []
205 (let [hand (box 0.5 0.2 0.2 :position (Vector3f. 0 0 0)
206 :mass 0 :color ColorRGBA/Green)
207 finger (box 0.5 0.2 0.2 :position (Vector3f. 2.4 0 0)
208 :mass 1 :color ColorRGBA/Red)
209 connection-point (Vector3f. 1.2 0 0)
210 root (nodify [hand finger])]
212 (join-at-point hand finger (Vector3f. 1.2 0 0))
214 (.setCollisionGroup
215 (.getControl hand RigidBodyControl)
216 PhysicsCollisionObject/COLLISION_GROUP_NONE)
217 (world
218 root
219 standard-debug-controls
220 (fn [world]
221 (enable-debug world)
222 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
223 (set-gravity world Vector3f/ZERO)
224 )
225 no-op)))
226 (import java.awt.image.BufferedImage)
228 (defn draw-sprite [image sprite x y color ]
229 (dorun
230 (for [[u v] sprite]
231 (.setRGB image (+ u x) (+ v y) color))))
233 (defn view-angle
234 "create a debug view of an angle"
235 [color]
236 (let [image (BufferedImage. 50 50 BufferedImage/TYPE_INT_RGB)
237 previous (atom [25 25])
238 sprite [[0 0] [0 1]
239 [0 -1] [-1 0] [1 0]]]
240 (fn [angle]
241 (let [angle (float angle)]
242 (let [position
243 [(+ 25 (int (* 20 (Math/cos angle))))
244 (+ 25 (int (* 20(Math/sin angle))))]]
245 (draw-sprite image sprite (@previous 0) (@previous 1) 0x000000)
246 (draw-sprite image sprite (position 0) (position 1) color)
247 (reset! previous position))
248 image))))
250 (defn proprioception-debug-window
251 []
252 (let [yaw (view-angle 0xFF0000)
253 roll (view-angle 0x00FF00)
254 pitch (view-angle 0xFFFFFF)
255 v-yaw (view-image)
256 v-roll (view-image)
257 v-pitch (view-image)
258 ]
259 (fn [prop-data]
260 (dorun
261 (map
262 (fn [[y r p]]
263 (v-yaw (yaw y))
264 (v-roll (roll r))
265 (v-pitch (pitch p)))
266 prop-data)))))
267 (comment
269 (defn proprioception-debug-window
270 []
271 (let [time (atom 0)]
272 (fn [prop-data]
273 (if (= 0 (rem (swap! time inc) 40))
274 (println-repl prop-data)))))
275 )
277 (comment
278 (dorun
279 (map
280 (comp
281 println-repl
282 (fn [[p y r]]
283 (format
284 "pitch: %1.2f\nyaw: %1.2f\nroll: %1.2f\n"
285 p y r)))
286 prop-data)))
292 (defn test-proprioception
293 "Testing proprioception:
294 You should see two foating bars, and a printout of pitch, yaw, and
295 roll. Pressing key-r/key-t should move the blue bar up and down and
296 change only the value of pitch. key-f/key-g moves it side to side
297 and changes yaw. key-v/key-b will spin the blue segment clockwise
298 and counterclockwise, and only affect roll."
299 []
300 (let [hand (box 1 0.2 0.2 :position (Vector3f. 0 2 0)
301 :mass 0 :color ColorRGBA/Green :name "hand")
302 finger (box 1 0.2 0.2 :position (Vector3f. 2.4 2 0)
303 :mass 1 :color ColorRGBA/Red :name "finger")
304 floor (box 10 10 10 :position (Vector3f. 0 -15 0)
305 :mass 0 :color ColorRGBA/Gray)
306 joint-node (box 0.1 0.05 0.05 :color ColorRGBA/Yellow
307 :position (Vector3f. 1.2 2 0)
308 :physical? false)
310 move-up? (atom false)
311 move-down? (atom false)
312 move-left? (atom false)
313 move-right? (atom false)
314 roll-left? (atom false)
315 roll-right? (atom false)
316 control (.getControl finger RigidBodyControl)
317 time (atom 0)
318 joint (join-at-point hand finger (Vector3f. 1.2 2 0 ))
319 creature (nodify [hand finger joint-node])
320 prop (joint-proprioception creature joint-node)
322 prop-view (proprioception-debug-window)
325 ]
330 (.setCollisionGroup
331 (.getControl hand RigidBodyControl)
332 PhysicsCollisionObject/COLLISION_GROUP_NONE)
335 (world
336 (nodify [hand finger floor joint-node])
337 (merge standard-debug-controls
338 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
339 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
340 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
341 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
342 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
343 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
344 (fn [world]
345 (.setTimer world (com.aurellem.capture.RatchetTimer. 60))
346 (set-gravity world (Vector3f. 0 0 0))
347 (light-up-everything world))
348 (fn [_ _]
349 (if @move-up?
350 (.applyTorque control
351 (.mult (.getPhysicsRotation control)
352 (Vector3f. 0 0 10))))
353 (if @move-down?
354 (.applyTorque control
355 (.mult (.getPhysicsRotation control)
356 (Vector3f. 0 0 -10))))
357 (if @move-left?
358 (.applyTorque control
359 (.mult (.getPhysicsRotation control)
360 (Vector3f. 0 10 0))))
361 (if @move-right?
362 (.applyTorque control
363 (.mult (.getPhysicsRotation control)
364 (Vector3f. 0 -10 0))))
365 (if @roll-left?
366 (.applyTorque control
367 (.mult (.getPhysicsRotation control)
368 (Vector3f. -1 0 0))))
369 (if @roll-right?
370 (.applyTorque control
371 (.mult (.getPhysicsRotation control)
372 (Vector3f. 1 0 0))))
374 ;;(if (= 0 (rem (swap! time inc) 20))
375 (prop-view (list (prop)))))))
377 #+end_src
379 #+results: test-body
380 : #'cortex.test.body/test-proprioception
383 * COMMENT code-limbo
384 #+begin_src clojure
385 ;;(.loadModel
386 ;; (doto (asset-manager)
387 ;; (.registerLoader BlenderModelLoader (into-array String ["blend"])))
388 ;; "Models/person/person.blend")
391 (defn load-blender-model
392 "Load a .blend file using an asset folder relative path."
393 [^String model]
394 (.loadModel
395 (doto (asset-manager)
396 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
397 model))
400 (defn view-model [^String model]
401 (view
402 (.loadModel
403 (doto (asset-manager)
404 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
405 model)))
407 (defn load-blender-scene [^String model]
408 (.loadModel
409 (doto (asset-manager)
410 (.registerLoader BlenderLoader (into-array String ["blend"])))
411 model))
413 (defn worm
414 []
415 (.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml"))
417 (defn oto
418 []
419 (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml"))
421 (defn sinbad
422 []
423 (.loadModel (asset-manager) "Models/Sinbad/Sinbad.mesh.xml"))
425 (defn worm-blender
426 []
427 (first (seq (.getChildren (load-blender-model
428 "Models/anim2/simple-worm.blend")))))
430 (defn body
431 "given a node with a SkeletonControl, will produce a body sutiable
432 for AI control with movement and proprioception."
433 [node]
434 (let [skeleton-control (.getControl node SkeletonControl)
435 krc (KinematicRagdollControl.)]
436 (comment
437 (dorun
438 (map #(.addBoneName krc %)
439 ["mid2" "tail" "head" "mid1" "mid3" "mid4" "Dummy-Root" ""]
440 ;;"mid2" "mid3" "tail" "head"]
441 )))
442 (.addControl node krc)
443 (.setRagdollMode krc)
444 )
445 node
446 )
447 (defn show-skeleton [node]
448 (let [sd
450 (doto
451 (SkeletonDebugger. "aurellem-skel-debug"
452 (skel node))
453 (.setMaterial (green-x-ray)))]
454 (.attachChild node sd)
455 node))
459 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
461 ;; this could be a good way to give objects special properties like
462 ;; being eyes and the like
464 (.getUserData
465 (.getChild
466 (load-blender-model "Models/property/test.blend") 0)
467 "properties")
469 ;; the properties are saved along with the blender file.
470 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
475 (defn init-debug-skel-node
476 [f debug-node skeleton]
477 (let [bones
478 (map #(.getBone skeleton %)
479 (range (.getBoneCount skeleton)))]
480 (dorun (map #(.setUserControl % true) bones))
481 (dorun (map (fn [b]
482 (println (.getName b)
483 " -- " (f b)))
484 bones))
485 (dorun
486 (map #(.attachChild
487 debug-node
488 (doto
489 (sphere 0.1
490 :position (f %)
491 :physical? false)
492 (.setMaterial (green-x-ray))))
493 bones)))
494 debug-node)
496 (import jme3test.bullet.PhysicsTestHelper)
499 (defn test-zzz [the-worm world value]
500 (if (not value)
501 (let [skeleton (skel the-worm)]
502 (println-repl "enabling bones")
503 (dorun
504 (map
505 #(.setUserControl (.getBone skeleton %) true)
506 (range (.getBoneCount skeleton))))
509 (let [b (.getBone skeleton 2)]
510 (println-repl "moving " (.getName b))
511 (println-repl (.getLocalPosition b))
512 (.setUserTransforms b
513 Vector3f/UNIT_X
514 Quaternion/IDENTITY
515 ;;(doto (Quaternion.)
516 ;; (.fromAngles (/ Math/PI 2)
517 ;; 0
518 ;; 0
520 (Vector3f. 1 1 1))
521 )
523 (println-repl "hi! <3"))))
526 (defn test-ragdoll []
528 (let [the-worm
530 ;;(.loadModel (asset-manager) "Models/anim2/Cube.mesh.xml")
531 (doto (show-skeleton (worm-blender))
532 (.setLocalTranslation (Vector3f. 0 10 0))
533 ;;(worm)
534 ;;(oto)
535 ;;(sinbad)
536 )
537 ]
540 (.start
541 (world
542 (doto (Node.)
543 (.attachChild the-worm))
544 {"key-return" (fire-cannon-ball)
545 "key-space" (partial test-zzz the-worm)
546 }
547 (fn [world]
548 (light-up-everything world)
549 (PhysicsTestHelper/createPhysicsTestWorld
550 (.getRootNode world)
551 (asset-manager)
552 (.getPhysicsSpace
553 (.getState (.getStateManager world) BulletAppState)))
554 (set-gravity world Vector3f/ZERO)
555 ;;(.setTimer world (NanoTimer.))
556 ;;(org.lwjgl.input.Mouse/setGrabbed false)
557 )
558 no-op
559 )
562 )))
565 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
566 ;;; here is the ragdoll stuff
568 (def worm-mesh (.getMesh (.getChild (worm-blender) 0)))
569 (def mesh worm-mesh)
571 (.getFloatBuffer mesh VertexBuffer$Type/Position)
572 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
573 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
576 (defn position [index]
577 (.get
578 (.getFloatBuffer worm-mesh VertexBuffer$Type/Position)
579 index))
581 (defn bones [index]
582 (.get
583 (.getData (.getBuffer mesh VertexBuffer$Type/BoneIndex))
584 index))
586 (defn bone-weights [index]
587 (.get
588 (.getFloatBuffer mesh VertexBuffer$Type/BoneWeight)
589 index))
593 (defn vertex-bones [vertex]
594 (vec (map (comp int bones) (range (* vertex 4) (+ (* vertex 4) 4)))))
596 (defn vertex-weights [vertex]
597 (vec (map (comp float bone-weights) (range (* vertex 4) (+ (* vertex 4) 4)))))
599 (defn vertex-position [index]
600 (let [offset (* index 3)]
601 (Vector3f. (position offset)
602 (position (inc offset))
603 (position (inc(inc offset))))))
605 (def vertex-info (juxt vertex-position vertex-bones vertex-weights))
607 (defn bone-control-color [index]
608 (get {[1 0 0 0] ColorRGBA/Red
609 [1 2 0 0] ColorRGBA/Magenta
610 [2 0 0 0] ColorRGBA/Blue}
611 (vertex-bones index)
612 ColorRGBA/White))
614 (defn influence-color [index bone-num]
615 (get
616 {(float 0) ColorRGBA/Blue
617 (float 0.5) ColorRGBA/Green
618 (float 1) ColorRGBA/Red}
619 ;; find the weight of the desired bone
620 ((zipmap (vertex-bones index)(vertex-weights index))
621 bone-num)
622 ColorRGBA/Blue))
624 (def worm-vertices (set (map vertex-info (range 60))))
627 (defn test-info []
628 (let [points (Node.)]
629 (dorun
630 (map #(.attachChild points %)
631 (map #(sphere 0.01
632 :position (vertex-position %)
633 :color (influence-color % 1)
634 :physical? false)
635 (range 60))))
636 (view points)))
639 (defrecord JointControl [joint physics-space]
640 PhysicsControl
641 (setPhysicsSpace [this space]
642 (dosync
643 (ref-set (:physics-space this) space))
644 (.addJoint space (:joint this)))
645 (update [this tpf])
646 (setSpatial [this spatial])
647 (render [this rm vp])
648 (getPhysicsSpace [this] (deref (:physics-space this)))
649 (isEnabled [this] true)
650 (setEnabled [this state]))
652 (defn add-joint
653 "Add a joint to a particular object. When the object is added to the
654 PhysicsSpace of a simulation, the joint will also be added"
655 [object joint]
656 (let [control (JointControl. joint (ref nil))]
657 (.addControl object control))
658 object)
661 (defn hinge-world
662 []
663 (let [sphere1 (sphere)
664 sphere2 (sphere 1 :position (Vector3f. 3 3 3))
665 joint (Point2PointJoint.
666 (.getControl sphere1 RigidBodyControl)
667 (.getControl sphere2 RigidBodyControl)
668 Vector3f/ZERO (Vector3f. 3 3 3))]
669 (add-joint sphere1 joint)
670 (doto (Node. "hinge-world")
671 (.attachChild sphere1)
672 (.attachChild sphere2))))
675 (defn test-joint []
676 (view (hinge-world)))
678 ;; (defn copier-gen []
679 ;; (let [count (atom 0)]
680 ;; (fn [in]
681 ;; (swap! count inc)
682 ;; (clojure.contrib.duck-streams/copy
683 ;; in (File. (str "/home/r/tmp/mao-test/clojure-images/"
684 ;; ;;/home/r/tmp/mao-test/clojure-images
685 ;; (format "%08d.png" @count)))))))
686 ;; (defn decrease-framerate []
687 ;; (map
688 ;; (copier-gen)
689 ;; (sort
690 ;; (map first
691 ;; (partition
692 ;; 4
693 ;; (filter #(re-matches #".*.png$" (.getCanonicalPath %))
694 ;; (file-seq
695 ;; (file-str
696 ;; "/home/r/media/anime/mao-temp/images"))))))))
700 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
702 (defn proprioception
703 "Create a proprioception map that reports the rotations of the
704 various limbs of the creature's body"
705 [creature]
706 [#^Node creature]
707 (let [
708 nodes (node-seq creature)
709 joints
710 (map
711 :joint
712 (filter
713 #(isa? (class %) JointControl)
714 (reduce
715 concat
716 (map (fn [node]
717 (map (fn [num] (.getControl node num))
718 (range (.getNumControls node))))
719 nodes))))]
720 (fn []
721 (reduce concat (map relative-positions (list (first joints)))))))
724 (defn skel [node]
725 (doto
726 (.getSkeleton
727 (.getControl node SkeletonControl))
728 ;; this is necessary to force the skeleton to have accurate world
729 ;; transforms before it is rendered to the screen.
730 (.resetAndUpdate)))
732 (defn green-x-ray []
733 (doto (Material. (asset-manager)
734 "Common/MatDefs/Misc/Unshaded.j3md")
735 (.setColor "Color" ColorRGBA/Green)
736 (-> (.getAdditionalRenderState)
737 (.setDepthTest false))))
739 (defn test-worm []
740 (.start
741 (world
742 (doto (Node.)
743 ;;(.attachChild (point-worm))
744 (.attachChild (load-blender-model
745 "Models/anim2/joint-worm.blend"))
747 (.attachChild (box 10 1 10
748 :position (Vector3f. 0 -2 0) :mass 0
749 :color (ColorRGBA/Gray))))
750 {
751 "key-space" (fire-cannon-ball)
752 }
753 (fn [world]
754 (enable-debug world)
755 (light-up-everything world)
756 ;;(.setTimer world (NanoTimer.))
757 )
758 no-op)))
762 ;; defunct movement stuff
763 (defn torque-controls [control]
764 (let [torques
765 (concat
766 (map #(Vector3f. 0 (Math/sin %) (Math/cos %))
767 (range 0 (* Math/PI 2) (/ (* Math/PI 2) 20)))
768 [Vector3f/UNIT_X])]
769 (map (fn [torque-axis]
770 (fn [torque]
771 (.applyTorque
772 control
773 (.mult (.mult (.getPhysicsRotation control)
774 torque-axis)
775 (float
776 (* (.getMass control) torque))))))
777 torques)))
779 (defn motor-map
780 "Take a creature and generate a function that will enable fine
781 grained control over all the creature's limbs."
782 [#^Node creature]
783 (let [controls (keep #(.getControl % RigidBodyControl)
784 (node-seq creature))
785 limb-controls (reduce concat (map torque-controls controls))
786 body-control (partial map #(%1 %2) limb-controls)]
787 body-control))
789 (defn test-motor-map
790 "see how torque works."
791 []
792 (let [finger (box 3 0.5 0.5 :position (Vector3f. 0 2 0)
793 :mass 1 :color ColorRGBA/Green)
794 motor-map (motor-map finger)]
795 (world
796 (nodify [finger
797 (box 10 0.5 10 :position (Vector3f. 0 -5 0) :mass 0
798 :color ColorRGBA/Gray)])
799 standard-debug-controls
800 (fn [world]
801 (set-gravity world Vector3f/ZERO)
802 (light-up-everything world)
803 (.setTimer world (NanoTimer.)))
804 (fn [_ _]
805 (dorun (motor-map [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]))))))
806 #+end_src
814 * COMMENT generate Source
815 #+begin_src clojure :tangle ../src/cortex/body.clj
816 <<proprioception>>
817 <<motor-control>>
818 #+end_src
820 #+begin_src clojure :tangle ../src/cortex/test/body.clj
821 <<test-body>>
822 #+end_src